forked from buildkite/terminal-to-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.go
259 lines (226 loc) · 5.74 KB
/
screen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package terminal
import (
"bytes"
"math"
"strconv"
"strings"
)
// A terminal 'screen'. Current cursor position, cursor style, and characters
type screen struct {
x int
y int
screen []screenLine
style *style
}
type screenLine struct {
nodes []node
// metadata is { namespace => { key => value, ... }, ... }
// e.g. { "bk" => { "t" => "1234" } }
metadata map[string]map[string]string
}
const screenEndOfLine = -1
const screenStartOfLine = 0
// Clear part (or all) of a line on the screen
func (s *screen) clear(y int, xStart int, xEnd int) {
if len(s.screen) <= y {
return
}
line := s.screen[y]
if xStart == screenStartOfLine && xEnd == screenEndOfLine {
// Blank the entire line, but keep any existing line metadata.
s.screen[y].nodes = make([]node, 0, 80)
} else {
if xEnd == screenEndOfLine {
xEnd = len(line.nodes) - 1
}
// TODO: optimise clear-to-end-of-line by truncating line.nodes?
for i := xStart; i <= xEnd && i < len(line.nodes); i++ {
line.nodes[i] = emptyNode
}
}
}
// "Safe" parseint for parsing ANSI instructions
func ansiInt(s string) int {
if s == "" {
return 1
}
i, _ := strconv.ParseInt(s, 10, 8)
return int(i)
}
// Move the cursor up, if we can
func (s *screen) up(i string) {
s.y -= ansiInt(i)
s.y = int(math.Max(0, float64(s.y)))
}
// Move the cursor down
func (s *screen) down(i string) {
s.y += ansiInt(i)
}
// Move the cursor forward on the line
func (s *screen) forward(i string) {
s.x += ansiInt(i)
}
// Move the cursor backward, if we can
func (s *screen) backward(i string) {
s.x -= ansiInt(i)
s.x = int(math.Max(0, float64(s.x)))
}
func (s *screen) getCurrentLineForWriting() *screenLine {
// Add rows to our screen if necessary
for i := len(s.screen); i <= s.y; i++ {
s.screen = append(s.screen, screenLine{nodes: make([]node, 0, 80)})
}
line := &s.screen[s.y]
// Add columns if currently shorter than the cursor's x position
for i := len(line.nodes); i <= s.x; i++ {
line.nodes = append(line.nodes, emptyNode)
}
return line
}
// Write a character to the screen's current X&Y, along with the current screen style
func (s *screen) write(data rune) {
line := s.getCurrentLineForWriting()
line.nodes[s.x] = node{blob: data, style: s.style}
}
// Append a character to the screen
func (s *screen) append(data rune) {
s.write(data)
s.x++
}
// Append multiple characters to the screen
func (s *screen) appendMany(data []rune) {
for _, char := range data {
s.append(char)
}
}
func (s *screen) appendElement(i *element) {
line := s.getCurrentLineForWriting()
line.nodes[s.x] = node{style: s.style, elem: i}
s.x++
}
// Set non-existing line metadata. Merges the provided data into any existing
// metadata for the current line, keeping existing data when keys collide.
func (s *screen) setnxLineMetadata(namespace string, data map[string]string) {
line := s.getCurrentLineForWriting()
if line.metadata == nil {
line.metadata = make(map[string]map[string]string)
}
if ns, nsExists := line.metadata[namespace]; nsExists {
// set keys that don't already exist
for k, v := range data {
if _, kExists := ns[k]; !kExists {
ns[k] = v
}
}
} else {
// namespace did not exist, set all data
line.metadata[namespace] = data
}
}
// Apply color instruction codes to the screen's current style
func (s *screen) color(i []string) {
s.style = s.style.color(i)
}
// Apply an escape sequence to the screen
func (s *screen) applyEscape(code rune, instructions []string) {
if len(instructions) == 0 {
// Ensure we always have a first instruction
instructions = []string{""}
}
switch code {
case 'M':
s.color(instructions)
case 'G':
s.x = 0
// "Erase in Display"
case 'J':
switch instructions[0] {
// "erase from current position to end (inclusive)"
case "0", "":
// This line should be equivalent to K0
s.clear(s.y, s.x, screenEndOfLine)
// Truncate the screen below the current line
if len(s.screen) > s.y {
s.screen = s.screen[:s.y+1]
}
// "erase from beginning to current position (inclusive)"
case "1":
// This line should be equivalent to K1
s.clear(s.y, screenStartOfLine, s.x)
// Truncate the screen above the current line
if len(s.screen) > s.y {
s.screen = s.screen[s.y+1:]
}
// Adjust the cursor position to compensate
s.y = 0
// 2: "erase entire display", 3: "erase whole display including scroll-back buffer"
// Given we don't have a scrollback of our own, we treat these as equivalent
case "2", "3":
s.screen = nil
s.x = 0
s.y = 0
}
// "Erase in Line"
case 'K':
switch instructions[0] {
case "0", "":
s.clear(s.y, s.x, screenEndOfLine)
case "1":
s.clear(s.y, screenStartOfLine, s.x)
case "2":
s.clear(s.y, screenStartOfLine, screenEndOfLine)
}
case 'A':
s.up(instructions[0])
case 'B':
s.down(instructions[0])
case 'C':
s.forward(instructions[0])
case 'D':
s.backward(instructions[0])
}
}
// Parse ANSI input, populate our screen buffer with nodes
func (s *screen) parse(ansi []byte) {
s.style = &emptyStyle
parseANSIToScreen(s, ansi)
}
func (s *screen) asHTML() []byte {
var lines []string
for _, line := range s.screen {
lines = append(lines, outputLineAsHTML(line))
}
return []byte(strings.Join(lines, "\n"))
}
// asPlainText renders the screen without any ANSI style etc.
func (s *screen) asPlainText() string {
var buf bytes.Buffer
for i, line := range s.screen {
for _, node := range line.nodes {
if node.elem == nil {
buf.WriteRune(node.blob)
}
}
if i < len(s.screen)-1 {
buf.WriteRune('\n')
}
}
return strings.TrimRight(buf.String(), " \t")
}
func (s *screen) newLine() {
s.x = 0
s.y++
}
func (s *screen) revNewLine() {
if s.y > 0 {
s.y--
}
}
func (s *screen) carriageReturn() {
s.x = 0
}
func (s *screen) backspace() {
if s.x > 0 {
s.x--
}
}