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