Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
tmck-code committed Dec 27, 2024
1 parent 626cb36 commit c7ea9a2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
25 changes: 22 additions & 3 deletions src/pokesay/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,16 @@ func TokeniseANSIString(msg string) [][]ANSILineToken {
}
isColour = true
colour = string(ch)
} else if ch == ' ' {
if isSpaces {
text += string(ch)
} else {
if text != "" {
tokens = append(tokens, ANSILineToken{fg, bg, text, false})
text = string(ch)
}
isSpaces = true
}
} else if isColour {
colour += string(ch)
if ch == 'm' {
Expand All @@ -271,8 +281,13 @@ func TokeniseANSIString(msg string) [][]ANSILineToken {
}
}
} else {
isSpaces = (ch == ' ')
text += string(ch)
if isSpaces {
tokens = append(tokens, ANSILineToken{fg, bg, text, true})
isSpaces = false
text = string(ch)
} else {
text += string(ch)
}
}
}
if text != "" {
Expand All @@ -287,6 +302,10 @@ func TokeniseANSIString(msg string) [][]ANSILineToken {
return lines
}

// func ReverseANSITokens(tokens []ANSILineToken) []ANSILineToken {
// // reversed := make([]ANSILineToken, len(tokens))
// }

func ReverseANSIString(line string) string {
lines := TokeniseANSIString(line)
reversed := ""
Expand All @@ -305,7 +324,7 @@ func ReverseANSIString(line string) string {
// ensure vertical alignment
reversed += strings.Repeat(" ", maxWidth-widths[idx])
for i := len(tokens) - 1; i >= 0; i-- {
if tokens[i].Reset && (tokens[i].FGColour != "\x1b[0m" && tokens[i].BGColour != "\x1b[0m") {
if tokens[i].Reset && (tokens[i].FGColour != "\x1b[0m" && tokens[i].BGColour != "\x1b[0m") && reversed[len(reversed)-2:len(reversed)-1] != "\x1b[0m" {
reversed += "\x1b[0m" + tokens[i].FGColour + tokens[i].BGColour + tokens[i].Text
} else {
reversed += tokens[i].FGColour + tokens[i].BGColour + ReverseUnicodeString(tokens[i].Text)
Expand Down
4 changes: 2 additions & 2 deletions test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Debug() bool {

// Fails a test with a formatted message showing the expected vs. result. (These are both printed in %#v form)
func Fail(expected interface{}, result interface{}, test *testing.T) {
test.Fatalf("\n\x1b[38;5;196m%s items don't match!\x1b[0m\n> expected:\t%#v\x1b[0m\n> result:\t%#v\x1b[0m\n", failMark, expected, result)
test.Fatalf("\n\x1b[38;5;196m%s items don't match!\x1b[0m\n> expected:\t%#v\x1b[0m\n> result:\t%#v\x1b[0m\n\n", failMark, expected, result)
}

// Takes in an expected & result object, of any type.
Expand All @@ -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%#v\x1b[0m\n> result:\t%#v\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\n", successMark, expected, result)
}
return
}
Expand Down
35 changes: 32 additions & 3 deletions test/pokesay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ func TestUnicodeTokenise(test *testing.T) {
input: " ▄▄ ▄▄",
expected: [][]pokesay.ANSILineToken{
{
pokesay.ANSILineToken{FGColour: "", BGColour: "", Text: " ▄▄ ▄▄"},
pokesay.ANSILineToken{FGColour: "", BGColour: "", Text: " ", Reset: true},
pokesay.ANSILineToken{FGColour: "", BGColour: "", Text: "▄▄"},
pokesay.ANSILineToken{FGColour: "", BGColour: "", Text: " ", Reset: true},
pokesay.ANSILineToken{FGColour: "", BGColour: "", Text: "▄▄"},
},
},
},
Expand Down Expand Up @@ -182,7 +185,7 @@ func TestUnicodeReverse(test *testing.T) {
}
}

func TestANSIBasic(test *testing.T) {
func TestANSITokenise(test *testing.T) {
testCases := []struct {
name string
input string
Expand Down Expand Up @@ -217,6 +220,18 @@ func TestANSIBasic(test *testing.T) {
},
},
},
// purple fg, red bg
{
name: "Single line with spaces",
input: "\x1b[38;5;129mAAA \x1b[48;5;160m XX \x1b[0m",
expected: [][]pokesay.ANSILineToken{
{
pokesay.ANSILineToken{FGColour: "\x1b[38;5;129m", BGColour: "", Text: "AAA"},
pokesay.ANSILineToken{FGColour: "\x1b[38;5;129m", BGColour: "\x1b[48;5;160m", Text: "XX"},
pokesay.ANSILineToken{FGColour: "\x1b[0m", BGColour: "", Text: ""},
},
},
},
}
for _, tc := range testCases {
test.Run(tc.name, func(test *testing.T) {
Expand Down Expand Up @@ -255,7 +270,21 @@ func TestANSIWithSpaces(test *testing.T) {
},
},
{
name: "Lines with colour continuation (spaces)",
name: "Lines with FG continuation (spaces)",
// purple fg, red bg
// the 4 spaces after AAA should have a purple fg, and no bg
input: "\x1b[38;5;129mAAA \x1b[48;5;160m XX \x1b[0m",
// 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{FGColour: "\x1b[38;5;129m", BGColour: "", Text: "AAA ", Reset: true},
pokesay.ANSILineToken{FGColour: "\x1b[38;5;129m", BGColour: "\x1b[48;5;160m", Text: " XX ", Reset: true},
pokesay.ANSILineToken{FGColour: "\x1b[0m", BGColour: "", Text: ""},
},
},
},
{
name: "Lines with BG continuation (spaces)",
// purple fg, red bg
// the 4 spaces after AAA should have a purple fg, and no bg
input: "\x1b[38;5;129mAAA \x1b[48;5;160m XX \x1b[0m",
Expand Down

0 comments on commit c7ea9a2

Please sign in to comment.