-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
376 lines (352 loc) · 8.69 KB
/
process.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package process
import (
"bytes"
"errors"
"slices"
"strings"
"unicode/utf8"
)
type ANSITable struct {
Sub *ANSITable
Data []byte
Bound [2]int // rune index
}
// split ansi table given index(bounds), left: [:index] right: [index:]
func (a *ANSITable) Split(index int) (*ANSITable, *ANSITable) {
if index <= a.Bound[0] {
return nil, a
}
if index >= a.Bound[1] {
return a, nil
}
left := a
_r := *a
right := &_r
left.Bound[1] = index
right.Bound[0] = index
if left.Sub != nil {
left.Sub, right.Sub = left.Sub.Split(index)
}
return left, right
}
func (a *ANSITable) AddStyle(data []byte, boundLeft int) {
if a.Bound[1] < boundLeft {
panic(errors.New("bound right bigger than current table"))
}
if a.Bound[0] > boundLeft {
boundLeft = a.Bound[0]
}
if a.Sub == nil {
a.Sub = &ANSITable{
Data: data,
Bound: [2]int{boundLeft, a.Bound[1]},
}
} else {
if a.Sub.Bound[0] <= boundLeft {
a.Sub.AddStyle(data, boundLeft)
} else {
a.Sub = &ANSITable{
Data: data,
Bound: [2]int{boundLeft, a.Bound[1]},
Sub: a.Sub,
}
}
}
}
// implement `BoundsStruct` for search
func (a *ANSITable) GetBounds() [2]int {
return a.Bound
}
type ANSITableList struct {
L []BoundsStruct
}
var EMPTY_ANSITABLELIST = make([]BoundsStruct, 0)
// get a slice of ansi table, it will find all tables between `startIndex` and `endIndex`
func (a *ANSITableList) GetSlice(startIndex, endIndex int) []BoundsStruct {
if len(a.L) == 0 {
return a.L
}
var start, end int
temp := Search(a.L, startIndex)
// len == 1 means index within a specific table
// len == 2 means index between two tables, and for `startIndex` we only need the tables after `startIndex`
// temp[1] == -1 means already at the front of tablelist and no matchs
if len(temp) == 1 {
start = temp[0]
} else if len(temp) == 2 {
if temp[1] == -1 {
return EMPTY_ANSITABLELIST
} else {
start = temp[1]
}
}
temp = Search(a.L, endIndex)
// len == 1 means index within a specific table
// len == 2 means index between two tables, and for `endIndex` we only need the tables before `endIndex`
// temp[1] == -1 means already at the front of tablelist and no matchs
if len(temp) == 1 {
end = temp[0]
} else if len(temp) == 2 {
if temp[0] == -1 {
return EMPTY_ANSITABLELIST
} else {
end = temp[0]
}
}
// get slice of tablelist between start and end
return a.L[start : end+1]
}
func (a *ANSITableList) SetStyle(style []byte, startIndex, endIndex int) {
if len(a.L) == 0 {
var t BoundsStruct = &ANSITable{
Sub: nil,
Data: style,
Bound: [2]int{startIndex, endIndex},
}
a.L = slices.Insert(a.L, 0, t)
return
}
var start, end int
temp := Search(a.L, startIndex)
if len(temp) == 1 {
start = temp[0]
} else if len(temp) == 2 {
if temp[1] == -1 {
var t BoundsStruct = &ANSITable{
Sub: nil,
Data: style,
// Bound: [2]int{a.L[temp[0]].GetBounds()[1], endIndex},
Bound: [2]int{startIndex, endIndex},
}
a.L = slices.Insert(a.L, len(a.L), t)
return
} else {
t := &ANSITable{
Sub: nil,
Data: style,
}
t.Bound[1] = a.L[temp[1]].GetBounds()[0]
if temp[0] != -1 {
t.Bound[0] = a.L[temp[0]].GetBounds()[1]
}
var tt BoundsStruct = t
a.L = slices.Insert(a.L, temp[1], tt)
start = temp[1] + 1
}
}
temp = Search(a.L, endIndex)
if len(temp) == 1 {
end = temp[0]
t := a.L[end].(*ANSITable)
// if endIndex bigger than bound[1], split
if endIndex < t.Bound[1] {
left, right := t.Split(endIndex + 1)
if right != nil {
var r BoundsStruct = right
a.L = slices.Insert(a.L, end+1, r)
a.L[end] = left
}
}
} else if len(temp) == 2 {
if temp[0] == -1 {
var t BoundsStruct = &ANSITable{
Sub: nil,
Data: style,
// Bound: [2]int{startIndex, a.L[temp[1]].GetBounds()[0]},
Bound: [2]int{startIndex, endIndex},
}
a.L = slices.Insert(a.L, 0, t)
return
} else {
t := &ANSITable{
Sub: nil,
Data: style,
}
var tt BoundsStruct = t
t.Bound[0] = a.L[temp[0]].GetBounds()[1]
if temp[1] != -1 {
t.Bound[1] = a.L[temp[1]].GetBounds()[0]
a.L = slices.Insert(a.L, temp[1], tt)
} else {
t.Bound[1] = endIndex
a.L = slices.Insert(a.L, len(a.L), tt)
}
end = temp[0]
}
}
if start == end {
a.L[start].(*ANSITable).AddStyle(style, startIndex)
} else if start < end {
length := end + 1 - start
index := start
var last *ANSITable
for i := 0; i < length; i++ {
at := a.L[index].(*ANSITable)
at.AddStyle(style, startIndex)
if last != nil {
lastEnd := last.Bound[1]
thisStart := at.Bound[0]
if lastEnd < thisStart {
var t BoundsStruct = &ANSITable{
Sub: nil,
Data: style,
Bound: [2]int{lastEnd, thisStart},
}
a.L = slices.Insert(a.L, index, t)
index++
}
}
last = at
index++
}
}
}
type ANSIQueueItem struct {
data []byte
startIndex int
}
// NOTE: Planning to make these rune process function available to be set from outside
const TAB_RUNE = '\t'
var TAB_BYTES = []byte{32, 32, 32, 32}
func processRune(r rune, writer *strings.Builder) int {
if r == TAB_RUNE {
writer.Write(TAB_BYTES)
return len(TAB_BYTES)
} else {
writer.WriteRune(r)
return 1
}
}
// transform queue into ansi table which contains all ansi sequences from start to end
func queueToTable(queue []*ANSIQueueItem, endIndex int) *ANSITable {
first := queue[0]
root := &ANSITable{
Bound: [2]int{
first.startIndex,
endIndex,
},
Data: first.data,
}
// add to sub
temp := root
for _, v := range queue[1:] {
temp.Sub = &ANSITable{
Bound: [2]int{
v.startIndex,
endIndex,
},
Data: v.data,
}
temp = temp.Sub
}
return root
}
// split `string with ansi` into `ansi sequences` and `raw string`, Ps: tab will be replaced by 4 spaces
func Extract(s string) (*ANSITableList, string) {
// preserve normal string
var normalString strings.Builder
normalString.Grow(len(s))
// preserve ansi string and position
tables := make([]BoundsStruct, 0)
ansiQueue := make([]*ANSIQueueItem, 0)
ansi := false
// NOTE: do not use `for i := range string` index since it's not i+=1 but i+=byte_len
// solution: transform s into []rune or use custom variable for index
i := 0
var ansiItem *ANSIQueueItem = nil
for _, v := range s {
// meet `esc` char
if v == ESCAPE_SEQUENCE {
// enable ansi mode until meet 'm'
ansi = true
// NOTE: using utf8 rune function
// but maybe just byte(v) is enough since ansi only contains rune of one byte?
byteData := []byte{}
byteData = utf8.AppendRune(byteData, v)
ansiItem = &ANSIQueueItem{
startIndex: i,
data: slices.Clip(byteData),
}
} else {
// in ansi sequence content mode
if ansi {
ansiItem.data = utf8.AppendRune(ansiItem.data, v)
// end of an ansi sequence. terminate
if IsEscEnd(v) {
ansi = false
// clip cap
ansiItem.data = slices.Clip(ansiItem.data)
// filter SGR(function named `m`) and push into queue
if IsSGR(ansiItem.data) {
ansiQueue = append(ansiQueue, ansiItem)
// ends all ansi SGR sequences in queue and create ansi table
if IsEndOfSGR(ansiItem.data) {
// skip if ansi queue only contains "[0m", which means no SGR actually working
if len(ansiQueue) > 1 {
table := queueToTable(ansiQueue[:len(ansiQueue)-1], i)
tables = append(tables, table)
}
// reset queue
ansiQueue = make([]*ANSIQueueItem, 0)
}
}
// reset item
ansiItem = nil
}
} else {
// normal content
i += processRune(v, &normalString)
}
}
}
return &ANSITableList{
L: slices.Clip(tables),
}, normalString.String()
}
func Render(atl *ANSITableList, _s string, startIndex int) []byte {
s := []rune(_s)
at := atl.GetSlice(startIndex, startIndex+len(s))
if len(at) == 0 {
return []byte(_s)
}
var buf bytes.Buffer
index := 0
// every table
for _, a := range at {
// table's sub tables
temp := a.(*ANSITable)
endIndex := temp.Bound[1] - startIndex
for temp != nil {
startIndex := temp.Bound[0] - startIndex
// before table startIndex
if startIndex > index {
subRuneDatas := SliceFrom(s, index, startIndex)
// subRuneDatas := lineRunes[index:startIndex]
for _, runeData := range subRuneDatas {
buf.WriteRune(runeData)
}
index += len(subRuneDatas)
}
// ansi insert
buf.Write(temp.Data)
// assign sub table
temp = temp.Sub
}
// add rest
subRuneDatas := SliceFrom(s, index, endIndex)
for _, runeData := range subRuneDatas {
buf.WriteRune(runeData)
}
index += len(subRuneDatas)
// add end escape
buf.WriteString(ESCAPE_SEQUENCE_END)
}
// add rest
if index <= len(s)-1 {
subRuneDatas := s[index:]
for _, runeData := range subRuneDatas {
buf.WriteRune(runeData)
}
}
return buf.Bytes()
}