Skip to content

Commit

Permalink
fix(ansi): ensure OSC command number is parsed when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Oct 29, 2024
1 parent 08d1601 commit e01e068
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
47 changes: 35 additions & 12 deletions ansi/parser_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,31 @@ func DecodeSequence[T string | []byte](b T, state byte, p *Parser) (seq T, width
switch c {
case BEL:
if HasOscPrefix(b) {
parseOscCmd(p)
return b[:i+1], 0, i + 1, NormalState
}
case CAN, SUB:
if HasOscPrefix(b) {
// Ensure we parse the OSC command number
parseOscCmd(p)
}

// Cancel the sequence
return b[:i], 0, i, NormalState
case ST:
if HasOscPrefix(b) {
// Ensure we parse the OSC command number
parseOscCmd(p)
}

return b[:i+1], 0, i + 1, NormalState
case ESC:
if HasStPrefix(b[i:]) {
if HasOscPrefix(b) {
// Ensure we parse the OSC command number
parseOscCmd(p)
}

// End of string 7-bit (ST)
return b[:i+2], 0, i + 2, NormalState
}
Expand All @@ -270,18 +286,8 @@ func DecodeSequence[T string | []byte](b T, state byte, p *Parser) (seq T, width
p.DataLen++

// Parse the OSC command number
if c == ';' && p.Cmd == parser.MissingCommand && HasOscPrefix(b) {
for j := 0; j < p.DataLen; j++ {
d := p.Data[j]
if d < '0' || d > '9' {
break
}
if p.Cmd == parser.MissingCommand {
p.Cmd = 0
}
p.Cmd *= 10
p.Cmd += int(d - '0')
}
if c == ';' && HasOscPrefix(b) {
parseOscCmd(p)
}
}
}
Expand All @@ -290,6 +296,23 @@ func DecodeSequence[T string | []byte](b T, state byte, p *Parser) (seq T, width
return b, 0, len(b), state
}

func parseOscCmd(p *Parser) {
if p == nil || p.Cmd != parser.MissingCommand {
return
}
for j := 0; j < p.DataLen; j++ {
d := p.Data[j]
if d < '0' || d > '9' {
break
}
if p.Cmd == parser.MissingCommand {
p.Cmd = 0
}
p.Cmd *= 10
p.Cmd += int(d - '0')
}
}

// Index returns the index of the first occurrence of the given byte slice in
// the data. It returns -1 if the byte slice is not found.
func Index[T string | []byte](data, b T) int {
Expand Down
7 changes: 7 additions & 0 deletions ansi/parser_decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ func TestDecodeSequence(t *testing.T) {
{seq: []byte{'b'}, n: 1, width: 1},
},
},
{
name: "single param osc",
input: []byte("\x1b]112\x07"),
expected: []expectedSequence{
{seq: []byte("\x1b]112\x07"), n: 6, cmd: 112, data: []byte("112")},
},
},
}

for _, tc := range cases {
Expand Down

0 comments on commit e01e068

Please sign in to comment.