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 41fb60c commit 595091d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
44 changes: 31 additions & 13 deletions src/pokesay/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ func UnicodeStringLength(s string) int {
for i, r := range s {
if i < nRunes-1 {
// detect the beginning of an ANSI escape code
// e.g. "\033[38;5;196m"
// e.g. "\x1b[38;5;196m"
// ^^^ start ^ end
if s[i:i+2] == "\033[" {
if s[i:i+2] == "\x1b[" {
ansiCode = true
}
}
Expand All @@ -218,37 +218,55 @@ func UnicodeStringLength(s string) int {
return totalLen
}

type AnsiLineToken struct {
type ANSILineToken struct {
Colour string
Text string
}

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

token := AnsiLineToken{}
currentColour := ""
currentText := ""

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

func ReverseANSIString(line string) string {
tokens := TokeniseANSIString(line)
reversed := ""

for i := len(tokens) - 1; i >= 0; i-- {
reversed += tokens[i].Colour + tokens[i].Text
}

return reversed
}

// 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
13 changes: 9 additions & 4 deletions test/pokesay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,15 @@ func TestUnicodeStringLength(test *testing.T) {
Assert(expected, results, test)
}

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

expected := []pokesay.ANSILineToken{
pokesay.ANSILineToken{Colour: "", Text: "AAA "},
pokesay.ANSILineToken{Colour: "", Text: " XX "},
pokesay.ANSILineToken{Colour: "", Text: ""},
}
result := pokesay.TokeniseANSIString(line)
Assert(expected, result, test)
}

Expand All @@ -130,7 +135,7 @@ func TestFlipHorizontalLine(test *testing.T) {
// The AAA should still have a purple fg
// The XX should still have a red bg
expected := " XX  AAA"
result := pokesay.ReverseString(line)
result := pokesay.ReverseANSIString(line)

Assert(expected, result, test)
}
Expand Down

0 comments on commit 595091d

Please sign in to comment.