1package termenv
2
3import (
4 "fmt"
5 "strings"
6)
7
8// Sequence definitions.
9const (
10 // Cursor positioning.
11 CursorUpSeq = "%dA"
12 CursorDownSeq = "%dB"
13 CursorForwardSeq = "%dC"
14 CursorBackSeq = "%dD"
15 CursorNextLineSeq = "%dE"
16 CursorPreviousLineSeq = "%dF"
17 CursorHorizontalSeq = "%dG"
18 CursorPositionSeq = "%d;%dH"
19 EraseDisplaySeq = "%dJ"
20 EraseLineSeq = "%dK"
21 ScrollUpSeq = "%dS"
22 ScrollDownSeq = "%dT"
23 SaveCursorPositionSeq = "s"
24 RestoreCursorPositionSeq = "u"
25 ChangeScrollingRegionSeq = "%d;%dr"
26 InsertLineSeq = "%dL"
27 DeleteLineSeq = "%dM"
28
29 // Explicit values for EraseLineSeq.
30 EraseLineRightSeq = "0K"
31 EraseLineLeftSeq = "1K"
32 EraseEntireLineSeq = "2K"
33
34 // Mouse.
35 EnableMousePressSeq = "?9h" // press only (X10)
36 DisableMousePressSeq = "?9l"
37 EnableMouseSeq = "?1000h" // press, release, wheel
38 DisableMouseSeq = "?1000l"
39 EnableMouseHiliteSeq = "?1001h" // highlight
40 DisableMouseHiliteSeq = "?1001l"
41 EnableMouseCellMotionSeq = "?1002h" // press, release, move on pressed, wheel
42 DisableMouseCellMotionSeq = "?1002l"
43 EnableMouseAllMotionSeq = "?1003h" // press, release, move, wheel
44 DisableMouseAllMotionSeq = "?1003l"
45 EnableMouseExtendedModeSeq = "?1006h" // press, release, move, wheel, extended coordinates
46 DisableMouseExtendedModeSeq = "?1006l"
47 EnableMousePixelsModeSeq = "?1016h" // press, release, move, wheel, extended pixel coordinates
48 DisableMousePixelsModeSeq = "?1016l"
49
50 // Screen.
51 RestoreScreenSeq = "?47l"
52 SaveScreenSeq = "?47h"
53 AltScreenSeq = "?1049h"
54 ExitAltScreenSeq = "?1049l"
55
56 // Bracketed paste.
57 // https://en.wikipedia.org/wiki/Bracketed-paste
58 EnableBracketedPasteSeq = "?2004h"
59 DisableBracketedPasteSeq = "?2004l"
60 StartBracketedPasteSeq = "200~"
61 EndBracketedPasteSeq = "201~"
62
63 // Session.
64 SetWindowTitleSeq = "2;%s" + string(BEL)
65 SetForegroundColorSeq = "10;%s" + string(BEL)
66 SetBackgroundColorSeq = "11;%s" + string(BEL)
67 SetCursorColorSeq = "12;%s" + string(BEL)
68 ShowCursorSeq = "?25h"
69 HideCursorSeq = "?25l"
70)
71
72// Reset the terminal to its default style, removing any active styles.
73func (o Output) Reset() {
74 fmt.Fprint(o.w, CSI+ResetSeq+"m") //nolint:errcheck
75}
76
77// SetForegroundColor sets the default foreground color.
78func (o Output) SetForegroundColor(color Color) {
79 fmt.Fprintf(o.w, OSC+SetForegroundColorSeq, color) //nolint:errcheck
80}
81
82// SetBackgroundColor sets the default background color.
83func (o Output) SetBackgroundColor(color Color) {
84 fmt.Fprintf(o.w, OSC+SetBackgroundColorSeq, color) //nolint:errcheck
85}
86
87// SetCursorColor sets the cursor color.
88func (o Output) SetCursorColor(color Color) {
89 fmt.Fprintf(o.w, OSC+SetCursorColorSeq, color) //nolint:errcheck
90}
91
92// RestoreScreen restores a previously saved screen state.
93func (o Output) RestoreScreen() {
94 fmt.Fprint(o.w, CSI+RestoreScreenSeq) //nolint:errcheck
95}
96
97// SaveScreen saves the screen state.
98func (o Output) SaveScreen() {
99 fmt.Fprint(o.w, CSI+SaveScreenSeq) //nolint:errcheck
100}
101
102// AltScreen switches to the alternate screen buffer. The former view can be
103// restored with ExitAltScreen().
104func (o Output) AltScreen() {
105 fmt.Fprint(o.w, CSI+AltScreenSeq) //nolint:errcheck
106}
107
108// ExitAltScreen exits the alternate screen buffer and returns to the former
109// terminal view.
110func (o Output) ExitAltScreen() {
111 fmt.Fprint(o.w, CSI+ExitAltScreenSeq) //nolint:errcheck
112}
113
114// ClearScreen clears the visible portion of the terminal.
115func (o Output) ClearScreen() {
116 fmt.Fprintf(o.w, CSI+EraseDisplaySeq, 2) //nolint:errcheck,mnd
117 o.MoveCursor(1, 1)
118}
119
120// MoveCursor moves the cursor to a given position.
121func (o Output) MoveCursor(row int, column int) {
122 fmt.Fprintf(o.w, CSI+CursorPositionSeq, row, column) //nolint:errcheck
123}
124
125// HideCursor hides the cursor.
126func (o Output) HideCursor() {
127 fmt.Fprint(o.w, CSI+HideCursorSeq) //nolint:errcheck
128}
129
130// ShowCursor shows the cursor.
131func (o Output) ShowCursor() {
132 fmt.Fprint(o.w, CSI+ShowCursorSeq) //nolint:errcheck
133}
134
135// SaveCursorPosition saves the cursor position.
136func (o Output) SaveCursorPosition() {
137 fmt.Fprint(o.w, CSI+SaveCursorPositionSeq) //nolint:errcheck
138}
139
140// RestoreCursorPosition restores a saved cursor position.
141func (o Output) RestoreCursorPosition() {
142 fmt.Fprint(o.w, CSI+RestoreCursorPositionSeq) //nolint:errcheck
143}
144
145// CursorUp moves the cursor up a given number of lines.
146func (o Output) CursorUp(n int) {
147 fmt.Fprintf(o.w, CSI+CursorUpSeq, n) //nolint:errcheck
148}
149
150// CursorDown moves the cursor down a given number of lines.
151func (o Output) CursorDown(n int) {
152 fmt.Fprintf(o.w, CSI+CursorDownSeq, n) //nolint:errcheck
153}
154
155// CursorForward moves the cursor up a given number of lines.
156func (o Output) CursorForward(n int) {
157 fmt.Fprintf(o.w, CSI+CursorForwardSeq, n) //nolint:errcheck
158}
159
160// CursorBack moves the cursor backwards a given number of cells.
161func (o Output) CursorBack(n int) {
162 fmt.Fprintf(o.w, CSI+CursorBackSeq, n) //nolint:errcheck
163}
164
165// CursorNextLine moves the cursor down a given number of lines and places it at
166// the beginning of the line.
167func (o Output) CursorNextLine(n int) {
168 fmt.Fprintf(o.w, CSI+CursorNextLineSeq, n) //nolint:errcheck
169}
170
171// CursorPrevLine moves the cursor up a given number of lines and places it at
172// the beginning of the line.
173func (o Output) CursorPrevLine(n int) {
174 fmt.Fprintf(o.w, CSI+CursorPreviousLineSeq, n) //nolint:errcheck
175}
176
177// ClearLine clears the current line.
178func (o Output) ClearLine() {
179 fmt.Fprint(o.w, CSI+EraseEntireLineSeq) //nolint:errcheck
180}
181
182// ClearLineLeft clears the line to the left of the cursor.
183func (o Output) ClearLineLeft() {
184 fmt.Fprint(o.w, CSI+EraseLineLeftSeq) //nolint:errcheck
185}
186
187// ClearLineRight clears the line to the right of the cursor.
188func (o Output) ClearLineRight() {
189 fmt.Fprint(o.w, CSI+EraseLineRightSeq) //nolint:errcheck
190}
191
192// ClearLines clears a given number of lines.
193func (o Output) ClearLines(n int) {
194 clearLine := fmt.Sprintf(CSI+EraseLineSeq, 2) //nolint:mnd
195 cursorUp := fmt.Sprintf(CSI+CursorUpSeq, 1)
196 fmt.Fprint(o.w, clearLine+strings.Repeat(cursorUp+clearLine, n)) //nolint:errcheck
197}
198
199// ChangeScrollingRegion sets the scrolling region of the terminal.
200func (o Output) ChangeScrollingRegion(top, bottom int) {
201 fmt.Fprintf(o.w, CSI+ChangeScrollingRegionSeq, top, bottom) //nolint:errcheck
202}
203
204// InsertLines inserts the given number of lines at the top of the scrollable
205// region, pushing lines below down.
206func (o Output) InsertLines(n int) {
207 fmt.Fprintf(o.w, CSI+InsertLineSeq, n) //nolint:errcheck
208}
209
210// DeleteLines deletes the given number of lines, pulling any lines in
211// the scrollable region below up.
212func (o Output) DeleteLines(n int) {
213 fmt.Fprintf(o.w, CSI+DeleteLineSeq, n) //nolint:errcheck
214}
215
216// EnableMousePress enables X10 mouse mode. Button press events are sent only.
217func (o Output) EnableMousePress() {
218 fmt.Fprint(o.w, CSI+EnableMousePressSeq) //nolint:errcheck
219}
220
221// DisableMousePress disables X10 mouse mode.
222func (o Output) DisableMousePress() {
223 fmt.Fprint(o.w, CSI+DisableMousePressSeq) //nolint:errcheck
224}
225
226// EnableMouse enables Mouse Tracking mode.
227func (o Output) EnableMouse() {
228 fmt.Fprint(o.w, CSI+EnableMouseSeq) //nolint:errcheck
229}
230
231// DisableMouse disables Mouse Tracking mode.
232func (o Output) DisableMouse() {
233 fmt.Fprint(o.w, CSI+DisableMouseSeq) //nolint:errcheck
234}
235
236// EnableMouseHilite enables Hilite Mouse Tracking mode.
237func (o Output) EnableMouseHilite() {
238 fmt.Fprint(o.w, CSI+EnableMouseHiliteSeq) //nolint:errcheck
239}
240
241// DisableMouseHilite disables Hilite Mouse Tracking mode.
242func (o Output) DisableMouseHilite() {
243 fmt.Fprint(o.w, CSI+DisableMouseHiliteSeq) //nolint:errcheck
244}
245
246// EnableMouseCellMotion enables Cell Motion Mouse Tracking mode.
247func (o Output) EnableMouseCellMotion() {
248 fmt.Fprint(o.w, CSI+EnableMouseCellMotionSeq) //nolint:errcheck
249}
250
251// DisableMouseCellMotion disables Cell Motion Mouse Tracking mode.
252func (o Output) DisableMouseCellMotion() {
253 fmt.Fprint(o.w, CSI+DisableMouseCellMotionSeq) //nolint:errcheck
254}
255
256// EnableMouseAllMotion enables All Motion Mouse mode.
257func (o Output) EnableMouseAllMotion() {
258 fmt.Fprint(o.w, CSI+EnableMouseAllMotionSeq) //nolint:errcheck
259}
260
261// DisableMouseAllMotion disables All Motion Mouse mode.
262func (o Output) DisableMouseAllMotion() {
263 fmt.Fprint(o.w, CSI+DisableMouseAllMotionSeq) //nolint:errcheck
264}
265
266// EnableMouseExtendedMotion enables Extended Mouse mode (SGR). This should be
267// enabled in conjunction with EnableMouseCellMotion, and EnableMouseAllMotion.
268func (o Output) EnableMouseExtendedMode() {
269 fmt.Fprint(o.w, CSI+EnableMouseExtendedModeSeq) //nolint:errcheck
270}
271
272// DisableMouseExtendedMotion disables Extended Mouse mode (SGR).
273func (o Output) DisableMouseExtendedMode() {
274 fmt.Fprint(o.w, CSI+DisableMouseExtendedModeSeq) //nolint:errcheck
275}
276
277// EnableMousePixelsMotion enables Pixel Motion Mouse mode (SGR-Pixels). This
278// should be enabled in conjunction with EnableMouseCellMotion, and
279// EnableMouseAllMotion.
280func (o Output) EnableMousePixelsMode() {
281 fmt.Fprint(o.w, CSI+EnableMousePixelsModeSeq) //nolint:errcheck
282}
283
284// DisableMousePixelsMotion disables Pixel Motion Mouse mode (SGR-Pixels).
285func (o Output) DisableMousePixelsMode() {
286 fmt.Fprint(o.w, CSI+DisableMousePixelsModeSeq) //nolint:errcheck
287}
288
289// SetWindowTitle sets the terminal window title.
290func (o Output) SetWindowTitle(title string) {
291 fmt.Fprintf(o.w, OSC+SetWindowTitleSeq, title) //nolint:errcheck
292}
293
294// EnableBracketedPaste enables bracketed paste.
295func (o Output) EnableBracketedPaste() {
296 fmt.Fprintf(o.w, CSI+EnableBracketedPasteSeq) //nolint:errcheck
297}
298
299// DisableBracketedPaste disables bracketed paste.
300func (o Output) DisableBracketedPaste() {
301 fmt.Fprintf(o.w, CSI+DisableBracketedPasteSeq) //nolint:errcheck
302}
303
304// Legacy functions.
305
306// Reset the terminal to its default style, removing any active styles.
307//
308// Deprecated: please use termenv.Output instead.
309func Reset() {
310 output.Reset()
311}
312
313// SetForegroundColor sets the default foreground color.
314//
315// Deprecated: please use termenv.Output instead.
316func SetForegroundColor(color Color) {
317 output.SetForegroundColor(color)
318}
319
320// SetBackgroundColor sets the default background color.
321//
322// Deprecated: please use termenv.Output instead.
323func SetBackgroundColor(color Color) {
324 output.SetBackgroundColor(color)
325}
326
327// SetCursorColor sets the cursor color.
328//
329// Deprecated: please use termenv.Output instead.
330func SetCursorColor(color Color) {
331 output.SetCursorColor(color)
332}
333
334// RestoreScreen restores a previously saved screen state.
335//
336// Deprecated: please use termenv.Output instead.
337func RestoreScreen() {
338 output.RestoreScreen()
339}
340
341// SaveScreen saves the screen state.
342//
343// Deprecated: please use termenv.Output instead.
344func SaveScreen() {
345 output.SaveScreen()
346}
347
348// AltScreen switches to the alternate screen buffer. The former view can be
349// restored with ExitAltScreen().
350//
351// Deprecated: please use termenv.Output instead.
352func AltScreen() {
353 output.AltScreen()
354}
355
356// ExitAltScreen exits the alternate screen buffer and returns to the former
357// terminal view.
358//
359// Deprecated: please use termenv.Output instead.
360func ExitAltScreen() {
361 output.ExitAltScreen()
362}
363
364// ClearScreen clears the visible portion of the terminal.
365//
366// Deprecated: please use termenv.Output instead.
367func ClearScreen() {
368 output.ClearScreen()
369}
370
371// MoveCursor moves the cursor to a given position.
372//
373// Deprecated: please use termenv.Output instead.
374func MoveCursor(row int, column int) {
375 output.MoveCursor(row, column)
376}
377
378// HideCursor hides the cursor.
379//
380// Deprecated: please use termenv.Output instead.
381func HideCursor() {
382 output.HideCursor()
383}
384
385// ShowCursor shows the cursor.
386//
387// Deprecated: please use termenv.Output instead.
388func ShowCursor() {
389 output.ShowCursor()
390}
391
392// SaveCursorPosition saves the cursor position.
393//
394// Deprecated: please use termenv.Output instead.
395func SaveCursorPosition() {
396 output.SaveCursorPosition()
397}
398
399// RestoreCursorPosition restores a saved cursor position.
400//
401// Deprecated: please use termenv.Output instead.
402func RestoreCursorPosition() {
403 output.RestoreCursorPosition()
404}
405
406// CursorUp moves the cursor up a given number of lines.
407//
408// Deprecated: please use termenv.Output instead.
409func CursorUp(n int) {
410 output.CursorUp(n)
411}
412
413// CursorDown moves the cursor down a given number of lines.
414//
415// Deprecated: please use termenv.Output instead.
416func CursorDown(n int) {
417 output.CursorDown(n)
418}
419
420// CursorForward moves the cursor up a given number of lines.
421//
422// Deprecated: please use termenv.Output instead.
423func CursorForward(n int) {
424 output.CursorForward(n)
425}
426
427// CursorBack moves the cursor backwards a given number of cells.
428//
429// Deprecated: please use termenv.Output instead.
430func CursorBack(n int) {
431 output.CursorBack(n)
432}
433
434// CursorNextLine moves the cursor down a given number of lines and places it at
435// the beginning of the line.
436//
437// Deprecated: please use termenv.Output instead.
438func CursorNextLine(n int) {
439 output.CursorNextLine(n)
440}
441
442// CursorPrevLine moves the cursor up a given number of lines and places it at
443// the beginning of the line.
444//
445// Deprecated: please use termenv.Output instead.
446func CursorPrevLine(n int) {
447 output.CursorPrevLine(n)
448}
449
450// ClearLine clears the current line.
451//
452// Deprecated: please use termenv.Output instead.
453func ClearLine() {
454 output.ClearLine()
455}
456
457// ClearLineLeft clears the line to the left of the cursor.
458//
459// Deprecated: please use termenv.Output instead.
460func ClearLineLeft() {
461 output.ClearLineLeft()
462}
463
464// ClearLineRight clears the line to the right of the cursor.
465//
466// Deprecated: please use termenv.Output instead.
467func ClearLineRight() {
468 output.ClearLineRight()
469}
470
471// ClearLines clears a given number of lines.
472//
473// Deprecated: please use termenv.Output instead.
474func ClearLines(n int) {
475 output.ClearLines(n)
476}
477
478// ChangeScrollingRegion sets the scrolling region of the terminal.
479//
480// Deprecated: please use termenv.Output instead.
481func ChangeScrollingRegion(top, bottom int) {
482 output.ChangeScrollingRegion(top, bottom)
483}
484
485// InsertLines inserts the given number of lines at the top of the scrollable
486// region, pushing lines below down.
487//
488// Deprecated: please use termenv.Output instead.
489func InsertLines(n int) {
490 output.InsertLines(n)
491}
492
493// DeleteLines deletes the given number of lines, pulling any lines in
494// the scrollable region below up.
495//
496// Deprecated: please use termenv.Output instead.
497func DeleteLines(n int) {
498 output.DeleteLines(n)
499}
500
501// EnableMousePress enables X10 mouse mode. Button press events are sent only.
502//
503// Deprecated: please use termenv.Output instead.
504func EnableMousePress() {
505 output.EnableMousePress()
506}
507
508// DisableMousePress disables X10 mouse mode.
509//
510// Deprecated: please use termenv.Output instead.
511func DisableMousePress() {
512 output.DisableMousePress()
513}
514
515// EnableMouse enables Mouse Tracking mode.
516//
517// Deprecated: please use termenv.Output instead.
518func EnableMouse() {
519 output.EnableMouse()
520}
521
522// DisableMouse disables Mouse Tracking mode.
523//
524// Deprecated: please use termenv.Output instead.
525func DisableMouse() {
526 output.DisableMouse()
527}
528
529// EnableMouseHilite enables Hilite Mouse Tracking mode.
530//
531// Deprecated: please use termenv.Output instead.
532func EnableMouseHilite() {
533 output.EnableMouseHilite()
534}
535
536// DisableMouseHilite disables Hilite Mouse Tracking mode.
537//
538// Deprecated: please use termenv.Output instead.
539func DisableMouseHilite() {
540 output.DisableMouseHilite()
541}
542
543// EnableMouseCellMotion enables Cell Motion Mouse Tracking mode.
544//
545// Deprecated: please use termenv.Output instead.
546func EnableMouseCellMotion() {
547 output.EnableMouseCellMotion()
548}
549
550// DisableMouseCellMotion disables Cell Motion Mouse Tracking mode.
551//
552// Deprecated: please use termenv.Output instead.
553func DisableMouseCellMotion() {
554 output.DisableMouseCellMotion()
555}
556
557// EnableMouseAllMotion enables All Motion Mouse mode.
558//
559// Deprecated: please use termenv.Output instead.
560func EnableMouseAllMotion() {
561 output.EnableMouseAllMotion()
562}
563
564// DisableMouseAllMotion disables All Motion Mouse mode.
565//
566// Deprecated: please use termenv.Output instead.
567func DisableMouseAllMotion() {
568 output.DisableMouseAllMotion()
569}
570
571// SetWindowTitle sets the terminal window title.
572//
573// Deprecated: please use termenv.Output instead.
574func SetWindowTitle(title string) {
575 output.SetWindowTitle(title)
576}
577
578// EnableBracketedPaste enables bracketed paste.
579//
580// Deprecated: please use termenv.Output instead.
581func EnableBracketedPaste() {
582 output.EnableBracketedPaste()
583}
584
585// DisableBracketedPaste disables bracketed paste.
586//
587// Deprecated: please use termenv.Output instead.
588func DisableBracketedPaste() {
589 output.DisableBracketedPaste()
590}