Skip to content

Commit

Permalink
fix(ansi.Scanner): correction for 0 width control characters.
Browse files Browse the repository at this point in the history
  • Loading branch information
pachecot committed Oct 27, 2024
1 parent 6f1bdf9 commit 4a6a3ce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
15 changes: 13 additions & 2 deletions ansi/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,19 @@ func (s *Scanner) Scan() bool {
}
s.escape = false
}
if s.advance(1, 1) {
return true
switch c := s.b[s.end]; {
case c > US && c < DEL:
if s.advance(1, 1) {
return true
}
case c <= US || c == DEL || c < 0xC0:
if s.advance(1, 0) {
return true
}
default:
if s.advance(1, 1) {
return true
}
}
default:
if !s.escape {
Expand Down
30 changes: 15 additions & 15 deletions ansi/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestScannerLinesWords(t *testing.T) {
expected: []scanResult{
{true, 0, "\x1b[91m"},
{false, 3, "foo"},
{false, 1, "\n"},
{false, 0, "\n"},
{false, 3, "bar"},
{true, 0, "\x1b[0"},
},
Expand All @@ -116,7 +116,7 @@ func TestScannerLinesWords(t *testing.T) {
expected: []scanResult{
{true, 0, "\x1b[91m"},
{false, 3, "foo"},
{false, 1, "\r"},
{false, 0, "\r"},
{false, 3, "bar"},
{true, 0, "\x1b[0"},
},
Expand All @@ -127,7 +127,7 @@ func TestScannerLinesWords(t *testing.T) {
expected: []scanResult{
{true, 0, "\x1b[91m"},
{false, 3, "foo"},
{false, 2, "\r\n"},
{false, 0, "\r\n"},
{false, 3, "bar"},
{true, 0, "\x1b[0"},
},
Expand All @@ -138,7 +138,7 @@ func TestScannerLinesWords(t *testing.T) {
expected: []scanResult{
{true, 0, "\x1b[91m"},
{false, 3, "foo"},
{false, 4, "\r\r\r\n"},
{false, 0, "\r\r\r\n"},
{false, 3, "bar"},
{true, 0, "\x1b[0"},
},
Expand All @@ -150,7 +150,7 @@ func TestScannerLinesWords(t *testing.T) {
{true, 0, "\x1b[91m"},
{false, 3, "foo"},
{false, 1, " "},
{false, 2, "\r\n"},
{false, 0, "\r\n"},
{false, 1, " "},
{false, 3, "bar"},
{false, 1, " "},
Expand All @@ -163,9 +163,9 @@ func TestScannerLinesWords(t *testing.T) {
expected: []scanResult{
{true, 0, "\x1b[91m"},
{false, 3, "foo"},
{false, 1, "\n"},
{false, 1, "\n"},
{false, 2, "\r\n"},
{false, 0, "\n"},
{false, 0, "\n"},
{false, 0, "\r\n"},
{false, 3, "bar"},
{true, 0, "\x1b[0"},
},
Expand All @@ -175,10 +175,10 @@ func TestScannerLinesWords(t *testing.T) {
input: " \n\n \r\n ",
expected: []scanResult{
{false, 3, " "},
{false, 1, "\n"},
{false, 1, "\n"},
{false, 0, "\n"},
{false, 0, "\n"},
{false, 3, " "},
{false, 2, "\r\n"},
{false, 0, "\r\n"},
{false, 3, " "},
},
},
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestScannerLines(t *testing.T) {
expected: []scanResult{
{true, 0, "\x1b[91m"},
{false, 3, "foo"},
{false, 1, "\n"},
{false, 0, "\n"},
{false, 3, "bar"},
{true, 0, "\x1b[0"},
},
Expand All @@ -263,7 +263,7 @@ func TestScannerLines(t *testing.T) {
expected: []scanResult{
{true, 0, "\x1b[91m"},
{false, 4, "foo "},
{false, 2, "\r\n"},
{false, 0, "\r\n"},
{false, 5, " bar "},
{true, 0, "\x1b[0"},
},
Expand All @@ -274,7 +274,7 @@ func TestScannerLines(t *testing.T) {
expected: []scanResult{
{true, 0, "\x1b[91m"},
{false, 4, "foo "},
{false, 1, "\r"},
{false, 0, "\r"},
{false, 5, " bar "},
{true, 0, "\x1b[0"},
},
Expand Down Expand Up @@ -347,7 +347,7 @@ func TestScanner(t *testing.T) {
input: "\x1b[91mfoo \r\n bar \x1b[0",
expected: []scanResult{
{true, 0, "\x1b[91m"},
{false, 11, "foo \r\n bar "},
{false, 9, "foo \r\n bar "},
{true, 0, "\x1b[0"},
},
},
Expand Down

0 comments on commit 4a6a3ce

Please sign in to comment.