diff --git a/src/pokesay/print.go b/src/pokesay/print.go index 0e56d71a..9a611292 100644 --- a/src/pokesay/print.go +++ b/src/pokesay/print.go @@ -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)) diff --git a/test/pokesay_test.go b/test/pokesay_test.go index a77e3095..fa7e89c2 100644 --- a/test/pokesay_test.go +++ b/test/pokesay_test.go @@ -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{ " ▄▄ ▄▄",