Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
tmck-code committed Dec 26, 2024
1 parent 84f18d9 commit 26e6b2a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 36 deletions.
27 changes: 14 additions & 13 deletions src/pokesay/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,9 @@ func ReverseUnicodeString(s string) string {
}

type ANSILineToken struct {
Colour string
Text string
FGColour string
BGColour string
Text string
}

func TokeniseANSIString(msg string) [][]ANSILineToken {
Expand All @@ -247,7 +248,7 @@ func TokeniseANSIString(msg string) [][]ANSILineToken {
for _, ch := range line {
if ch == '\033' {
if text != "" {
tokens = append(tokens, ANSILineToken{bg + fg, text})
tokens = append(tokens, ANSILineToken{fg, bg, text})
colour = ""
text = ""
}
Expand All @@ -271,10 +272,10 @@ func TokeniseANSIString(msg string) [][]ANSILineToken {
}
}
if text != "" {
tokens = append(tokens, ANSILineToken{bg + fg, text})
tokens = append(tokens, ANSILineToken{fg, bg, text})
}
if (colour != "") && len(tokens) > 0 {
tokens = append(tokens, ANSILineToken{"\033[0m", ""})
tokens = append(tokens, ANSILineToken{"\033[0m", "", ""})
}
lines = append(lines, tokens)
tokens = nil
Expand All @@ -297,18 +298,18 @@ func ReverseANSIString(line string) string {
}

for idx, tokens := range lines {
needsReset := false
// needsReset := false
// ensure vertical alignment
reversed += strings.Repeat(" ", maxWidth-widths[idx])
for i := len(tokens) - 1; i >= 0; i-- {
if tokens[i].Colour != "" {
needsReset = true
}
reversed += tokens[i].Colour + ReverseUnicodeString(tokens[i].Text)
}
if needsReset {
reversed += "\033[0m"
// if tokens[i].BGColour != "" {
// needsReset = true
// }
reversed += tokens[i].FGColour + tokens[i].BGColour + ReverseUnicodeString(tokens[i].Text)
}
// if needsReset {
// reversed += "\033[0m"
// }
if idx < len(lines)-1 {
reversed += "\n"
}
Expand Down
2 changes: 1 addition & 1 deletion test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Assert(expected interface{}, result interface{}, test *testing.T) {
expectedString, resultString := fmt.Sprintf("%#v", expected), fmt.Sprintf("%#v", result)
if expectedString == resultString {
if Debug() {
fmt.Printf("\x1b[38;5;46m%s items match!\x1b[0m\n> expected:\t%s\x1b[0m\n> result:\t%s\x1b[0m\n", successMark, expected, result)
fmt.Printf("\x1b[38;5;46m%s items match!\x1b[0m\n> expected:\t%#v\x1b[0m\n> result:\t%#v\x1b[0m\n", successMark, expected, result)
}
return
}
Expand Down
48 changes: 26 additions & 22 deletions test/pokesay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestTokeniseANSIString(test *testing.T) {
input: " ▄▄ ▄▄",
expected: [][]pokesay.ANSILineToken{
{
pokesay.ANSILineToken{Colour: "", Text: " ▄▄ ▄▄"},
pokesay.ANSILineToken{FGColour: "", BGColour: "", Text: " ▄▄ ▄▄"},
},
},
},
Expand All @@ -144,9 +144,9 @@ func TestTokeniseANSIString(test *testing.T) {
input: "\x1b[38;5;129mAAA \x1b[48;5;160m XX \x1b[0m",
expected: [][]pokesay.ANSILineToken{
{
pokesay.ANSILineToken{Colour: "\x1b[38;5;129m", Text: "AAA "},
pokesay.ANSILineToken{Colour: "\x1b[48;5;160m\x1b[38;5;129m", Text: " XX "},
pokesay.ANSILineToken{Colour: "\x1b[0m", Text: ""},
pokesay.ANSILineToken{FGColour: "\x1b[38;5;129m", BGColour: "", Text: "AAA "},
pokesay.ANSILineToken{FGColour: "\x1b[48;5;160m\x1b[38;5;129m", BGColour: "", Text: " XX "},
pokesay.ANSILineToken{FGColour: "\x1b[0m", BGColour: "", Text: ""},
},
},
},
Expand All @@ -156,14 +156,14 @@ func TestTokeniseANSIString(test *testing.T) {
input: "\x1b[38;5;160m▄ \x1b[38;5;46m▄\n\x1b[38;5;190m▄",
expected: [][]pokesay.ANSILineToken{
{ // Line 1
pokesay.ANSILineToken{Colour: "\x1b[38;5;160m", Text: "▄ "},
pokesay.ANSILineToken{Colour: "\x1b[38;5;46m", Text: "▄"},
pokesay.ANSILineToken{Colour: "\x1b[0m", Text: ""},
pokesay.ANSILineToken{FGColour: "\x1b[38;5;160m", BGColour: "", Text: "▄ "},
pokesay.ANSILineToken{FGColour: "\x1b[38;5;46m", BGColour: "", Text: "▄"},
pokesay.ANSILineToken{FGColour: "\x1b[0m", BGColour: "", Text: ""},
},
{ // Line 2
pokesay.ANSILineToken{Colour: "\x1b[38;5;46m", Text: "▄ "},
pokesay.ANSILineToken{Colour: "\x1b[38;5;190m", Text: "▄"},
pokesay.ANSILineToken{Colour: "\x1b[0m", Text: ""},
pokesay.ANSILineToken{FGColour: "\x1b[38;5;46m", BGColour: "", Text: "▄ "},
pokesay.ANSILineToken{FGColour: "\x1b[38;5;190m", BGColour: "", Text: "▄"},
pokesay.ANSILineToken{FGColour: "\x1b[0m", BGColour: "", Text: ""},
},
},
},
Expand All @@ -176,11 +176,11 @@ func TestTokeniseANSIString(test *testing.T) {
// The XX should still have a red bg
expected: [][]pokesay.ANSILineToken{
{
pokesay.ANSILineToken{Colour: "", Text: " "},
pokesay.ANSILineToken{Colour: "\x1b[38;5;129m", Text: "AAA "},
pokesay.ANSILineToken{Colour: "\x1b[48;5;160m\x1b[38;5;129m", Text: " XY "},
pokesay.ANSILineToken{Colour: "\x1b[0m", Text: " "},
pokesay.ANSILineToken{Colour: "\x1b[0m", Text: ""},
pokesay.ANSILineToken{FGColour: "", BGColour: "", Text: " "},
pokesay.ANSILineToken{FGColour: "\x1b[38;5;129m", BGColour: "", Text: "AAA "},
pokesay.ANSILineToken{FGColour: "\x1b[48;5;160m\x1b[38;5;129m", BGColour: "", Text: " XY "},
pokesay.ANSILineToken{FGColour: "\x1b[0m", BGColour: "", Text: " "},
pokesay.ANSILineToken{FGColour: "\x1b[0m", BGColour: "", Text: ""},
},
},
},
Expand All @@ -192,16 +192,21 @@ func TestTokeniseANSIString(test *testing.T) {
// expected := "\x1b[0m\x1b[48;5;160m\x1b[38;5;129m XX \x1b[38;5;129m\x1b[49m AAA\x1b[0m"
expected: [][]pokesay.ANSILineToken{
{
pokesay.ANSILineToken{Colour: "\x1b[38;5;129m", Text: "AAA "},
pokesay.ANSILineToken{Colour: "\x1b[48;5;160m\x1b[38;5;129m", Text: " XX "},
pokesay.ANSILineToken{Colour: "\x1b[0m", Text: ""},
pokesay.ANSILineToken{FGColour: "\x1b[38;5;129m", Text: "AAA "},
pokesay.ANSILineToken{FGColour: "\x1b[48;5;160m", BGColour: "\x1b[38;5;129m", Text: " XX "},
pokesay.ANSILineToken{FGColour: "\x1b[0m", BGColour: "", Text: ""},
},
},
},
}
for _, tc := range testCases {
test.Run(tc.name, func(test *testing.T) {
result := pokesay.TokeniseANSIString(tc.input)
if Debug() {
fmt.Printf("input: '%v\x1b[0m'\n", tc.input)
fmt.Printf("expected: '%v\x1b[0m'\n", tc.expected)
fmt.Printf("result: '%v\x1b[0m'\n", result)
}
Assert(tc.expected, result, test)
})
}
Expand Down Expand Up @@ -258,12 +263,11 @@ func TestReverseANSIString(test *testing.T) {
for _, tc := range testCases {
test.Run(tc.name, func(test *testing.T) {
result := pokesay.ReverseANSIString(tc.input)

if Debug() {
fmt.Println("expected:", tc.expected)
fmt.Println("result:", result)
fmt.Printf("input: '%v\x1b[0m'\n", tc.input)
fmt.Printf("expected: '%v\x1b[0m'\n", tc.expected)
fmt.Printf("result: '%v\x1b[0m'\n", result)
}

Assert(tc.expected, result, test)
})
}
Expand Down

0 comments on commit 26e6b2a

Please sign in to comment.