Debian Codegraph @

ansi/cursor.go

raw
1 package ansi
2
3 import (
4 "strconv"
5 )
6
7 // SaveCursor (DECSC) is an escape sequence that saves the current cursor
8 // position.
9 //
10 // ESC 7
11 //
12 // See: https://vt100.net/docs/vt510-rm/DECSC.html
13 const (
14 SaveCursor = "\x1b7"
15 DECSC = SaveCursor
16 )
17
18 // RestoreCursor (DECRC) is an escape sequence that restores the cursor
19 // position.
20 //
21 // ESC 8
22 //
23 // See: https://vt100.net/docs/vt510-rm/DECRC.html
24 const (
25 RestoreCursor = "\x1b8"
26 DECRC = RestoreCursor
27 )
28
29 // RequestCursorPosition is an escape sequence that requests the current cursor
30 // position.
31 //
32 // CSI 6 n
33 //
34 // The terminal will report the cursor position as a CSI sequence in the
35 // following format:
36 //
37 // CSI Pl ; Pc R
38 //
39 // Where Pl is the line number and Pc is the column number.
40 // See: https://vt100.net/docs/vt510-rm/CPR.html
41 //
42 // Deprecated: use [RequestCursorPositionReport] instead.
43 const RequestCursorPosition = "\x1b[6n"
44
45 // RequestExtendedCursorPosition (DECXCPR) is a sequence for requesting the
46 // cursor position report including the current page number.
47 //
48 // CSI ? 6 n
49 //
50 // The terminal will report the cursor position as a CSI sequence in the
51 // following format:
52 //
53 // CSI ? Pl ; Pc ; Pp R
54 //
55 // Where Pl is the line number, Pc is the column number, and Pp is the page
56 // number.
57 // See: https://vt100.net/docs/vt510-rm/DECXCPR.html
58 //
59 // Deprecated: use [RequestExtendedCursorPositionReport] instead.
60 const RequestExtendedCursorPosition = "\x1b[?6n"
61
62 // CursorUp (CUU) returns a sequence for moving the cursor up n cells.
63 //
64 // CSI n A
65 //
66 // See: https://vt100.net/docs/vt510-rm/CUU.html
67 func CursorUp(n int) string {
68 var s string
69 if n > 1 {
70 s = strconv.Itoa(n)
71 }
72 return "\x1b[" + s + "A"
73 }
74
75 // CUU is an alias for [CursorUp].
76 func CUU(n int) string {
77 return CursorUp(n)
78 }
79
80 // CUU1 is a sequence for moving the cursor up one cell.
81 const CUU1 = "\x1b[A"
82
83 // CursorUp1 is a sequence for moving the cursor up one cell.
84 //
85 // This is equivalent to CursorUp(1).
86 //
87 // Deprecated: use [CUU1] instead.
88 const CursorUp1 = "\x1b[A"
89
90 // CursorDown (CUD) returns a sequence for moving the cursor down n cells.
91 //
92 // CSI n B
93 //
94 // See: https://vt100.net/docs/vt510-rm/CUD.html
95 func CursorDown(n int) string {
96 var s string
97 if n > 1 {
98 s = strconv.Itoa(n)
99 }
100 return "\x1b[" + s + "B"
101 }
102
103 // CUD is an alias for [CursorDown].
104 func CUD(n int) string {
105 return CursorDown(n)
106 }
107
108 // CUD1 is a sequence for moving the cursor down one cell.
109 const CUD1 = "\x1b[B"
110
111 // CursorDown1 is a sequence for moving the cursor down one cell.
112 //
113 // This is equivalent to CursorDown(1).
114 //
115 // Deprecated: use [CUD1] instead.
116 const CursorDown1 = "\x1b[B"
117
118 // CursorForward (CUF) returns a sequence for moving the cursor right n cells.
119 //
120 // # CSI n C
121 //
122 // See: https://vt100.net/docs/vt510-rm/CUF.html
123 func CursorForward(n int) string {
124 var s string
125 if n > 1 {
126 s = strconv.Itoa(n)
127 }
128 return "\x1b[" + s + "C"
129 }
130
131 // CUF is an alias for [CursorForward].
132 func CUF(n int) string {
133 return CursorForward(n)
134 }
135
136 // CUF1 is a sequence for moving the cursor right one cell.
137 const CUF1 = "\x1b[C"
138
139 // CursorRight (CUF) returns a sequence for moving the cursor right n cells.
140 //
141 // CSI n C
142 //
143 // See: https://vt100.net/docs/vt510-rm/CUF.html
144 //
145 // Deprecated: use [CursorForward] instead.
146 func CursorRight(n int) string {
147 return CursorForward(n)
148 }
149
150 // CursorRight1 is a sequence for moving the cursor right one cell.
151 //
152 // This is equivalent to CursorRight(1).
153 //
154 // Deprecated: use [CUF1] instead.
155 const CursorRight1 = CUF1
156
157 // CursorBackward (CUB) returns a sequence for moving the cursor left n cells.
158 //
159 // # CSI n D
160 //
161 // See: https://vt100.net/docs/vt510-rm/CUB.html
162 func CursorBackward(n int) string {
163 var s string
164 if n > 1 {
165 s = strconv.Itoa(n)
166 }
167 return "\x1b[" + s + "D"
168 }
169
170 // CUB is an alias for [CursorBackward].
171 func CUB(n int) string {
172 return CursorBackward(n)
173 }
174
175 // CUB1 is a sequence for moving the cursor left one cell.
176 const CUB1 = "\x1b[D"
177
178 // CursorLeft (CUB) returns a sequence for moving the cursor left n cells.
179 //
180 // CSI n D
181 //
182 // See: https://vt100.net/docs/vt510-rm/CUB.html
183 //
184 // Deprecated: use [CursorBackward] instead.
185 func CursorLeft(n int) string {
186 return CursorBackward(n)
187 }
188
189 // CursorLeft1 is a sequence for moving the cursor left one cell.
190 //
191 // This is equivalent to CursorLeft(1).
192 //
193 // Deprecated: use [CUB1] instead.
194 const CursorLeft1 = CUB1
195
196 // CursorNextLine (CNL) returns a sequence for moving the cursor to the
197 // beginning of the next line n times.
198 //
199 // CSI n E
200 //
201 // See: https://vt100.net/docs/vt510-rm/CNL.html
202 func CursorNextLine(n int) string {
203 var s string
204 if n > 1 {
205 s = strconv.Itoa(n)
206 }
207 return "\x1b[" + s + "E"
208 }
209
210 // CNL is an alias for [CursorNextLine].
211 func CNL(n int) string {
212 return CursorNextLine(n)
213 }
214
215 // CursorPreviousLine (CPL) returns a sequence for moving the cursor to the
216 // beginning of the previous line n times.
217 //
218 // CSI n F
219 //
220 // See: https://vt100.net/docs/vt510-rm/CPL.html
221 func CursorPreviousLine(n int) string {
222 var s string
223 if n > 1 {
224 s = strconv.Itoa(n)
225 }
226 return "\x1b[" + s + "F"
227 }
228
229 // CPL is an alias for [CursorPreviousLine].
230 func CPL(n int) string {
231 return CursorPreviousLine(n)
232 }
233
234 // CursorHorizontalAbsolute (CHA) returns a sequence for moving the cursor to
235 // the given column.
236 //
237 // Default is 1.
238 //
239 // CSI n G
240 //
241 // See: https://vt100.net/docs/vt510-rm/CHA.html
242 func CursorHorizontalAbsolute(col int) string {
243 var s string
244 if col > 0 {
245 s = strconv.Itoa(col)
246 }
247 return "\x1b[" + s + "G"
248 }
249
250 // CHA is an alias for [CursorHorizontalAbsolute].
251 func CHA(col int) string {
252 return CursorHorizontalAbsolute(col)
253 }
254
255 // CursorPosition (CUP) returns a sequence for setting the cursor to the
256 // given row and column.
257 //
258 // Default is 1,1.
259 //
260 // CSI n ; m H
261 //
262 // See: https://vt100.net/docs/vt510-rm/CUP.html
263 func CursorPosition(col, row int) string {
264 if row <= 0 && col <= 0 {
265 return CursorHomePosition
266 }
267
268 var r, c string
269 if row > 0 {
270 r = strconv.Itoa(row)
271 }
272 if col > 0 {
273 c = strconv.Itoa(col)
274 }
275 return "\x1b[" + r + ";" + c + "H"
276 }
277
278 // CUP is an alias for [CursorPosition].
279 func CUP(col, row int) string {
280 return CursorPosition(col, row)
281 }
282
283 // CursorHomePosition is a sequence for moving the cursor to the upper left
284 // corner of the scrolling region. This is equivalent to `CursorPosition(1, 1)`.
285 const CursorHomePosition = "\x1b[H"
286
287 // SetCursorPosition (CUP) returns a sequence for setting the cursor to the
288 // given row and column.
289 //
290 // CSI n ; m H
291 //
292 // See: https://vt100.net/docs/vt510-rm/CUP.html
293 //
294 // Deprecated: use [CursorPosition] instead.
295 func SetCursorPosition(col, row int) string {
296 if row <= 0 && col <= 0 {
297 return HomeCursorPosition
298 }
299
300 var r, c string
301 if row > 0 {
302 r = strconv.Itoa(row)
303 }
304 if col > 0 {
305 c = strconv.Itoa(col)
306 }
307 return "\x1b[" + r + ";" + c + "H"
308 }
309
310 // HomeCursorPosition is a sequence for moving the cursor to the upper left
311 // corner of the scrolling region. This is equivalent to `SetCursorPosition(1, 1)`.
312 //
313 // Deprecated: use [CursorHomePosition] instead.
314 const HomeCursorPosition = CursorHomePosition
315
316 // MoveCursor (CUP) returns a sequence for setting the cursor to the
317 // given row and column.
318 //
319 // CSI n ; m H
320 //
321 // See: https://vt100.net/docs/vt510-rm/CUP.html
322 //
323 // Deprecated: use [CursorPosition] instead.
324 func MoveCursor(col, row int) string {
325 return SetCursorPosition(col, row)
326 }
327
328 // CursorOrigin is a sequence for moving the cursor to the upper left corner of
329 // the display. This is equivalent to `SetCursorPosition(1, 1)`.
330 //
331 // Deprecated: use [CursorHomePosition] instead.
332 const CursorOrigin = "\x1b[1;1H"
333
334 // MoveCursorOrigin is a sequence for moving the cursor to the upper left
335 // corner of the display. This is equivalent to `SetCursorPosition(1, 1)`.
336 //
337 // Deprecated: use [CursorHomePosition] instead.
338 const MoveCursorOrigin = CursorOrigin
339
340 // CursorHorizontalForwardTab (CHT) returns a sequence for moving the cursor to
341 // the next tab stop n times.
342 //
343 // Default is 1.
344 //
345 // CSI n I
346 //
347 // See: https://vt100.net/docs/vt510-rm/CHT.html
348 func CursorHorizontalForwardTab(n int) string {
349 var s string
350 if n > 1 {
351 s = strconv.Itoa(n)
352 }
353 return "\x1b[" + s + "I"
354 }
355
356 // CHT is an alias for [CursorHorizontalForwardTab].
357 func CHT(n int) string {
358 return CursorHorizontalForwardTab(n)
359 }
360
361 // EraseCharacter (ECH) returns a sequence for erasing n characters from the
362 // screen. This doesn't affect other cell attributes.
363 //
364 // Default is 1.
365 //
366 // CSI n X
367 //
368 // See: https://vt100.net/docs/vt510-rm/ECH.html
369 func EraseCharacter(n int) string {
370 var s string
371 if n > 1 {
372 s = strconv.Itoa(n)
373 }
374 return "\x1b[" + s + "X"
375 }
376
377 // ECH is an alias for [EraseCharacter].
378 func ECH(n int) string {
379 return EraseCharacter(n)
380 }
381
382 // CursorBackwardTab (CBT) returns a sequence for moving the cursor to the
383 // previous tab stop n times.
384 //
385 // Default is 1.
386 //
387 // CSI n Z
388 //
389 // See: https://vt100.net/docs/vt510-rm/CBT.html
390 func CursorBackwardTab(n int) string {
391 var s string
392 if n > 1 {
393 s = strconv.Itoa(n)
394 }
395 return "\x1b[" + s + "Z"
396 }
397
398 // CBT is an alias for [CursorBackwardTab].
399 func CBT(n int) string {
400 return CursorBackwardTab(n)
401 }
402
403 // VerticalPositionAbsolute (VPA) returns a sequence for moving the cursor to
404 // the given row.
405 //
406 // Default is 1.
407 //
408 // CSI n d
409 //
410 // See: https://vt100.net/docs/vt510-rm/VPA.html
411 func VerticalPositionAbsolute(row int) string {
412 var s string
413 if row > 0 {
414 s = strconv.Itoa(row)
415 }
416 return "\x1b[" + s + "d"
417 }
418
419 // VPA is an alias for [VerticalPositionAbsolute].
420 func VPA(row int) string {
421 return VerticalPositionAbsolute(row)
422 }
423
424 // VerticalPositionRelative (VPR) returns a sequence for moving the cursor down
425 // n rows relative to the current position.
426 //
427 // Default is 1.
428 //
429 // CSI n e
430 //
431 // See: https://vt100.net/docs/vt510-rm/VPR.html
432 func VerticalPositionRelative(n int) string {
433 var s string
434 if n > 1 {
435 s = strconv.Itoa(n)
436 }
437 return "\x1b[" + s + "e"
438 }
439
440 // VPR is an alias for [VerticalPositionRelative].
441 func VPR(n int) string {
442 return VerticalPositionRelative(n)
443 }
444
445 // HorizontalVerticalPosition (HVP) returns a sequence for moving the cursor to
446 // the given row and column.
447 //
448 // Default is 1,1.
449 //
450 // CSI n ; m f
451 //
452 // This has the same effect as [CursorPosition].
453 //
454 // See: https://vt100.net/docs/vt510-rm/HVP.html
455 func HorizontalVerticalPosition(col, row int) string {
456 var r, c string
457 if row > 0 {
458 r = strconv.Itoa(row)
459 }
460 if col > 0 {
461 c = strconv.Itoa(col)
462 }
463 return "\x1b[" + r + ";" + c + "f"
464 }
465
466 // HVP is an alias for [HorizontalVerticalPosition].
467 func HVP(col, row int) string {
468 return HorizontalVerticalPosition(col, row)
469 }
470
471 // HorizontalVerticalHomePosition is a sequence for moving the cursor to the
472 // upper left corner of the scrolling region. This is equivalent to
473 // `HorizontalVerticalPosition(1, 1)`.
474 const HorizontalVerticalHomePosition = "\x1b[f"
475
476 // SaveCurrentCursorPosition (SCOSC) is a sequence for saving the current cursor
477 // position for SCO console mode.
478 //
479 // CSI s
480 //
481 // This acts like [DECSC], except the page number where the cursor is located
482 // is not saved.
483 //
484 // See: https://vt100.net/docs/vt510-rm/SCOSC.html
485 const (
486 SaveCurrentCursorPosition = "\x1b[s"
487 SCOSC = SaveCurrentCursorPosition
488 )
489
490 // SaveCursorPosition (SCP or SCOSC) is a sequence for saving the cursor
491 // position.
492 //
493 // CSI s
494 //
495 // This acts like Save, except the page number where the cursor is located is
496 // not saved.
497 //
498 // See: https://vt100.net/docs/vt510-rm/SCOSC.html
499 //
500 // Deprecated: use [SaveCurrentCursorPosition] instead.
501 const SaveCursorPosition = "\x1b[s"
502
503 // RestoreCurrentCursorPosition (SCORC) is a sequence for restoring the current
504 // cursor position for SCO console mode.
505 //
506 // CSI u
507 //
508 // This acts like [DECRC], except the page number where the cursor was saved is
509 // not restored.
510 //
511 // See: https://vt100.net/docs/vt510-rm/SCORC.html
512 const (
513 RestoreCurrentCursorPosition = "\x1b[u"
514 SCORC = RestoreCurrentCursorPosition
515 )
516
517 // RestoreCursorPosition (RCP or SCORC) is a sequence for restoring the cursor
518 // position.
519 //
520 // CSI u
521 //
522 // This acts like Restore, except the cursor stays on the same page where the
523 // cursor was saved.
524 //
525 // See: https://vt100.net/docs/vt510-rm/SCORC.html
526 //
527 // Deprecated: use [RestoreCurrentCursorPosition] instead.
528 const RestoreCursorPosition = "\x1b[u"
529
530 // SetCursorStyle (DECSCUSR) returns a sequence for changing the cursor style.
531 //
532 // Default is 1.
533 //
534 // CSI Ps SP q
535 //
536 // Where Ps is the cursor style:
537 //
538 // 0: Blinking block
539 // 1: Blinking block (default)
540 // 2: Steady block
541 // 3: Blinking underline
542 // 4: Steady underline
543 // 5: Blinking bar (xterm)
544 // 6: Steady bar (xterm)
545 //
546 // See: https://vt100.net/docs/vt510-rm/DECSCUSR.html
547 // See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h4-Functions-using-CSI-_-ordered-by-the-final-character-lparen-s-rparen:CSI-Ps-SP-q.1D81
548 func SetCursorStyle(style int) string {
549 if style < 0 {
550 style = 0
551 }
552 return "\x1b[" + strconv.Itoa(style) + " q"
553 }
554
555 // DECSCUSR is an alias for [SetCursorStyle].
556 func DECSCUSR(style int) string {
557 return SetCursorStyle(style)
558 }
559
560 // SetPointerShape returns a sequence for changing the mouse pointer cursor
561 // shape. Use "default" for the default pointer shape.
562 //
563 // OSC 22 ; Pt ST
564 // OSC 22 ; Pt BEL
565 //
566 // Where Pt is the pointer shape name. The name can be anything that the
567 // operating system can understand. Some common names are:
568 //
569 // - copy
570 // - crosshair
571 // - default
572 // - ew-resize
573 // - n-resize
574 // - text
575 // - wait
576 //
577 // See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Operating-System-Commands
578 func SetPointerShape(shape string) string {
579 return "\x1b]22;" + shape + "\x07"
580 }
581
582 // ReverseIndex (RI) is an escape sequence for moving the cursor up one line in
583 // the same column. If the cursor is at the top margin, the screen scrolls
584 // down.
585 //
586 // This has the same effect as [RI].
587 const ReverseIndex = "\x1bM"
588
589 // HorizontalPositionAbsolute (HPA) returns a sequence for moving the cursor to
590 // the given column. This has the same effect as [CUP].
591 //
592 // Default is 1.
593 //
594 // CSI n \`
595 //
596 // See: https://vt100.net/docs/vt510-rm/HPA.html
597 func HorizontalPositionAbsolute(col int) string {
598 var s string
599 if col > 0 {
600 s = strconv.Itoa(col)
601 }
602 return "\x1b[" + s + "`"
603 }
604
605 // HPA is an alias for [HorizontalPositionAbsolute].
606 func HPA(col int) string {
607 return HorizontalPositionAbsolute(col)
608 }
609
610 // HorizontalPositionRelative (HPR) returns a sequence for moving the cursor
611 // right n columns relative to the current position. This has the same effect
612 // as [CUP].
613 //
614 // Default is 1.
615 //
616 // CSI n a
617 //
618 // See: https://vt100.net/docs/vt510-rm/HPR.html
619 func HorizontalPositionRelative(n int) string {
620 var s string
621 if n > 0 {
622 s = strconv.Itoa(n)
623 }
624 return "\x1b[" + s + "a"
625 }
626
627 // HPR is an alias for [HorizontalPositionRelative].
628 func HPR(n int) string {
629 return HorizontalPositionRelative(n)
630 }
631
632 // Index (IND) is an escape sequence for moving the cursor down one line in the
633 // same column. If the cursor is at the bottom margin, the screen scrolls up.
634 // This has the same effect as [IND].
635 const Index = "\x1bD"