Debian Codegraph @

ansi/status.go

raw
1 package ansi
2
3 import (
4 "strconv"
5 "strings"
6 )
7
8 // StatusReport represents a terminal status report.
9 type StatusReport interface {
10 // StatusReport returns the status report identifier.
11 StatusReport() int
12 }
13
14 // ANSIStatusReport represents an ANSI terminal status report.
15 type ANSIStatusReport int //nolint:revive
16
17 // StatusReport returns the status report identifier.
18 func (s ANSIStatusReport) StatusReport() int {
19 return int(s)
20 }
21
22 // DECStatusReport represents a DEC terminal status report.
23 type DECStatusReport int
24
25 // StatusReport returns the status report identifier.
26 func (s DECStatusReport) StatusReport() int {
27 return int(s)
28 }
29
30 // DeviceStatusReport (DSR) is a control sequence that reports the terminal's
31 // status.
32 // The terminal responds with a DSR sequence.
33 //
34 // CSI Ps n
35 // CSI ? Ps n
36 //
37 // If one of the statuses is a [DECStatus], the sequence will use the DEC
38 // format.
39 //
40 // See also https://vt100.net/docs/vt510-rm/DSR.html
41 func DeviceStatusReport(statues ...StatusReport) string {
42 var dec bool
43 list := make([]string, len(statues))
44 seq := "\x1b["
45 for i, status := range statues {
46 list[i] = strconv.Itoa(status.StatusReport())
47 switch status.(type) {
48 case DECStatusReport:
49 dec = true
50 }
51 }
52 if dec {
53 seq += "?"
54 }
55 return seq + strings.Join(list, ";") + "n"
56 }
57
58 // DSR is an alias for [DeviceStatusReport].
59 func DSR(status StatusReport) string {
60 return DeviceStatusReport(status)
61 }
62
63 // RequestCursorPositionReport is an escape sequence that requests the current
64 // cursor position.
65 //
66 // CSI 6 n
67 //
68 // The terminal will report the cursor position as a CSI sequence in the
69 // following format:
70 //
71 // CSI Pl ; Pc R
72 //
73 // Where Pl is the line number and Pc is the column number.
74 // See: https://vt100.net/docs/vt510-rm/CPR.html
75 const RequestCursorPositionReport = "\x1b[6n"
76
77 // RequestExtendedCursorPositionReport (DECXCPR) is a sequence for requesting
78 // the cursor position report including the current page number.
79 //
80 // CSI ? 6 n
81 //
82 // The terminal will report the cursor position as a CSI sequence in the
83 // following format:
84 //
85 // CSI ? Pl ; Pc ; Pp R
86 //
87 // Where Pl is the line number, Pc is the column number, and Pp is the page
88 // number.
89 // See: https://vt100.net/docs/vt510-rm/DECXCPR.html
90 const RequestExtendedCursorPositionReport = "\x1b[?6n"
91
92 // RequestLightDarkReport is a control sequence that requests the terminal to
93 // report its operating system light/dark color preference. Supported terminals
94 // should respond with a [LightDarkReport] sequence as follows:
95 //
96 // CSI ? 997 ; 1 n for dark mode
97 // CSI ? 997 ; 2 n for light mode
98 //
99 // See: https://contour-terminal.org/vt-extensions/color-palette-update-notifications/
100 const RequestLightDarkReport = "\x1b[?996n"
101
102 // CursorPositionReport (CPR) is a control sequence that reports the cursor's
103 // position.
104 //
105 // CSI Pl ; Pc R
106 //
107 // Where Pl is the line number and Pc is the column number.
108 //
109 // See also https://vt100.net/docs/vt510-rm/CPR.html
110 func CursorPositionReport(line, column int) string {
111 if line < 1 {
112 line = 1
113 }
114 if column < 1 {
115 column = 1
116 }
117 return "\x1b[" + strconv.Itoa(line) + ";" + strconv.Itoa(column) + "R"
118 }
119
120 // CPR is an alias for [CursorPositionReport].
121 func CPR(line, column int) string {
122 return CursorPositionReport(line, column)
123 }
124
125 // ExtendedCursorPositionReport (DECXCPR) is a control sequence that reports the
126 // cursor's position along with the page number (optional).
127 //
128 // CSI ? Pl ; Pc R
129 // CSI ? Pl ; Pc ; Pv R
130 //
131 // Where Pl is the line number, Pc is the column number, and Pv is the page
132 // number.
133 //
134 // If the page number is zero or negative, the returned sequence won't include
135 // the page number.
136 //
137 // See also https://vt100.net/docs/vt510-rm/DECXCPR.html
138 func ExtendedCursorPositionReport(line, column, page int) string {
139 if line < 1 {
140 line = 1
141 }
142 if column < 1 {
143 column = 1
144 }
145 if page < 1 {
146 return "\x1b[?" + strconv.Itoa(line) + ";" + strconv.Itoa(column) + "R"
147 }
148 return "\x1b[?" + strconv.Itoa(line) + ";" + strconv.Itoa(column) + ";" + strconv.Itoa(page) + "R"
149 }
150
151 // DECXCPR is an alias for [ExtendedCursorPositionReport].
152 func DECXCPR(line, column, page int) string {
153 return ExtendedCursorPositionReport(line, column, page)
154 }
155
156 // LightDarkReport is a control sequence that reports the terminal's operating
157 // system light/dark color preference.
158 //
159 // CSI ? 997 ; 1 n for dark mode
160 // CSI ? 997 ; 2 n for light mode
161 //
162 // See: https://contour-terminal.org/vt-extensions/color-palette-update-notifications/
163 func LightDarkReport(dark bool) string {
164 if dark {
165 return "\x1b[?997;1n"
166 }
167 return "\x1b[?997;2n"
168 }