Debian Codegraph @

ansi/screen.go

raw
1 package ansi
2
3 import (
4 "strconv"
5 "strings"
6 )
7
8 // EraseDisplay (ED) clears the display or parts of the display. A screen is
9 // the shown part of the terminal display excluding the scrollback buffer.
10 // Possible values:
11 //
12 // Default is 0.
13 //
14 // 0: Clear from cursor to end of screen.
15 // 1: Clear from cursor to beginning of the screen.
16 // 2: Clear entire screen (and moves cursor to upper left on DOS).
17 // 3: Clear entire display which delete all lines saved in the scrollback buffer (xterm).
18 //
19 // CSI <n> J
20 //
21 // See: https://vt100.net/docs/vt510-rm/ED.html
22 func EraseDisplay(n int) string {
23 var s string
24 if n > 0 {
25 s = strconv.Itoa(n)
26 }
27 return "\x1b[" + s + "J"
28 }
29
30 // ED is an alias for [EraseDisplay].
31 func ED(n int) string {
32 return EraseDisplay(n)
33 }
34
35 // EraseDisplay constants.
36 // These are the possible values for the EraseDisplay function.
37 const (
38 EraseScreenBelow = "\x1b[J"
39 EraseScreenAbove = "\x1b[1J"
40 EraseEntireScreen = "\x1b[2J"
41 EraseEntireDisplay = "\x1b[3J"
42 )
43
44 // EraseLine (EL) clears the current line or parts of the line. Possible values:
45 //
46 // 0: Clear from cursor to end of line.
47 // 1: Clear from cursor to beginning of the line.
48 // 2: Clear entire line.
49 //
50 // The cursor position is not affected.
51 //
52 // CSI <n> K
53 //
54 // See: https://vt100.net/docs/vt510-rm/EL.html
55 func EraseLine(n int) string {
56 var s string
57 if n > 0 {
58 s = strconv.Itoa(n)
59 }
60 return "\x1b[" + s + "K"
61 }
62
63 // EL is an alias for [EraseLine].
64 func EL(n int) string {
65 return EraseLine(n)
66 }
67
68 // EraseLine constants.
69 // These are the possible values for the EraseLine function.
70 const (
71 EraseLineRight = "\x1b[K"
72 EraseLineLeft = "\x1b[1K"
73 EraseEntireLine = "\x1b[2K"
74 )
75
76 // ScrollUp (SU) scrolls the screen up n lines. New lines are added at the
77 // bottom of the screen.
78 //
79 // CSI Pn S
80 //
81 // See: https://vt100.net/docs/vt510-rm/SU.html
82 func ScrollUp(n int) string {
83 var s string
84 if n > 1 {
85 s = strconv.Itoa(n)
86 }
87 return "\x1b[" + s + "S"
88 }
89
90 // PanDown is an alias for [ScrollUp].
91 func PanDown(n int) string {
92 return ScrollUp(n)
93 }
94
95 // SU is an alias for [ScrollUp].
96 func SU(n int) string {
97 return ScrollUp(n)
98 }
99
100 // ScrollDown (SD) scrolls the screen down n lines. New lines are added at the
101 // top of the screen.
102 //
103 // CSI Pn T
104 //
105 // See: https://vt100.net/docs/vt510-rm/SD.html
106 func ScrollDown(n int) string {
107 var s string
108 if n > 1 {
109 s = strconv.Itoa(n)
110 }
111 return "\x1b[" + s + "T"
112 }
113
114 // PanUp is an alias for [ScrollDown].
115 func PanUp(n int) string {
116 return ScrollDown(n)
117 }
118
119 // SD is an alias for [ScrollDown].
120 func SD(n int) string {
121 return ScrollDown(n)
122 }
123
124 // InsertLine (IL) inserts n blank lines at the current cursor position.
125 // Existing lines are moved down.
126 //
127 // CSI Pn L
128 //
129 // See: https://vt100.net/docs/vt510-rm/IL.html
130 func InsertLine(n int) string {
131 var s string
132 if n > 1 {
133 s = strconv.Itoa(n)
134 }
135 return "\x1b[" + s + "L"
136 }
137
138 // IL is an alias for [InsertLine].
139 func IL(n int) string {
140 return InsertLine(n)
141 }
142
143 // DeleteLine (DL) deletes n lines at the current cursor position. Existing
144 // lines are moved up.
145 //
146 // CSI Pn M
147 //
148 // See: https://vt100.net/docs/vt510-rm/DL.html
149 func DeleteLine(n int) string {
150 var s string
151 if n > 1 {
152 s = strconv.Itoa(n)
153 }
154 return "\x1b[" + s + "M"
155 }
156
157 // DL is an alias for [DeleteLine].
158 func DL(n int) string {
159 return DeleteLine(n)
160 }
161
162 // SetTopBottomMargins (DECSTBM) sets the top and bottom margins for the scrolling
163 // region. The default is the entire screen.
164 //
165 // Default is 1 and the bottom of the screen.
166 //
167 // CSI Pt ; Pb r
168 //
169 // See: https://vt100.net/docs/vt510-rm/DECSTBM.html
170 func SetTopBottomMargins(top, bot int) string {
171 var t, b string
172 if top > 0 {
173 t = strconv.Itoa(top)
174 }
175 if bot > 0 {
176 b = strconv.Itoa(bot)
177 }
178 return "\x1b[" + t + ";" + b + "r"
179 }
180
181 // DECSTBM is an alias for [SetTopBottomMargins].
182 func DECSTBM(top, bot int) string {
183 return SetTopBottomMargins(top, bot)
184 }
185
186 // SetLeftRightMargins (DECSLRM) sets the left and right margins for the scrolling
187 // region.
188 //
189 // Default is 1 and the right of the screen.
190 //
191 // CSI Pl ; Pr s
192 //
193 // See: https://vt100.net/docs/vt510-rm/DECSLRM.html
194 func SetLeftRightMargins(left, right int) string {
195 var l, r string
196 if left > 0 {
197 l = strconv.Itoa(left)
198 }
199 if right > 0 {
200 r = strconv.Itoa(right)
201 }
202 return "\x1b[" + l + ";" + r + "s"
203 }
204
205 // DECSLRM is an alias for [SetLeftRightMargins].
206 func DECSLRM(left, right int) string {
207 return SetLeftRightMargins(left, right)
208 }
209
210 // SetScrollingRegion (DECSTBM) sets the top and bottom margins for the scrolling
211 // region. The default is the entire screen.
212 //
213 // CSI <top> ; <bottom> r
214 //
215 // See: https://vt100.net/docs/vt510-rm/DECSTBM.html
216 //
217 // Deprecated: use [SetTopBottomMargins] instead.
218 func SetScrollingRegion(t, b int) string {
219 if t < 0 {
220 t = 0
221 }
222 if b < 0 {
223 b = 0
224 }
225 return "\x1b[" + strconv.Itoa(t) + ";" + strconv.Itoa(b) + "r"
226 }
227
228 // InsertCharacter (ICH) inserts n blank characters at the current cursor
229 // position. Existing characters move to the right. Characters moved past the
230 // right margin are lost. ICH has no effect outside the scrolling margins.
231 //
232 // Default is 1.
233 //
234 // CSI Pn @
235 //
236 // See: https://vt100.net/docs/vt510-rm/ICH.html
237 func InsertCharacter(n int) string {
238 var s string
239 if n > 1 {
240 s = strconv.Itoa(n)
241 }
242 return "\x1b[" + s + "@"
243 }
244
245 // ICH is an alias for [InsertCharacter].
246 func ICH(n int) string {
247 return InsertCharacter(n)
248 }
249
250 // DeleteCharacter (DCH) deletes n characters at the current cursor position.
251 // As the characters are deleted, the remaining characters move to the left and
252 // the cursor remains at the same position.
253 //
254 // Default is 1.
255 //
256 // CSI Pn P
257 //
258 // See: https://vt100.net/docs/vt510-rm/DCH.html
259 func DeleteCharacter(n int) string {
260 var s string
261 if n > 1 {
262 s = strconv.Itoa(n)
263 }
264 return "\x1b[" + s + "P"
265 }
266
267 // DCH is an alias for [DeleteCharacter].
268 func DCH(n int) string {
269 return DeleteCharacter(n)
270 }
271
272 // SetTabEvery8Columns (DECST8C) sets the tab stops at every 8 columns.
273 //
274 // CSI ? 5 W
275 //
276 // See: https://vt100.net/docs/vt510-rm/DECST8C.html
277 const (
278 SetTabEvery8Columns = "\x1b[?5W"
279 DECST8C = SetTabEvery8Columns
280 )
281
282 // HorizontalTabSet (HTS) sets a horizontal tab stop at the current cursor
283 // column.
284 //
285 // This is equivalent to [HTS].
286 //
287 // ESC H
288 //
289 // See: https://vt100.net/docs/vt510-rm/HTS.html
290 const HorizontalTabSet = "\x1bH"
291
292 // TabClear (TBC) clears tab stops.
293 //
294 // Default is 0.
295 //
296 // Possible values:
297 // 0: Clear tab stop at the current column. (default)
298 // 3: Clear all tab stops.
299 //
300 // CSI Pn g
301 //
302 // See: https://vt100.net/docs/vt510-rm/TBC.html
303 func TabClear(n int) string {
304 var s string
305 if n > 0 {
306 s = strconv.Itoa(n)
307 }
308 return "\x1b[" + s + "g"
309 }
310
311 // TBC is an alias for [TabClear].
312 func TBC(n int) string {
313 return TabClear(n)
314 }
315
316 // RequestPresentationStateReport (DECRQPSR) requests the terminal to send a
317 // report of the presentation state. This includes the cursor information [DECCIR],
318 // and tab stop [DECTABSR] reports.
319 //
320 // Default is 0.
321 //
322 // Possible values:
323 // 0: Error, request ignored.
324 // 1: Cursor information report [DECCIR].
325 // 2: Tab stop report [DECTABSR].
326 //
327 // CSI Ps $ w
328 //
329 // See: https://vt100.net/docs/vt510-rm/DECRQPSR.html
330 func RequestPresentationStateReport(n int) string {
331 var s string
332 if n > 0 {
333 s = strconv.Itoa(n)
334 }
335 return "\x1b[" + s + "$w"
336 }
337
338 // DECRQPSR is an alias for [RequestPresentationStateReport].
339 func DECRQPSR(n int) string {
340 return RequestPresentationStateReport(n)
341 }
342
343 // TabStopReport (DECTABSR) is the response to a tab stop report request.
344 // It reports the tab stops set in the terminal.
345 //
346 // The response is a list of tab stops separated by a slash (/) character.
347 //
348 // DCS 2 $ u D ... D ST
349 //
350 // Where D is a decimal number representing a tab stop.
351 //
352 // See: https://vt100.net/docs/vt510-rm/DECTABSR.html
353 func TabStopReport(stops ...int) string {
354 var s []string //nolint:prealloc
355 for _, v := range stops {
356 s = append(s, strconv.Itoa(v))
357 }
358 return "\x1bP2$u" + strings.Join(s, "/") + "\x1b\\"
359 }
360
361 // DECTABSR is an alias for [TabStopReport].
362 func DECTABSR(stops ...int) string {
363 return TabStopReport(stops...)
364 }
365
366 // CursorInformationReport (DECCIR) is the response to a cursor information
367 // report request. It reports the cursor position, visual attributes, and
368 // character protection attributes. It also reports the status of origin mode
369 // [DECOM] and the current active character set.
370 //
371 // The response is a list of values separated by a semicolon (;) character.
372 //
373 // DCS 1 $ u D ... D ST
374 //
375 // Where D is a decimal number representing a value.
376 //
377 // See: https://vt100.net/docs/vt510-rm/DECCIR.html
378 func CursorInformationReport(values ...int) string {
379 var s []string //nolint:prealloc
380 for _, v := range values {
381 s = append(s, strconv.Itoa(v))
382 }
383 return "\x1bP1$u" + strings.Join(s, ";") + "\x1b\\"
384 }
385
386 // DECCIR is an alias for [CursorInformationReport].
387 func DECCIR(values ...int) string {
388 return CursorInformationReport(values...)
389 }
390
391 // RepeatPreviousCharacter (REP) repeats the previous character n times.
392 // This is identical to typing the same character n times.
393 //
394 // Default is 1.
395 //
396 // CSI Pn b
397 //
398 // See: ECMA-48 ยง 8.3.103.
399 func RepeatPreviousCharacter(n int) string {
400 var s string
401 if n > 1 {
402 s = strconv.Itoa(n)
403 }
404 return "\x1b[" + s + "b"
405 }
406
407 // REP is an alias for [RepeatPreviousCharacter].
408 func REP(n int) string {
409 return RepeatPreviousCharacter(n)
410 }