vendor/github.com/charmbracelet/x/ansi/cursor.go

235 lines · 5719 bytes

  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 SaveCursor = "\x1b7"
 12
 13// RestoreCursor (DECRC) is an escape sequence that restores the cursor
 14// position.
 15//
 16//	ESC 8
 17//
 18// See: https://vt100.net/docs/vt510-rm/DECRC.html
 19const RestoreCursor = "\x1b8"
 20
 21// RequestCursorPosition (CPR) is an escape sequence that requests the current
 22// cursor position.
 23//
 24//	CSI 6 n
 25//
 26// The terminal will report the cursor position as a CSI sequence in the
 27// following format:
 28//
 29//	CSI Pl ; Pc R
 30//
 31// Where Pl is the line number and Pc is the column number.
 32// See: https://vt100.net/docs/vt510-rm/CPR.html
 33const RequestCursorPosition = "\x1b[6n"
 34
 35// RequestExtendedCursorPosition (DECXCPR) is a sequence for requesting the
 36// cursor position report including the current page number.
 37//
 38//	CSI ? 6 n
 39//
 40// The terminal will report the cursor position as a CSI sequence in the
 41// following format:
 42//
 43//	CSI ? Pl ; Pc ; Pp R
 44//
 45// Where Pl is the line number, Pc is the column number, and Pp is the page
 46// number.
 47// See: https://vt100.net/docs/vt510-rm/DECXCPR.html
 48const RequestExtendedCursorPosition = "\x1b[?6n"
 49
 50// CursorUp (CUU) returns a sequence for moving the cursor up n cells.
 51//
 52//	CSI n A
 53//
 54// See: https://vt100.net/docs/vt510-rm/CUU.html
 55func CursorUp(n int) string {
 56	var s string
 57	if n > 1 {
 58		s = strconv.Itoa(n)
 59	}
 60	return "\x1b[" + s + "A"
 61}
 62
 63// CursorUp1 is a sequence for moving the cursor up one cell.
 64//
 65// This is equivalent to CursorUp(1).
 66const CursorUp1 = "\x1b[A"
 67
 68// CursorDown (CUD) returns a sequence for moving the cursor down n cells.
 69//
 70//	CSI n B
 71//
 72// See: https://vt100.net/docs/vt510-rm/CUD.html
 73func CursorDown(n int) string {
 74	var s string
 75	if n > 1 {
 76		s = strconv.Itoa(n)
 77	}
 78	return "\x1b[" + s + "B"
 79}
 80
 81// CursorDown1 is a sequence for moving the cursor down one cell.
 82//
 83// This is equivalent to CursorDown(1).
 84const CursorDown1 = "\x1b[B"
 85
 86// CursorRight (CUF) returns a sequence for moving the cursor right n cells.
 87//
 88//	CSI n C
 89//
 90// See: https://vt100.net/docs/vt510-rm/CUF.html
 91func CursorRight(n int) string {
 92	var s string
 93	if n > 1 {
 94		s = strconv.Itoa(n)
 95	}
 96	return "\x1b[" + s + "C"
 97}
 98
 99// CursorRight1 is a sequence for moving the cursor right one cell.
100//
101// This is equivalent to CursorRight(1).
102const CursorRight1 = "\x1b[C"
103
104// CursorLeft (CUB) returns a sequence for moving the cursor left n cells.
105//
106//	CSI n D
107//
108// See: https://vt100.net/docs/vt510-rm/CUB.html
109func CursorLeft(n int) string {
110	var s string
111	if n > 1 {
112		s = strconv.Itoa(n)
113	}
114	return "\x1b[" + s + "D"
115}
116
117// CursorLeft1 is a sequence for moving the cursor left one cell.
118//
119// This is equivalent to CursorLeft(1).
120const CursorLeft1 = "\x1b[D"
121
122// CursorNextLine (CNL) returns a sequence for moving the cursor to the
123// beginning of the next line n times.
124//
125//	CSI n E
126//
127// See: https://vt100.net/docs/vt510-rm/CNL.html
128func CursorNextLine(n int) string {
129	var s string
130	if n > 1 {
131		s = strconv.Itoa(n)
132	}
133	return "\x1b[" + s + "E"
134}
135
136// CursorPreviousLine (CPL) returns a sequence for moving the cursor to the
137// beginning of the previous line n times.
138//
139//	CSI n F
140//
141// See: https://vt100.net/docs/vt510-rm/CPL.html
142func CursorPreviousLine(n int) string {
143	var s string
144	if n > 1 {
145		s = strconv.Itoa(n)
146	}
147	return "\x1b[" + s + "F"
148}
149
150// MoveCursor (CUP) returns a sequence for moving the cursor to the given row
151// and column.
152//
153//	CSI n ; m H
154//
155// See: https://vt100.net/docs/vt510-rm/CUP.html
156func MoveCursor(row, col int) string {
157	if row < 0 {
158		row = 0
159	}
160	if col < 0 {
161		col = 0
162	}
163	return "\x1b[" + strconv.Itoa(row) + ";" + strconv.Itoa(col) + "H"
164}
165
166// MoveCursorOrigin is a sequence for moving the cursor to the upper left
167// corner of the screen. This is equivalent to MoveCursor(1, 1).
168const MoveCursorOrigin = "\x1b[1;1H"
169
170// SaveCursorPosition (SCP or SCOSC) is a sequence for saving the cursor
171// position.
172//
173//	CSI s
174//
175// This acts like Save, except the page number where the cursor is located is
176// not saved.
177//
178// See: https://vt100.net/docs/vt510-rm/SCOSC.html
179const SaveCursorPosition = "\x1b[s"
180
181// RestoreCursorPosition (RCP or SCORC) is a sequence for restoring the cursor
182// position.
183//
184//	CSI u
185//
186// This acts like Restore, except the cursor stays on the same page where the
187// cursor was saved.
188//
189// See: https://vt100.net/docs/vt510-rm/SCORC.html
190const RestoreCursorPosition = "\x1b[u"
191
192// SetCursorStyle (DECSCUSR) returns a sequence for changing the cursor style.
193//
194//	CSI Ps SP q
195//
196// Where Ps is the cursor style:
197//
198//	0: Blinking block
199//	1: Blinking block (default)
200//	2: Steady block
201//	3: Blinking underline
202//	4: Steady underline
203//	5: Blinking bar (xterm)
204//	6: Steady bar (xterm)
205//
206// See: https://vt100.net/docs/vt510-rm/DECSCUSR.html
207// 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
208func SetCursorStyle(style int) string {
209	if style < 0 {
210		style = 0
211	}
212	return "\x1b[" + strconv.Itoa(style) + " q"
213}
214
215// SetPointerShape returns a sequence for changing the mouse pointer cursor
216// shape. Use "default" for the default pointer shape.
217//
218//	OSC 22 ; Pt ST
219//	OSC 22 ; Pt BEL
220//
221// Where Pt is the pointer shape name. The name can be anything that the
222// operating system can understand. Some common names are:
223//
224//   - copy
225//   - crosshair
226//   - default
227//   - ew-resize
228//   - n-resize
229//   - text
230//   - wait
231//
232// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Operating-System-Commands
233func SetPointerShape(shape string) string {
234	return "\x1b]22;" + shape + "\x07"
235}