Skip to content

Commit

Permalink
fix(term): ansi: sequences string representations
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Apr 8, 2024
1 parent 0100b8b commit 525ba71
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 44 deletions.
3 changes: 0 additions & 3 deletions exp/term/ansi/osc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ansi

import (
"bytes"
"strconv"
"strings"
)

Expand Down Expand Up @@ -64,8 +63,6 @@ func (s OscSequence) Bytes() []byte {
func (s OscSequence) buffer() *bytes.Buffer {
var b bytes.Buffer
b.WriteString("\x1b]")
b.WriteString(strconv.Itoa(s.Cmd))
b.WriteByte(';')
b.Write(s.Data)
b.WriteByte(BEL)
return &b
Expand Down
13 changes: 3 additions & 10 deletions exp/term/ansi/osc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,16 @@ func TestOscSequence_String(t *testing.T) {
{
name: "empty",
s: OscSequence{
Cmd: 0,
Cmd: 0,
Data: []byte("0;"),
},
want: "\x1b]0;\a",
},
{
name: "with data",
s: OscSequence{
Cmd: 1,
Data: []byte("hello"),
},
want: "\x1b]1;hello\x07",
},
{
name: "with data and command",
s: OscSequence{
Cmd: 1,
Data: []byte("hello"),
Data: []byte("1;hello"),
},
want: "\x1b]1;hello\x07",
},
Expand Down
6 changes: 3 additions & 3 deletions exp/term/ansi/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,11 @@ func (p *Parser) performAction(dispatcher ParserDispatcher, action parser.Action
case parser.OscStringState:
seq = OscSequence{Cmd: p.Cmd, Data: data}
case parser.SosStringState:
seq = SosSequence(data)
seq = SosSequence{Data: data}
case parser.PmStringState:
seq = PmSequence(data)
seq = PmSequence{Data: data}
case parser.ApcStringState:
seq = ApcSequence(data)
seq = ApcSequence{Data: data}
}

dispatcher(seq)
Expand Down
2 changes: 1 addition & 1 deletion exp/term/ansi/parser_apc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestSosPmApcSequence(t *testing.T) {
name: "apc7",
input: "\x1b_Gf=24,s=10,v=20,o=z;aGVsbG8gd29ybGQ=\x1b\\",
expected: []Sequence{
ApcSequence("Gf=24,s=10,v=20,o=z;aGVsbG8gd29ybGQ="),
ApcSequence{Data: []byte("Gf=24,s=10,v=20,o=z;aGVsbG8gd29ybGQ=")},
EscSequence('\\'),
},
},
Expand Down
60 changes: 36 additions & 24 deletions exp/term/ansi/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,16 @@ func (e EscSequence) Intermediate() int {
}

// SosSequence represents a SOS sequence.
type SosSequence []byte
type SosSequence struct {
// Data contains the raw data of the sequence.
Data []byte
}

var _ Sequence = SosSequence(nil)
var _ Sequence = &SosSequence{}

// Clone implements Sequence.
func (s SosSequence) Clone() Sequence {
return append(SosSequence(nil), s...)
return SosSequence{Data: append([]byte(nil), s.Data...)}
}

// Bytes implements Sequence.
Expand All @@ -124,64 +127,73 @@ func (s SosSequence) buffer() *bytes.Buffer {
var b bytes.Buffer
b.WriteByte('\x1b')
b.WriteByte('X')
b.Write([]byte(s))
b.Write(s.Data)
b.WriteString("\x1b\\")
return &b
}

// PmSequence represents a PM sequence.
type PmSequence []byte
type PmSequence struct {
// Data contains the raw data of the sequence.
Data []byte
}

var _ Sequence = PmSequence(nil)
var _ Sequence = &PmSequence{}

// Clone implements Sequence.
func (p PmSequence) Clone() Sequence {
return append(PmSequence(nil), p...)
func (s PmSequence) Clone() Sequence {
return PmSequence{Data: append([]byte(nil), s.Data...)}
}

// Bytes implements Sequence.
func (p PmSequence) Bytes() []byte {
return p.buffer().Bytes()
func (s PmSequence) Bytes() []byte {
return s.buffer().Bytes()
}

// String implements Sequence.
func (p PmSequence) String() string {
return p.buffer().String()
func (s PmSequence) String() string {
return s.buffer().String()
}

// buffer returns the buffer of the PM sequence.
func (p PmSequence) buffer() *bytes.Buffer {
func (s PmSequence) buffer() *bytes.Buffer {
var b bytes.Buffer
b.WriteByte('\x1b')
b.WriteByte('^')
b.Write([]byte(p))
b.Write(s.Data)
b.WriteString("\x1b\\")
return &b
}

// ApcSequence represents an APC sequence.
type ApcSequence []byte
type ApcSequence struct {
// Data contains the raw data of the sequence.
Data []byte
}

var _ Sequence = ApcSequence(nil)
var _ Sequence = &ApcSequence{}

// Clone implements Sequence.
func (a ApcSequence) Clone() Sequence {
return append(ApcSequence(nil), a...)
func (s ApcSequence) Clone() Sequence {
return ApcSequence{Data: append([]byte(nil), s.Data...)}
}

// Bytes implements Sequence.
func (a ApcSequence) Bytes() []byte {
return a.buffer().Bytes()
func (s ApcSequence) Bytes() []byte {
return s.buffer().Bytes()
}

// String implements Sequence.
func (a ApcSequence) String() string {
return a.buffer().String()
func (s ApcSequence) String() string {
return s.buffer().String()
}

// buffer returns the buffer of the APC sequence.
func (a ApcSequence) buffer() *bytes.Buffer {
func (s ApcSequence) buffer() *bytes.Buffer {
var b bytes.Buffer
b.WriteByte('\x1b')
b.WriteByte('_')
b.Write([]byte(a))
b.Write(s.Data)
b.WriteString("\x1b\\")
return &b
}
6 changes: 3 additions & 3 deletions exp/term/examples/parserlog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ func main() {
}
fmt.Printf("Data=%q\n", s.Data)
case ansi.SosSequence:
fmt.Printf("[SosSequence] Data=%q\n", []byte(s))
fmt.Printf("[SosSequence] Data=%q\n", s.Data)
case ansi.PmSequence:
fmt.Printf("[PmSequence] Data=%q\n", []byte(s))
fmt.Printf("[PmSequence] Data=%q\n", s.Data)
case ansi.ApcSequence:
fmt.Printf("[ApcSequence] Data=%q\n", []byte(s))
fmt.Printf("[ApcSequence] Data=%q\n", s.Data)
}
}

Expand Down

0 comments on commit 525ba71

Please sign in to comment.