Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
tmck-code committed Dec 9, 2024
1 parent 4dd7d6a commit 41fb60c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/pokesay/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,37 @@ func UnicodeStringLength(s string) int {
return totalLen
}

type AnsiLineToken struct {
Colour string
Text string
}

func TokeniseANSILine(line string) []AnsiLineToken {
tokens := make([]AnsiLineToken, 0)
var inAnsiCode bool

token := AnsiLineToken{}
for i, r := range line {
if i < len(line)-1 {
if line[i:i+2] == "\033[" {
inAnsiCode = true
}
}
if inAnsiCode {
token.Colour += string(r)
if r == 'm' {
inAnsiCode = false
}
} else {
token.Text += string(r)
if i == len(line)-1 {
tokens = append(tokens, token)
}
}
}
return tokens
}

// Prints a pokemon with its name & category information.
func printPokemon(args Args, index int, names []string, categoryKeys []string, GOBCowData embed.FS) {
d, _ := GOBCowData.ReadFile(pokedex.EntryFpath("build/assets/cows", index))
Expand Down
20 changes: 20 additions & 0 deletions test/pokesay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ func TestUnicodeStringLength(test *testing.T) {
Assert(expected, results, test)
}

func TestTokeniseAnsiLine(test *testing.T) {
line := "AAA  XX "
expected := []string{"", "AAA ", "", " XX ", ""}
result := pokesay.TokeniseANSILine(line)
Assert(expected, result, test)
}

func TestFlipHorizontalLine(test *testing.T) {
// The AAA has a purple fg
// The XX has a red bg
line := "AAA  XX "

// The AAA should still have a purple fg
// The XX should still have a red bg
expected := " XX  AAA"
result := pokesay.ReverseString(line)

Assert(expected, result, test)
}

func TestFlipHorizontalWithoutColour(test *testing.T) {
msg := []string{
" ▄▄ ▄▄",
Expand Down

0 comments on commit 41fb60c

Please sign in to comment.