vendor/github.com/charmbracelet/x/ansi/screen.go
126 lines · 2801 bytes
1package ansi
2
3import "strconv"
4
5// EraseDisplay (ED) clears the screen or parts of the screen. Possible values:
6//
7// 0: Clear from cursor to end of screen.
8// 1: Clear from cursor to beginning of the screen.
9// 2: Clear entire screen (and moves cursor to upper left on DOS).
10// 3: Clear entire screen and delete all lines saved in the scrollback buffer.
11//
12// CSI <n> J
13//
14// See: https://vt100.net/docs/vt510-rm/ED.html
15func EraseDisplay(n int) string {
16 if n < 0 {
17 n = 0
18 }
19 return "\x1b[" + strconv.Itoa(n) + "J"
20}
21
22// EraseDisplay constants.
23// These are the possible values for the EraseDisplay function.
24const (
25 EraseDisplayRight = "\x1b[0J"
26 EraseDisplayLeft = "\x1b[1J"
27 EraseEntireDisplay = "\x1b[2J"
28)
29
30// EraseLine (EL) clears the current line or parts of the line. Possible values:
31//
32// 0: Clear from cursor to end of line.
33// 1: Clear from cursor to beginning of the line.
34// 2: Clear entire line.
35//
36// The cursor position is not affected.
37//
38// CSI <n> K
39//
40// See: https://vt100.net/docs/vt510-rm/EL.html
41func EraseLine(n int) string {
42 if n < 0 {
43 n = 0
44 }
45 return "\x1b[" + strconv.Itoa(n) + "K"
46}
47
48// EraseLine constants.
49// These are the possible values for the EraseLine function.
50const (
51 EraseLineRight = "\x1b[0K"
52 EraseLineLeft = "\x1b[1K"
53 EraseEntireLine = "\x1b[2K"
54)
55
56// ScrollUp (SU) scrolls the screen up n lines. New lines are added at the
57// bottom of the screen.
58//
59// CSI <n> S
60//
61// See: https://vt100.net/docs/vt510-rm/SU.html
62func ScrollUp(n int) string {
63 var s string
64 if n > 1 {
65 s = strconv.Itoa(n)
66 }
67 return "\x1b[" + s + "S"
68}
69
70// ScrollDown (SD) scrolls the screen down n lines. New lines are added at the
71// top of the screen.
72//
73// CSI <n> T
74//
75// See: https://vt100.net/docs/vt510-rm/SD.html
76func ScrollDown(n int) string {
77 var s string
78 if n > 1 {
79 s = strconv.Itoa(n)
80 }
81 return "\x1b[" + s + "T"
82}
83
84// InsertLine (IL) inserts n blank lines at the current cursor position.
85// Existing lines are moved down.
86//
87// CSI <n> L
88//
89// See: https://vt100.net/docs/vt510-rm/IL.html
90func InsertLine(n int) string {
91 var s string
92 if n > 1 {
93 s = strconv.Itoa(n)
94 }
95 return "\x1b[" + s + "L"
96}
97
98// DeleteLine (DL) deletes n lines at the current cursor position. Existing
99// lines are moved up.
100//
101// CSI <n> M
102//
103// See: https://vt100.net/docs/vt510-rm/DL.html
104func DeleteLine(n int) string {
105 var s string
106 if n > 1 {
107 s = strconv.Itoa(n)
108 }
109 return "\x1b[" + s + "M"
110}
111
112// SetScrollingRegion (DECSTBM) sets the top and bottom margins for the scrolling
113// region. The default is the entire screen.
114//
115// CSI <top> ; <bottom> r
116//
117// See: https://vt100.net/docs/vt510-rm/DECSTBM.html
118func SetScrollingRegion(t, b int) string {
119 if t < 0 {
120 t = 0
121 }
122 if b < 0 {
123 b = 0
124 }
125 return "\x1b[" + strconv.Itoa(t) + ";" + strconv.Itoa(b) + "r"
126}