/
screen.go
258 lines (214 loc) · 6.65 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
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"
// Screen.
RestoreScreenSeq = "?47l"
SaveScreenSeq = "?47h"
AltScreenSeq = "?1049h"
ExitAltScreenSeq = "?1049l"
// Session.
SetWindowTitleSeq = "2;%s\007"
SetForegroundColorSeq = "10;%s\007"
SetBackgroundColorSeq = "11;%s\007"
SetCursorColorSeq = "12;%s\007"
ShowCursorSeq = "?25h"
HideCursorSeq = "?25l"
)
// Reset the terminal to its default style, removing any active styles.
func Reset() {
fmt.Print(CSI + ResetSeq + "m")
}
// SetForegroundColor sets the default foreground color.
func SetForegroundColor(color Color) {
fmt.Printf(OSC+SetForegroundColorSeq, color)
}
// SetBackgroundColor sets the default background color.
func SetBackgroundColor(color Color) {
fmt.Printf(OSC+SetBackgroundColorSeq, color)
}
// SetCursorColor sets the cursor color.
func SetCursorColor(color Color) {
fmt.Printf(OSC+SetCursorColorSeq, color)
}
// RestoreScreen restores a previously saved screen state.
func RestoreScreen() {
fmt.Print(CSI + RestoreScreenSeq)
}
// SaveScreen saves the screen state.
func SaveScreen() {
fmt.Print(CSI + SaveScreenSeq)
}
// AltScreen switches to the alternate screen buffer. The former view can be
// restored with ExitAltScreen().
func AltScreen() {
fmt.Print(CSI + AltScreenSeq)
}
// ExitAltScreen exits the alternate screen buffer and returns to the former
// terminal view.
func ExitAltScreen() {
fmt.Print(CSI + ExitAltScreenSeq)
}
// ClearScreen clears the visible portion of the terminal.
func ClearScreen() {
fmt.Printf(CSI+EraseDisplaySeq, 2)
MoveCursor(1, 1)
}
// MoveCursor moves the cursor to a given position.
func MoveCursor(row int, column int) {
fmt.Printf(CSI+CursorPositionSeq, row, column)
}
// HideCursor hides the cursor.
func HideCursor() {
fmt.Printf(CSI + HideCursorSeq)
}
// ShowCursor shows the cursor.
func ShowCursor() {
fmt.Printf(CSI + ShowCursorSeq)
}
// SaveCursorPosition saves the cursor position.
func SaveCursorPosition() {
fmt.Print(CSI + SaveCursorPositionSeq)
}
// RestoreCursorPosition restores a saved cursor position.
func RestoreCursorPosition() {
fmt.Print(CSI + RestoreCursorPositionSeq)
}
// CursorUp moves the cursor up a given number of lines.
func CursorUp(n int) {
fmt.Printf(CSI+CursorUpSeq, n)
}
// CursorDown moves the cursor down a given number of lines.
func CursorDown(n int) {
fmt.Printf(CSI+CursorDownSeq, n)
}
// CursorForward moves the cursor up a given number of lines.
func CursorForward(n int) {
fmt.Printf(CSI+CursorForwardSeq, n)
}
// CursorBack moves the cursor backwards a given number of cells.
func CursorBack(n int) {
fmt.Printf(CSI+CursorBackSeq, n)
}
// CursorNextLine moves the cursor down a given number of lines and places it at
// the beginning of the line.
func CursorNextLine(n int) {
fmt.Printf(CSI+CursorNextLineSeq, n)
}
// CursorPrevLine moves the cursor up a given number of lines and places it at
// the beginning of the line.
func CursorPrevLine(n int) {
fmt.Printf(CSI+CursorPreviousLineSeq, n)
}
// ClearLine clears the current line.
func ClearLine() {
fmt.Print(CSI + EraseEntireLineSeq)
}
// ClearLineLeft clears the line to the left of the cursor.
func ClearLineLeft() {
fmt.Print(CSI + EraseLineLeftSeq)
}
// ClearLineRight clears the line to the right of the cursor.
func ClearLineRight() {
fmt.Print(CSI + EraseLineRightSeq)
}
// ClearLines clears a given number of lines.
func ClearLines(n int) {
clearLine := fmt.Sprintf(CSI+EraseLineSeq, 2)
cursorUp := fmt.Sprintf(CSI+CursorUpSeq, 1)
fmt.Print(clearLine + strings.Repeat(cursorUp+clearLine, n))
}
// ChangeScrollingRegion sets the scrolling region of the terminal.
func ChangeScrollingRegion(top, bottom int) {
fmt.Printf(CSI+ChangeScrollingRegionSeq, top, bottom)
}
// InsertLines inserts the given number of lines at the top of the scrollable
// region, pushing lines below down.
func InsertLines(n int) {
fmt.Printf(CSI+InsertLineSeq, n)
}
// DeleteLines deletes the given number of lines, pulling any lines in
// the scrollable region below up.
func DeleteLines(n int) {
fmt.Printf(CSI+DeleteLineSeq, n)
}
// EnableMousePress enables X10 mouse mode. Button press events are sent only.
func EnableMousePress() {
fmt.Print(CSI + EnableMousePressSeq)
}
// DisableMousePress disables X10 mouse mode.
func DisableMousePress() {
fmt.Print(CSI + DisableMousePressSeq)
}
// EnableMouse enables Mouse Tracking mode.
func EnableMouse() {
fmt.Print(CSI + EnableMouseSeq)
}
// DisableMouse disables Mouse Tracking mode.
func DisableMouse() {
fmt.Print(CSI + DisableMouseSeq)
}
// EnableMouseHilite enables Hilite Mouse Tracking mode.
func EnableMouseHilite() {
fmt.Print(CSI + EnableMouseHiliteSeq)
}
// DisableMouseHilite disables Hilite Mouse Tracking mode.
func DisableMouseHilite() {
fmt.Print(CSI + DisableMouseHiliteSeq)
}
// EnableMouseCellMotion enables Cell Motion Mouse Tracking mode.
func EnableMouseCellMotion() {
fmt.Print(CSI + EnableMouseCellMotionSeq)
}
// DisableMouseCellMotion disables Cell Motion Mouse Tracking mode.
func DisableMouseCellMotion() {
fmt.Print(CSI + DisableMouseCellMotionSeq)
}
// EnableMouseAllMotion enables All Motion Mouse mode.
func EnableMouseAllMotion() {
fmt.Print(CSI + EnableMouseAllMotionSeq)
}
// DisableMouseAllMotion disables All Motion Mouse mode.
func DisableMouseAllMotion() {
fmt.Print(CSI + DisableMouseAllMotionSeq)
}
// SetWindowTitle sets the terminal window title.
func SetWindowTitle(title string) {
fmt.Printf(OSC+SetWindowTitleSeq, title)
}