-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathto_utf8.go
276 lines (203 loc) · 6.12 KB
/
to_utf8.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package ansiart2utf8
import (
"bufio"
"fmt"
"io"
"strings"
)
// TRANSLATION ARRAY
var Array437 [256]rune = [256]rune{
'\x00', '☺', '☻', '♥', '♦', '♣', '♠', '•', '◘', '○', '◙', '♂', '♀', '♪', '♫', '☼',
'►', '◄', '↕', '‼', '¶', '§', '▬', '↨', '↑', '↓', '→', '←', '∟', '↔', '▲', '▼',
' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '⌂',
'Ç', 'ü', 'é', 'â', 'ä', 'à', 'å', 'ç', 'ê', 'ë', 'è', 'ï', 'î', 'ì', 'Ä', 'Å',
'É', 'æ', 'Æ', 'ô', 'ö', 'ò', 'û', 'ù', 'ÿ', 'Ö', 'Ü', '¢', '£', '¥', '₧', 'ƒ',
'á', 'í', 'ó', 'ú', 'ñ', 'Ñ', 'ª', 'º', '¿', '⌐', '¬', '½', '¼', '¡', '«', '»',
'░', '▒', '▓', '│', '┤', '╡', '╢', '╖', '╕', '╣', '║', '╗', '╝', '╜', '╛', '┐',
'└', '┴', '┬', '├', '─', '┼', '╞', '╟', '╚', '╔', '╩', '╦', '╠', '═', '╬', '╧',
'╨', '╤', '╥', '╙', '╘', '╒', '╓', '╫', '╪', '┘', '┌', '█', '▄', '▌', '▐', '▀',
'α', 'ß', 'Γ', 'π', 'Σ', 'σ', 'µ', 'τ', 'Φ', 'Θ', 'Ω', 'δ', '∞', 'φ', 'ε', '∩',
'≡', '±', '≥', '≤', '⌠', '⌡', '÷', '≈', '°', '∙', '·', '√', 'ⁿ', '²', '■', '\u00a0',
}
const (
CHR_ESCAPE = 0x1B
CHR_CR = 0x0D
CHR_LF = 0x0A
)
type DebugFunc func(...interface{}) (int, error)
type UTF8Marshaller struct {
Width uint
MaxBytes uint
Translate2Xterm256 bool
FakeEsc bool
Debug DebugFunc
Writer io.Writer
}
/*
ENCODES ANSI ART TO MODERN UTF8 TERMINAL CHARS
PRE-RENDERS TO MEMORY (MOTION ESCAPES, COLOR CHANGES, ETC)
WRITES OUTPUT, LINE-BY-LINE, TO .Writer
*/
func (M UTF8Marshaller) Encode(rdAnsi io.Reader) (E error) {
ixByte := -1
defer func() {
if E != nil {
E = fmt.Errorf("%s, at index %d", E.Error(), ixByte)
}
}()
pRdr := bufio.NewReader(rdAnsi)
pGrid, E := NewGrid(M.Width)
if E != nil {
return
}
bEsc := false
escCur := EscCode{}
sgrCur := SGR{}
sgrSaved := SGR{}
posCur := NewPos()
posSaved := NewPos()
// NO-OP
fnDebug := func(v ...interface{}) (int, error) {
if M.Debug != nil {
v = append(v, fmt.Sprintf("at index %d", ixByte))
return M.Debug(v...)
}
return 0, nil
}
CharLoop:
for true {
// TODO: try to extract dims from sauce
chr, e := pRdr.ReadByte()
if e == io.EOF {
break
} else if e != nil {
E = e
return
}
ixByte += 1
switch chr {
// TODO: break at ^ZSAUCE00 (^Z is 26 dec, 0x1A hex)
// STOP ON NULL & SAUCE
case 0, 26:
break CharLoop
}
if chr == CHR_CR {
posCur.X = 1
continue
} else if chr == CHR_LF {
// EXTEND ROW
posCur.Y += 1
pGrid.Touch(posCur.Y)
continue
} else if chr == CHR_ESCAPE {
// BEGIN ESCAPE CODE
bEsc = true
escCur.Reset()
continue
// HANDLE ESCAPE CODE SEQUENCE
} else if bEsc {
// ESCAPE CODE TERMINATING CHARS:
if strings.IndexByte(SGR_TERMINATORS, chr) == -1 {
// APPEND COMPONENT OF ESCAPE SEQUENCE
escCur.Params += string(chr)
} else {
// EXIT ESCAPE CODE FSM SUCCESSFULLY ON TERMINATING 'm' CHARACTER
bEsc = false
escCur.Code = rune(chr)
if escCur.Validate() {
// ONLY RESTORE SGR ESCAPE CODES
switch escCur.Code {
case 'm':
if e2 := sgrCur.MergeCodes(escCur.SubParams); e2 != nil {
E = fmt.Errorf("SGR ERROR %s", e2.Error())
return
}
// UP
case 'A':
pGrid.IncClamp(&posCur, 0, -int(escCur.SubParams[0]))
// DOWN
case 'B':
pGrid.IncClamp(&posCur, 0, int(escCur.SubParams[0]))
// FORWARD
case 'C':
pGrid.IncClamp(&posCur, int(escCur.SubParams[0]), 0)
// pGrid.Touch(posCur.Y)
// BACK
case 'D':
pGrid.IncClamp(&posCur, -int(escCur.SubParams[0]), 0)
// NOTE: NOT ANSI.SYS
case 'E', 'F', 'G':
// E: beginning on line, n lines down
// F: beginning on line, n lines up
// G: cursor to column n
// TO X,Y
case 'H', 'f':
posCur.Y = int(escCur.SubParams[0])
posCur.X = int(escCur.SubParams[1])
pGrid.Touch(posCur.Y)
case 'J':
switch escCur.SubParams[0] {
// clear from cursor to end of screen
case 0:
pGrid.ClearFromPosToEnd(posCur)
// clear from cursor to beginning of screen
case 1:
pGrid.ClearFromPosToBegin(posCur)
// clear entire screen, move cursor to upper-left
case 2:
posCur.X, posCur.Y = 1, 1
pGrid.ClearFromPosToEnd(posCur)
// clear entire screen, reset scrollback buffer
case 3:
pGrid.ClearFromPosToEnd(GridPos{1, 1})
}
case 'K':
switch escCur.SubParams[0] {
// clear from cursor to end of line
case 0:
pGrid.ClearLine(posCur, false)
// clear from cursor to beginning of line
case 1:
pGrid.ClearLine(posCur, true)
// clear entire line
case 2:
pGrid.ClearLine(GridPos{X: 1, Y: posCur.Y}, false)
}
// NO-OP: NOT ANSI.SYS
case 'S', 'T':
// S: scroll page up by n lines
// T: scroll page down by n lines
// SAVE CURSOR POS & SGR
case 's':
posSaved = posCur
sgrSaved = sgrCur
// RESTORE CURSOR POS & SGR
case 'u':
posCur = posSaved
sgrCur = sgrSaved
default:
E = fmt.Errorf("UNHANDLED CODE %s", escCur.Debug())
return
}
} else {
fnDebug("INVALID CODE: ", escCur.Debug())
}
continue
}
}
// HANDLE WRITABLE CHARACTERS OUTSIDE OF ESCAPE MODE
if !bEsc {
if e2 := pGrid.Put(posCur, Array437[chr], sgrCur); e2 != nil {
fnDebug(e2)
}
pGrid.Inc(&posCur, 1)
}
}
pGrid.Print(M.Writer, int(M.MaxBytes), M.Debug != nil, M.Translate2Xterm256, M.FakeEsc)
return
}