From 41fb60c533b9f2bd26d1e5e7fe22f1f2e713758b Mon Sep 17 00:00:00 2001 From: Tom McKeesick Date: Tue, 10 Dec 2024 07:08:02 +1100 Subject: [PATCH] update --- src/pokesay/print.go | 31 +++++++++++++++++++++++++++++++ test/pokesay_test.go | 20 ++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/pokesay/print.go b/src/pokesay/print.go index 0e56d71..9a61129 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 a77e309..fa7e89c 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{ " ▄▄ ▄▄",