-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcontrol.go
196 lines (167 loc) · 4.7 KB
/
control.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
// Package dev provides access to multiple devices.
package dev
import (
"bytes"
"unicode"
)
const ctrlG = 'G' - '@'
func removeControlChars(logger hasPrintf, debug bool, buf, suffix []byte) ([]byte, []byte) {
for i := 0; i < len(suffix); i++ {
b := suffix[i]
switch {
case b == LF: // do nothing, otherwise it would be killed as other control
case b == CR:
next := i + 1
if next < len(suffix) {
if suffix[next] == 0 {
suffix[next] = LF // handle CR NULL as CR LF
}
if suffix[next] == LF {
continue
}
}
// sole CR: perform carriage return
j := bytes.LastIndexByte(suffix[:i], LF)
if j < 0 {
// suffix: previous LF not found: search on buf
j = bytes.LastIndexByte(buf, LF)
if j < 0 {
// buf: previous LF not found: shift all
buf = buf[:0]
} else {
// buf: previous LF found: shift
buf = buf[:j]
}
suffix = suffix[next:] // shift suffix
i = -1 // handle suffix from start
continue
}
// suffix: previous LF found: shift over it
if j > 0 {
// LF is not first char on suffix: remove possible CR from suffix
if suffix[j-1] == CR {
j-- // kill CR LF from suffix
}
} else {
// LF is first char on suffix: remove possible CR from buf
bufSize := len(buf)
if bufSize > 0 {
last := bufSize - 1
if buf[last] == CR {
buf = buf[:last] // kill CR (last byte) from buf
}
}
}
suffix = append(suffix[:j], suffix[i+1:]...) // shift over previous [CR] LF
i = j - 1 // handle j again
case b == BS:
// perform backspace: remove two chars
if i > 0 {
// remove X,BS from suffix
suffix = append(suffix[:i-1], suffix[i+1:]...) // cut bytes at i-1 and i
i -= 2 // handle i again
continue
}
// remove X from buf, BS from suffix
if len(buf) > 0 {
buf = buf[:len(buf)-1]
}
suffix = suffix[1:]
i = -1 // handle prefix from start
case b == 27:
if j := i + 1; j < len(suffix) {
switch suffix[j] {
case '[':
if size, ok := prefixNumberM(suffix[j+1:]); ok {
// remove N control chars: ESC [ d d d m
// i j <----->
// size
// k
k := j + size
suffix = append(suffix[:i], suffix[k+1:]...) // cut bytes i..k
i-- // handle i again
continue
}
if k := j + 1; k < len(suffix) {
switch suffix[k] {
case 'A', 'B', 'C', 'D', 'J', 'K':
// remove 3 control chars: ESC [ x
suffix = append(suffix[:i], suffix[k+1:]...) // cut bytes i..k
i-- // handle i again
case '1', '3', '4':
if l := k + 1; l < len(suffix) {
switch suffix[l] {
case '~':
// remove 4 control chars: ESC [ x y
suffix = append(suffix[:i], suffix[k+1:]...) // cut bytes i..k
i-- // handle i again
default:
logger.Printf("unknown 4-char escape: %q", suffix[i:])
}
}
default:
logger.Printf("unknown 3-char escape: %q", suffix[i:])
}
}
case ']':
if k := j + 1; k < len(suffix) {
switch suffix[k] {
case '0':
if size, ok := prefixTitle(suffix[k+1:]); ok {
// remove N control chars: ESC ] 0 ; string ^G
// i j k <--------->
// size
// l
l := k + size
suffix = append(suffix[:i], suffix[l+1:]...) // cut bytes i..l
i-- // handle i again
continue
}
default:
logger.Printf("unknown 3-char escape: %q", suffix[i:])
}
}
default:
logger.Printf("unknown 2-char escape: %q", suffix[i:])
}
}
case b < 32: // other control
// remove the single control char
suffix = append(suffix[:i], suffix[i+1:]...) // cut byte at i
i-- // handle i again
}
}
return buf, suffix
}
// ; string ^G
func prefixTitle(s []byte) (int, bool) {
if len(s) < 2 {
return 0, false
}
if s[0] != ';' {
return 0, false
}
g := bytes.IndexByte(s, ctrlG)
if g < 1 {
return 0, false
}
return g + 1, true
}
// d ... d m
func prefixNumberM(s []byte) (int, bool) {
foundDigit := false
for i, c := range s {
switch {
case c == 'm':
if foundDigit {
return i + 1, true
}
return 0, false
case unicode.IsDigit(rune(c)):
foundDigit = true
default:
return 0, false
}
}
return 0, false
}