-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
screen.go
590 lines (498 loc) · 15.6 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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package termenv
import (
"fmt"
"strings"
)
// Sequence definitions.
const (
// Cursor positioning.
CursorUpSeq = "%dA"
CursorDownSeq = "%dB"
CursorForwardSeq = "%dC"
CursorBackSeq = "%dD"
CursorNextLineSeq = "%dE"
CursorPreviousLineSeq = "%dF"
CursorHorizontalSeq = "%dG"
CursorPositionSeq = "%d;%dH"
EraseDisplaySeq = "%dJ"
EraseLineSeq = "%dK"
ScrollUpSeq = "%dS"
ScrollDownSeq = "%dT"
SaveCursorPositionSeq = "s"
RestoreCursorPositionSeq = "u"
ChangeScrollingRegionSeq = "%d;%dr"
InsertLineSeq = "%dL"
DeleteLineSeq = "%dM"
// Explicit values for EraseLineSeq.
EraseLineRightSeq = "0K"
EraseLineLeftSeq = "1K"
EraseEntireLineSeq = "2K"
// Mouse.
EnableMousePressSeq = "?9h" // press only (X10)
DisableMousePressSeq = "?9l"
EnableMouseSeq = "?1000h" // press, release, wheel
DisableMouseSeq = "?1000l"
EnableMouseHiliteSeq = "?1001h" // highlight
DisableMouseHiliteSeq = "?1001l"
EnableMouseCellMotionSeq = "?1002h" // press, release, move on pressed, wheel
DisableMouseCellMotionSeq = "?1002l"
EnableMouseAllMotionSeq = "?1003h" // press, release, move, wheel
DisableMouseAllMotionSeq = "?1003l"
EnableMouseExtendedModeSeq = "?1006h" // press, release, move, wheel, extended coordinates
DisableMouseExtendedModeSeq = "?1006l"
EnableMousePixelsModeSeq = "?1016h" // press, release, move, wheel, extended pixel coordinates
DisableMousePixelsModeSeq = "?1016l"
// Screen.
RestoreScreenSeq = "?47l"
SaveScreenSeq = "?47h"
AltScreenSeq = "?1049h"
ExitAltScreenSeq = "?1049l"
// Bracketed paste.
// https://en.wikipedia.org/wiki/Bracketed-paste
EnableBracketedPasteSeq = "?2004h"
DisableBracketedPasteSeq = "?2004l"
StartBracketedPasteSeq = "200~"
EndBracketedPasteSeq = "201~"
// Session.
SetWindowTitleSeq = "2;%s" + string(BEL)
SetForegroundColorSeq = "10;%s" + string(BEL)
SetBackgroundColorSeq = "11;%s" + string(BEL)
SetCursorColorSeq = "12;%s" + string(BEL)
ShowCursorSeq = "?25h"
HideCursorSeq = "?25l"
)
// Reset the terminal to its default style, removing any active styles.
func (o Output) Reset() {
fmt.Fprint(o.w, CSI+ResetSeq+"m")
}
// SetForegroundColor sets the default foreground color.
func (o Output) SetForegroundColor(color Color) {
fmt.Fprintf(o.w, OSC+SetForegroundColorSeq, color)
}
// SetBackgroundColor sets the default background color.
func (o Output) SetBackgroundColor(color Color) {
fmt.Fprintf(o.w, OSC+SetBackgroundColorSeq, color)
}
// SetCursorColor sets the cursor color.
func (o Output) SetCursorColor(color Color) {
fmt.Fprintf(o.w, OSC+SetCursorColorSeq, color)
}
// RestoreScreen restores a previously saved screen state.
func (o Output) RestoreScreen() {
fmt.Fprint(o.w, CSI+RestoreScreenSeq)
}
// SaveScreen saves the screen state.
func (o Output) SaveScreen() {
fmt.Fprint(o.w, CSI+SaveScreenSeq)
}
// AltScreen switches to the alternate screen buffer. The former view can be
// restored with ExitAltScreen().
func (o Output) AltScreen() {
fmt.Fprint(o.w, CSI+AltScreenSeq)
}
// ExitAltScreen exits the alternate screen buffer and returns to the former
// terminal view.
func (o Output) ExitAltScreen() {
fmt.Fprint(o.w, CSI+ExitAltScreenSeq)
}
// ClearScreen clears the visible portion of the terminal.
func (o Output) ClearScreen() {
fmt.Fprintf(o.w, CSI+EraseDisplaySeq, 2)
o.MoveCursor(1, 1)
}
// MoveCursor moves the cursor to a given position.
func (o Output) MoveCursor(row int, column int) {
fmt.Fprintf(o.w, CSI+CursorPositionSeq, row, column)
}
// HideCursor hides the cursor.
func (o Output) HideCursor() {
fmt.Fprint(o.w, CSI+HideCursorSeq)
}
// ShowCursor shows the cursor.
func (o Output) ShowCursor() {
fmt.Fprint(o.w, CSI+ShowCursorSeq)
}
// SaveCursorPosition saves the cursor position.
func (o Output) SaveCursorPosition() {
fmt.Fprint(o.w, CSI+SaveCursorPositionSeq)
}
// RestoreCursorPosition restores a saved cursor position.
func (o Output) RestoreCursorPosition() {
fmt.Fprint(o.w, CSI+RestoreCursorPositionSeq)
}
// CursorUp moves the cursor up a given number of lines.
func (o Output) CursorUp(n int) {
fmt.Fprintf(o.w, CSI+CursorUpSeq, n)
}
// CursorDown moves the cursor down a given number of lines.
func (o Output) CursorDown(n int) {
fmt.Fprintf(o.w, CSI+CursorDownSeq, n)
}
// CursorForward moves the cursor up a given number of lines.
func (o Output) CursorForward(n int) {
fmt.Fprintf(o.w, CSI+CursorForwardSeq, n)
}
// CursorBack moves the cursor backwards a given number of cells.
func (o Output) CursorBack(n int) {
fmt.Fprintf(o.w, CSI+CursorBackSeq, n)
}
// CursorNextLine moves the cursor down a given number of lines and places it at
// the beginning of the line.
func (o Output) CursorNextLine(n int) {
fmt.Fprintf(o.w, CSI+CursorNextLineSeq, n)
}
// CursorPrevLine moves the cursor up a given number of lines and places it at
// the beginning of the line.
func (o Output) CursorPrevLine(n int) {
fmt.Fprintf(o.w, CSI+CursorPreviousLineSeq, n)
}
// ClearLine clears the current line.
func (o Output) ClearLine() {
fmt.Fprint(o.w, CSI+EraseEntireLineSeq)
}
// ClearLineLeft clears the line to the left of the cursor.
func (o Output) ClearLineLeft() {
fmt.Fprint(o.w, CSI+EraseLineLeftSeq)
}
// ClearLineRight clears the line to the right of the cursor.
func (o Output) ClearLineRight() {
fmt.Fprint(o.w, CSI+EraseLineRightSeq)
}
// ClearLines clears a given number of lines.
func (o Output) ClearLines(n int) {
clearLine := fmt.Sprintf(CSI+EraseLineSeq, 2)
cursorUp := fmt.Sprintf(CSI+CursorUpSeq, 1)
fmt.Fprint(o.w, clearLine+strings.Repeat(cursorUp+clearLine, n))
}
// ChangeScrollingRegion sets the scrolling region of the terminal.
func (o Output) ChangeScrollingRegion(top, bottom int) {
fmt.Fprintf(o.w, CSI+ChangeScrollingRegionSeq, top, bottom)
}
// InsertLines inserts the given number of lines at the top of the scrollable
// region, pushing lines below down.
func (o Output) InsertLines(n int) {
fmt.Fprintf(o.w, CSI+InsertLineSeq, n)
}
// DeleteLines deletes the given number of lines, pulling any lines in
// the scrollable region below up.
func (o Output) DeleteLines(n int) {
fmt.Fprintf(o.w, CSI+DeleteLineSeq, n)
}
// EnableMousePress enables X10 mouse mode. Button press events are sent only.
func (o Output) EnableMousePress() {
fmt.Fprint(o.w, CSI+EnableMousePressSeq)
}
// DisableMousePress disables X10 mouse mode.
func (o Output) DisableMousePress() {
fmt.Fprint(o.w, CSI+DisableMousePressSeq)
}
// EnableMouse enables Mouse Tracking mode.
func (o Output) EnableMouse() {
fmt.Fprint(o.w, CSI+EnableMouseSeq)
}
// DisableMouse disables Mouse Tracking mode.
func (o Output) DisableMouse() {
fmt.Fprint(o.w, CSI+DisableMouseSeq)
}
// EnableMouseHilite enables Hilite Mouse Tracking mode.
func (o Output) EnableMouseHilite() {
fmt.Fprint(o.w, CSI+EnableMouseHiliteSeq)
}
// DisableMouseHilite disables Hilite Mouse Tracking mode.
func (o Output) DisableMouseHilite() {
fmt.Fprint(o.w, CSI+DisableMouseHiliteSeq)
}
// EnableMouseCellMotion enables Cell Motion Mouse Tracking mode.
func (o Output) EnableMouseCellMotion() {
fmt.Fprint(o.w, CSI+EnableMouseCellMotionSeq)
}
// DisableMouseCellMotion disables Cell Motion Mouse Tracking mode.
func (o Output) DisableMouseCellMotion() {
fmt.Fprint(o.w, CSI+DisableMouseCellMotionSeq)
}
// EnableMouseAllMotion enables All Motion Mouse mode.
func (o Output) EnableMouseAllMotion() {
fmt.Fprint(o.w, CSI+EnableMouseAllMotionSeq)
}
// DisableMouseAllMotion disables All Motion Mouse mode.
func (o Output) DisableMouseAllMotion() {
fmt.Fprint(o.w, CSI+DisableMouseAllMotionSeq)
}
// EnableMouseExtendedMotion enables Extended Mouse mode (SGR). This should be
// enabled in conjunction with EnableMouseCellMotion, and EnableMouseAllMotion.
func (o Output) EnableMouseExtendedMode() {
fmt.Fprint(o.w, CSI+EnableMouseExtendedModeSeq)
}
// DisableMouseExtendedMotion disables Extended Mouse mode (SGR).
func (o Output) DisableMouseExtendedMode() {
fmt.Fprint(o.w, CSI+DisableMouseExtendedModeSeq)
}
// EnableMousePixelsMotion enables Pixel Motion Mouse mode (SGR-Pixels). This
// should be enabled in conjunction with EnableMouseCellMotion, and
// EnableMouseAllMotion.
func (o Output) EnableMousePixelsMode() {
fmt.Fprint(o.w, CSI+EnableMousePixelsModeSeq)
}
// DisableMousePixelsMotion disables Pixel Motion Mouse mode (SGR-Pixels).
func (o Output) DisableMousePixelsMode() {
fmt.Fprint(o.w, CSI+DisableMousePixelsModeSeq)
}
// SetWindowTitle sets the terminal window title.
func (o Output) SetWindowTitle(title string) {
fmt.Fprintf(o.w, OSC+SetWindowTitleSeq, title)
}
// EnableBracketedPaste enables bracketed paste.
func (o Output) EnableBracketedPaste() {
fmt.Fprintf(o.w, CSI+EnableBracketedPasteSeq)
}
// DisableBracketedPaste disables bracketed paste.
func (o Output) DisableBracketedPaste() {
fmt.Fprintf(o.w, CSI+DisableBracketedPasteSeq)
}
// Legacy functions.
// Reset the terminal to its default style, removing any active styles.
//
// Deprecated: please use termenv.Output instead.
func Reset() {
output.Reset()
}
// SetForegroundColor sets the default foreground color.
//
// Deprecated: please use termenv.Output instead.
func SetForegroundColor(color Color) {
output.SetForegroundColor(color)
}
// SetBackgroundColor sets the default background color.
//
// Deprecated: please use termenv.Output instead.
func SetBackgroundColor(color Color) {
output.SetBackgroundColor(color)
}
// SetCursorColor sets the cursor color.
//
// Deprecated: please use termenv.Output instead.
func SetCursorColor(color Color) {
output.SetCursorColor(color)
}
// RestoreScreen restores a previously saved screen state.
//
// Deprecated: please use termenv.Output instead.
func RestoreScreen() {
output.RestoreScreen()
}
// SaveScreen saves the screen state.
//
// Deprecated: please use termenv.Output instead.
func SaveScreen() {
output.SaveScreen()
}
// AltScreen switches to the alternate screen buffer. The former view can be
// restored with ExitAltScreen().
//
// Deprecated: please use termenv.Output instead.
func AltScreen() {
output.AltScreen()
}
// ExitAltScreen exits the alternate screen buffer and returns to the former
// terminal view.
//
// Deprecated: please use termenv.Output instead.
func ExitAltScreen() {
output.ExitAltScreen()
}
// ClearScreen clears the visible portion of the terminal.
//
// Deprecated: please use termenv.Output instead.
func ClearScreen() {
output.ClearScreen()
}
// MoveCursor moves the cursor to a given position.
//
// Deprecated: please use termenv.Output instead.
func MoveCursor(row int, column int) {
output.MoveCursor(row, column)
}
// HideCursor hides the cursor.
//
// Deprecated: please use termenv.Output instead.
func HideCursor() {
output.HideCursor()
}
// ShowCursor shows the cursor.
//
// Deprecated: please use termenv.Output instead.
func ShowCursor() {
output.ShowCursor()
}
// SaveCursorPosition saves the cursor position.
//
// Deprecated: please use termenv.Output instead.
func SaveCursorPosition() {
output.SaveCursorPosition()
}
// RestoreCursorPosition restores a saved cursor position.
//
// Deprecated: please use termenv.Output instead.
func RestoreCursorPosition() {
output.RestoreCursorPosition()
}
// CursorUp moves the cursor up a given number of lines.
//
// Deprecated: please use termenv.Output instead.
func CursorUp(n int) {
output.CursorUp(n)
}
// CursorDown moves the cursor down a given number of lines.
//
// Deprecated: please use termenv.Output instead.
func CursorDown(n int) {
output.CursorDown(n)
}
// CursorForward moves the cursor up a given number of lines.
//
// Deprecated: please use termenv.Output instead.
func CursorForward(n int) {
output.CursorForward(n)
}
// CursorBack moves the cursor backwards a given number of cells.
//
// Deprecated: please use termenv.Output instead.
func CursorBack(n int) {
output.CursorBack(n)
}
// CursorNextLine moves the cursor down a given number of lines and places it at
// the beginning of the line.
//
// Deprecated: please use termenv.Output instead.
func CursorNextLine(n int) {
output.CursorNextLine(n)
}
// CursorPrevLine moves the cursor up a given number of lines and places it at
// the beginning of the line.
//
// Deprecated: please use termenv.Output instead.
func CursorPrevLine(n int) {
output.CursorPrevLine(n)
}
// ClearLine clears the current line.
//
// Deprecated: please use termenv.Output instead.
func ClearLine() {
output.ClearLine()
}
// ClearLineLeft clears the line to the left of the cursor.
//
// Deprecated: please use termenv.Output instead.
func ClearLineLeft() {
output.ClearLineLeft()
}
// ClearLineRight clears the line to the right of the cursor.
//
// Deprecated: please use termenv.Output instead.
func ClearLineRight() {
output.ClearLineRight()
}
// ClearLines clears a given number of lines.
//
// Deprecated: please use termenv.Output instead.
func ClearLines(n int) {
output.ClearLines(n)
}
// ChangeScrollingRegion sets the scrolling region of the terminal.
//
// Deprecated: please use termenv.Output instead.
func ChangeScrollingRegion(top, bottom int) {
output.ChangeScrollingRegion(top, bottom)
}
// InsertLines inserts the given number of lines at the top of the scrollable
// region, pushing lines below down.
//
// Deprecated: please use termenv.Output instead.
func InsertLines(n int) {
output.InsertLines(n)
}
// DeleteLines deletes the given number of lines, pulling any lines in
// the scrollable region below up.
//
// Deprecated: please use termenv.Output instead.
func DeleteLines(n int) {
output.DeleteLines(n)
}
// EnableMousePress enables X10 mouse mode. Button press events are sent only.
//
// Deprecated: please use termenv.Output instead.
func EnableMousePress() {
output.EnableMousePress()
}
// DisableMousePress disables X10 mouse mode.
//
// Deprecated: please use termenv.Output instead.
func DisableMousePress() {
output.DisableMousePress()
}
// EnableMouse enables Mouse Tracking mode.
//
// Deprecated: please use termenv.Output instead.
func EnableMouse() {
output.EnableMouse()
}
// DisableMouse disables Mouse Tracking mode.
//
// Deprecated: please use termenv.Output instead.
func DisableMouse() {
output.DisableMouse()
}
// EnableMouseHilite enables Hilite Mouse Tracking mode.
//
// Deprecated: please use termenv.Output instead.
func EnableMouseHilite() {
output.EnableMouseHilite()
}
// DisableMouseHilite disables Hilite Mouse Tracking mode.
//
// Deprecated: please use termenv.Output instead.
func DisableMouseHilite() {
output.DisableMouseHilite()
}
// EnableMouseCellMotion enables Cell Motion Mouse Tracking mode.
//
// Deprecated: please use termenv.Output instead.
func EnableMouseCellMotion() {
output.EnableMouseCellMotion()
}
// DisableMouseCellMotion disables Cell Motion Mouse Tracking mode.
//
// Deprecated: please use termenv.Output instead.
func DisableMouseCellMotion() {
output.DisableMouseCellMotion()
}
// EnableMouseAllMotion enables All Motion Mouse mode.
//
// Deprecated: please use termenv.Output instead.
func EnableMouseAllMotion() {
output.EnableMouseAllMotion()
}
// DisableMouseAllMotion disables All Motion Mouse mode.
//
// Deprecated: please use termenv.Output instead.
func DisableMouseAllMotion() {
output.DisableMouseAllMotion()
}
// SetWindowTitle sets the terminal window title.
//
// Deprecated: please use termenv.Output instead.
func SetWindowTitle(title string) {
output.SetWindowTitle(title)
}
// EnableBracketedPaste enables bracketed paste.
//
// Deprecated: please use termenv.Output instead.
func EnableBracketedPaste() {
output.EnableBracketedPaste()
}
// DisableBracketedPaste disables bracketed paste.
//
// Deprecated: please use termenv.Output instead.
func DisableBracketedPaste() {
output.DisableBracketedPaste()
}