vendor/github.com/charmbracelet/x/ansi/sequence.go
199 lines · 4307 bytes
1package ansi
2
3import (
4 "bytes"
5
6 "github.com/charmbracelet/x/ansi/parser"
7)
8
9// Sequence represents an ANSI sequence. This can be a control sequence, escape
10// sequence, a printable character, etc.
11type Sequence interface {
12 // String returns the string representation of the sequence.
13 String() string
14 // Bytes returns the byte representation of the sequence.
15 Bytes() []byte
16 // Clone returns a copy of the sequence.
17 Clone() Sequence
18}
19
20// Rune represents a printable character.
21type Rune rune
22
23var _ Sequence = Rune(0)
24
25// Bytes implements Sequence.
26func (r Rune) Bytes() []byte {
27 return []byte(string(r))
28}
29
30// String implements Sequence.
31func (r Rune) String() string {
32 return string(r)
33}
34
35// Clone implements Sequence.
36func (r Rune) Clone() Sequence {
37 return r
38}
39
40// ControlCode represents a control code character. This is a character that
41// is not printable and is used to control the terminal. This would be a
42// character in the C0 or C1 set in the range of 0x00-0x1F and 0x80-0x9F.
43type ControlCode byte
44
45var _ Sequence = ControlCode(0)
46
47// Bytes implements Sequence.
48func (c ControlCode) Bytes() []byte {
49 return []byte{byte(c)}
50}
51
52// String implements Sequence.
53func (c ControlCode) String() string {
54 return string(c)
55}
56
57// Clone implements Sequence.
58func (c ControlCode) Clone() Sequence {
59 return c
60}
61
62// EscSequence represents an escape sequence.
63type EscSequence int
64
65var _ Sequence = EscSequence(0)
66
67// buffer returns the buffer of the escape sequence.
68func (e EscSequence) buffer() *bytes.Buffer {
69 var b bytes.Buffer
70 b.WriteByte('\x1b')
71 if i := parser.Intermediate(int(e)); i != 0 {
72 b.WriteByte(byte(i))
73 }
74 b.WriteByte(byte(e.Command()))
75 return &b
76}
77
78// Bytes implements Sequence.
79func (e EscSequence) Bytes() []byte {
80 return e.buffer().Bytes()
81}
82
83// String implements Sequence.
84func (e EscSequence) String() string {
85 return e.buffer().String()
86}
87
88// Clone implements Sequence.
89func (e EscSequence) Clone() Sequence {
90 return e
91}
92
93// Command returns the command byte of the escape sequence.
94func (e EscSequence) Command() int {
95 return parser.Command(int(e))
96}
97
98// Intermediate returns the intermediate byte of the escape sequence.
99func (e EscSequence) Intermediate() int {
100 return parser.Intermediate(int(e))
101}
102
103// SosSequence represents a SOS sequence.
104type SosSequence struct {
105 // Data contains the raw data of the sequence.
106 Data []byte
107}
108
109var _ Sequence = &SosSequence{}
110
111// Clone implements Sequence.
112func (s SosSequence) Clone() Sequence {
113 return SosSequence{Data: append([]byte(nil), s.Data...)}
114}
115
116// Bytes implements Sequence.
117func (s SosSequence) Bytes() []byte {
118 return s.buffer().Bytes()
119}
120
121// String implements Sequence.
122func (s SosSequence) String() string {
123 return s.buffer().String()
124}
125
126func (s SosSequence) buffer() *bytes.Buffer {
127 var b bytes.Buffer
128 b.WriteByte('\x1b')
129 b.WriteByte('X')
130 b.Write(s.Data)
131 b.WriteString("\x1b\\")
132 return &b
133}
134
135// PmSequence represents a PM sequence.
136type PmSequence struct {
137 // Data contains the raw data of the sequence.
138 Data []byte
139}
140
141var _ Sequence = &PmSequence{}
142
143// Clone implements Sequence.
144func (s PmSequence) Clone() Sequence {
145 return PmSequence{Data: append([]byte(nil), s.Data...)}
146}
147
148// Bytes implements Sequence.
149func (s PmSequence) Bytes() []byte {
150 return s.buffer().Bytes()
151}
152
153// String implements Sequence.
154func (s PmSequence) String() string {
155 return s.buffer().String()
156}
157
158// buffer returns the buffer of the PM sequence.
159func (s PmSequence) buffer() *bytes.Buffer {
160 var b bytes.Buffer
161 b.WriteByte('\x1b')
162 b.WriteByte('^')
163 b.Write(s.Data)
164 b.WriteString("\x1b\\")
165 return &b
166}
167
168// ApcSequence represents an APC sequence.
169type ApcSequence struct {
170 // Data contains the raw data of the sequence.
171 Data []byte
172}
173
174var _ Sequence = &ApcSequence{}
175
176// Clone implements Sequence.
177func (s ApcSequence) Clone() Sequence {
178 return ApcSequence{Data: append([]byte(nil), s.Data...)}
179}
180
181// Bytes implements Sequence.
182func (s ApcSequence) Bytes() []byte {
183 return s.buffer().Bytes()
184}
185
186// String implements Sequence.
187func (s ApcSequence) String() string {
188 return s.buffer().String()
189}
190
191// buffer returns the buffer of the APC sequence.
192func (s ApcSequence) buffer() *bytes.Buffer {
193 var b bytes.Buffer
194 b.WriteByte('\x1b')
195 b.WriteByte('_')
196 b.Write(s.Data)
197 b.WriteString("\x1b\\")
198 return &b
199}