-
Notifications
You must be signed in to change notification settings - Fork 0
/
states.go
430 lines (363 loc) · 11.3 KB
/
states.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Copyright 2022 wangqi. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package terminal
/*
type DrawState struct {
width int
height int
cursorCol int
cursorRow int
cursorColor Color
combiningCharCol int
combiningCharRow int
defaultTabs bool
tabs []bool
scrollingRegionTopRow int
scrollingRegionBottomRow int
renditions Renditions
save SavedCursor
// public fields
NextPrintWillWrap bool
// DEC private mode
OriginMode bool // two possiible value: ScrollingRegion(true), Absolute(false)
AutoWrapMode bool // true/false
CursorVisible bool // true/false
ReverseVideo bool // two possible value: Reverse(true), Normal(false)
BracketedPaste bool // true/false
MouseReportingMode int // replace it with MouseTrackingMode
MouseFocusEvent bool // replace it with MouseTrackingState.focusEventMode
MouseAlternateScroll bool // rename to altScrollMode
MouseEncodingMode int // replace it with MouseTrackingEnc
ApplicationModeCursorKeys bool // =cursorKeyMode two possible value : Application(true), ANSI(false)
mouseTrk MouseTrackingState
altSendsEscape bool
// ANSI mode
keyboardLocked bool
InsertMode bool // true/false
localEcho bool
autoNewlineMode bool
// added for vt400 compatibility
compatLevel CompatibilityLevel // VT52, VT100, VT400
altScreenBufferMode bool // Alternate Screen Buffer support: default false
columnMode ColMode // column mode 80 or 132, just for compatibility
horizMarginMode bool // left and right margins support
hMargin int // left margins
nColsEff int // right margins
bkspSendsDel bool // backspace send delete
savedCursorSCO SavedCursorSCO // SCO console cursor state
}
*/
type (
MouseTrackingMode uint16
MouseTrackingEnc uint16
CompatibilityLevel uint8
CursorKeyMode uint8
KeypadMode uint8
OriginMode uint8
ColMode uint8
SelectSnapTo uint8
)
const (
CursorKeyMode_ANSI CursorKeyMode = iota
CursorKeyMode_Application
)
const (
MouseTrackingMode_Disable MouseTrackingMode = 0
MouseTrackingMode_X10_Compat = 9
MouseTrackingMode_VT200 = 1000
MouseTrackingMode_VT200_HighLight = 1001
MouseTrackingMode_VT200_ButtonEvent = 1002
MouseTrackingMode_VT200_AnyEvent = 1003
)
const (
MouseTrackingEnc_Default MouseTrackingEnc = 0
MouseTrackingEnc_UTF8 = 1005
MouseTrackingEnc_SGR = 1006
MouseTrackingEnc_URXVT = 1015
)
const (
CompatLevel_Unused CompatibilityLevel = iota
CompatLevel_VT52
CompatLevel_VT100
CompatLevel_VT400
)
const (
KeypadMode_Normal KeypadMode = iota
KeypadMode_Application
)
const (
ColMode_C80 ColMode = iota
ColMode_C132
)
const (
OriginMode_Absolute OriginMode = iota
OriginMode_ScrollingRegion
)
const (
SelectSnapTo_Char SelectSnapTo = iota
SelectSnapTo_Word
SelectSnapTo_Line
SelectSnapTo_COUNT
)
/*
// TODO replace the following const with the above one
const (
MOUSE_REPORTING_NONE = 0
MOUSE_REPORTING_X10 = 9
MOUSE_REPORTING_VT220 = 1000
MOUSE_REPORTING_VT220_HILIGHT = 1001
MOUSE_REPORTING_BTN_EVENT = 1002
MOUSE_REPORTING_ANY_EVENT = 1003
MOUSE_ENCODING_DEFAULT = 0
MOUSE_ENCODING_UTF8 = 1005
MOUSE_ENCODING_SGR = 1006
MOUSE_ENCODING_URXVT = 1015
)
type SavedCursor struct {
cursorCol, cursorRow int
renditions Renditions
autoWrapMode bool // default value true
originMode bool
}
*/
// check resetScreen() for default value
type MouseTrackingState struct {
mode MouseTrackingMode // replicated by NewFrame(), default: MouseTrackingMode_Disable
enc MouseTrackingEnc // replicated by NewFrame(), default: MouseTrackingEnc_Default
focusEventMode bool // replicated by NewFrame(), default: false
}
func newMouseTrackingState() MouseTrackingState {
mouseTrk := MouseTrackingState{}
mouseTrk.enc = MouseTrackingEnc_Default
mouseTrk.mode = MouseTrackingMode_Disable
mouseTrk.focusEventMode = false
return mouseTrk
}
type SavedCursor_SCO struct {
isSet bool
posX int
posY int
lastCol bool
}
type SavedCursor_DEC struct {
SavedCursor_SCO
attrs Cell
originMode OriginMode // default: OriginMode_Absolute
charsetState CharsetState
}
func newSavedCursor_DEC() SavedCursor_DEC {
decsc := SavedCursor_DEC{}
resetCharsetState(&decsc.charsetState)
decsc.originMode = OriginMode_Absolute
return decsc
}
/*
type SavedCursorSCO struct {
col int
row int
isSet bool
lastCol bool
}
func NewDrawState(width, height int) *DrawState {
ds := new(DrawState)
ds.width = width
ds.height = height
ds.defaultTabs = true
ds.tabs = make([]bool, width)
ds.scrollingRegionBottomRow = height - 1
ds.renditions = Renditions{bgColor: 0}
ds.save = SavedCursor{autoWrapMode: true}
ds.AutoWrapMode = true
ds.CursorVisible = true
ds.cursorColor = ColorWhite
ds.MouseReportingMode = MOUSE_REPORTING_NONE
ds.MouseEncodingMode = MOUSE_ENCODING_DEFAULT
ds.columnMode = ColMode_C80
ds.bkspSendsDel = true
ds.reinitializeTabs(0)
return ds
}
func (ds *DrawState) reinitializeTabs(start uint) {
for i := start; i < uint(len(ds.tabs)); i++ {
ds.tabs[i] = (i % 8) == 0 // TODO : tab size adjustable?
}
}
// set the combining col,row position
func (ds *DrawState) newGrapheme() {
ds.combiningCharCol = ds.cursorCol
ds.combiningCharRow = ds.cursorRow
}
func (ds *DrawState) snapCursorToBorder() {
if ds.cursorRow < ds.limitTop() {
ds.cursorRow = ds.limitTop()
}
if ds.cursorRow > ds.LimitBottom() {
ds.cursorRow = ds.LimitBottom()
}
if ds.cursorCol < 0 {
ds.cursorCol = 0
}
if ds.cursorCol >= ds.width {
ds.cursorCol = ds.width - 1
}
}
func (ds *DrawState) MoveRow(N int, relative bool) {
if relative {
ds.cursorRow += N
} else {
ds.cursorRow = N + ds.limitTop()
}
ds.snapCursorToBorder()
ds.newGrapheme()
ds.NextPrintWillWrap = false
}
func (ds *DrawState) MoveCol(N int, relative bool, implicit bool) {
if implicit {
ds.newGrapheme()
}
if relative {
ds.cursorCol += N
} else {
ds.cursorCol = N
}
if implicit {
ds.NextPrintWillWrap = ds.cursorCol >= ds.width
}
ds.snapCursorToBorder()
if !implicit {
ds.newGrapheme()
ds.NextPrintWillWrap = false
}
}
func (ds DrawState) GetCursorCol() int { return ds.cursorCol }
func (ds DrawState) GetCursorRow() int { return ds.cursorRow }
func (ds DrawState) GetCombiningCharCol() int { return ds.combiningCharCol }
func (ds DrawState) GetCombiningCharRow() int { return ds.combiningCharRow }
func (ds DrawState) GetWidth() int { return ds.width }
func (ds DrawState) GetHeight() int { return ds.height }
func (ds *DrawState) SetTab() { ds.tabs[ds.cursorCol] = true }
func (ds *DrawState) ClearTab(col int) { ds.tabs[col] = false }
func (ds *DrawState) ClearDefaultTabs() { ds.defaultTabs = false }
// Default tabs can't be restored without resetting the draw state.
func (ds DrawState) GetNextTab(count int) int {
if count >= 0 {
for i := ds.cursorCol + 1; i < ds.width; i++ {
if ds.tabs[i] { // find one next tab stop
count -= 1 // finish one tab stop
if count == 0 {
return i
}
}
}
return -1
} else {
for i := ds.cursorCol - 1; i > 0; i-- {
if ds.tabs[i] { // find one previous tab stop
count += 1 // finish one tab stop
if count == 0 {
return i
}
}
}
return 0
}
}
func (ds *DrawState) SetScrollingRegion(top, bottom int) {
if ds.height < 1 {
return
}
ds.scrollingRegionTopRow = top
ds.scrollingRegionBottomRow = bottom
if ds.scrollingRegionTopRow < 0 {
ds.scrollingRegionTopRow = 0
}
if ds.scrollingRegionBottomRow >= ds.height {
ds.scrollingRegionBottomRow = ds.height - 1
}
if ds.scrollingRegionBottomRow < ds.scrollingRegionTopRow {
ds.scrollingRegionBottomRow = ds.scrollingRegionTopRow
}
// real rule requires TWO-line scrolling region
if ds.OriginMode {
ds.snapCursorToBorder()
ds.newGrapheme()
}
}
func (ds DrawState) GetScrollingRegionTopRow() int { return ds.scrollingRegionTopRow }
func (ds DrawState) GetScrollingRegionBottomRow() int { return ds.scrollingRegionBottomRow }
func (ds DrawState) limitTop() int {
if ds.OriginMode {
return ds.scrollingRegionTopRow
}
return 0
}
func (ds DrawState) LimitBottom() int {
if ds.OriginMode {
return ds.scrollingRegionBottomRow
}
return ds.height - 1
}
// set index color for foreground
func (ds *DrawState) SetForegroundColor(index int) { ds.renditions.SetForegroundColor(index) }
// set index color for background
func (ds *DrawState) SetBackgroundColor(index int) { ds.renditions.SetBackgroundColor(index) }
// TODO change the parameter of AddRenditions() from uint32 to none
func (ds *DrawState) AddRenditions() { ds.renditions = Renditions{} }
func (ds *DrawState) GetRenditions() *Renditions { return &ds.renditions }
func (ds DrawState) GetBackgroundRendition() Color { return ds.renditions.bgColor }
func (ds *DrawState) SaveCursor() {
ds.save.cursorCol = ds.cursorCol
ds.save.cursorRow = ds.cursorRow
ds.save.renditions = ds.renditions
ds.save.autoWrapMode = ds.AutoWrapMode
ds.save.originMode = ds.OriginMode
}
func (ds *DrawState) RestoreCursor() {
ds.cursorCol = ds.save.cursorCol
ds.cursorRow = ds.save.cursorRow
ds.renditions = ds.save.renditions
ds.AutoWrapMode = ds.save.autoWrapMode
ds.OriginMode = ds.save.originMode
ds.snapCursorToBorder()
ds.newGrapheme()
}
func (ds *DrawState) ClearSavedCursor() { ds.save = SavedCursor{autoWrapMode: true} }
func (ds *DrawState) Resize(width, height int) {
if ds.width != width || ds.height != height {
// reset entire scrolling region on any resize
// xterm and rxvt-unicode do this. gnome-terminal only
// resets scrolling region if it has to become smaller in resize
ds.scrollingRegionTopRow = 0
ds.scrollingRegionBottomRow = height - 1
}
// TODO : we initialize the tabs slice from the very beginning?
// if something wired happened, please consider to modify it.
ds.tabs = make([]bool, width)
if ds.defaultTabs {
ds.reinitializeTabs(0)
}
ds.width = width
ds.height = height
ds.snapCursorToBorder()
// saved cursor will be snapped to border on restore
// invalidate combining char cell if necessary
if ds.combiningCharCol >= width || ds.combiningCharRow >= height {
ds.combiningCharCol = -1
ds.combiningCharRow = -1
}
}
// use pointer parameter to avoid struct copy
func (ds DrawState) Equal(x *DrawState) bool {
// only compare fields that affect display
return ds.width == x.width && ds.height == x.height &&
ds.cursorCol == x.cursorCol && ds.cursorRow == x.cursorRow &&
ds.CursorVisible == x.CursorVisible && ds.ReverseVideo == x.ReverseVideo &&
ds.renditions == x.renditions &&
ds.BracketedPaste == x.BracketedPaste &&
ds.MouseReportingMode == x.MouseReportingMode &&
ds.MouseFocusEvent == x.MouseFocusEvent &&
ds.MouseAlternateScroll == x.MouseAlternateScroll &&
ds.MouseEncodingMode == x.MouseEncodingMode
}
*/