-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
termenv_unix.go
298 lines (250 loc) · 6.12 KB
/
termenv_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
// +build darwin dragonfly freebsd linux netbsd openbsd solaris zos
package termenv
import (
"fmt"
"io"
"strconv"
"strings"
"time"
"golang.org/x/sys/unix"
)
const (
// timeout for OSC queries
OSCTimeout = 5 * time.Second
)
// ColorProfile returns the supported color profile:
// Ascii, ANSI, ANSI256, or TrueColor.
func (o *Output) ColorProfile() Profile {
if !o.isTTY() {
return Ascii
}
if o.environ.Getenv("GOOGLE_CLOUD_SHELL") == "true" {
return TrueColor
}
term := o.environ.Getenv("TERM")
colorTerm := o.environ.Getenv("COLORTERM")
switch strings.ToLower(colorTerm) {
case "24bit":
fallthrough
case "truecolor":
if strings.HasPrefix(term, "screen") {
// tmux supports TrueColor, screen only ANSI256
if o.environ.Getenv("TERM_PROGRAM") != "tmux" {
return ANSI256
}
}
return TrueColor
case "yes":
fallthrough
case "true":
return ANSI256
}
switch term {
case
"alacritty",
"contour",
"wezterm",
"xterm-ghostty",
"xterm-kitty":
return TrueColor
case "linux":
return ANSI
}
if strings.Contains(term, "256color") {
return ANSI256
}
if strings.Contains(term, "color") {
return ANSI
}
if strings.Contains(term, "ansi") {
return ANSI
}
return Ascii
}
func (o Output) foregroundColor() Color {
s, err := o.termStatusReport(10)
if err == nil {
c, err := xTermColor(s)
if err == nil {
return c
}
}
colorFGBG := o.environ.Getenv("COLORFGBG")
if strings.Contains(colorFGBG, ";") {
c := strings.Split(colorFGBG, ";")
i, err := strconv.Atoi(c[0])
if err == nil {
return ANSIColor(i)
}
}
// default gray
return ANSIColor(7)
}
func (o Output) backgroundColor() Color {
s, err := o.termStatusReport(11)
if err == nil {
c, err := xTermColor(s)
if err == nil {
return c
}
}
colorFGBG := o.environ.Getenv("COLORFGBG")
if strings.Contains(colorFGBG, ";") {
c := strings.Split(colorFGBG, ";")
i, err := strconv.Atoi(c[len(c)-1])
if err == nil {
return ANSIColor(i)
}
}
// default black
return ANSIColor(0)
}
func (o *Output) waitForData(timeout time.Duration) error {
fd := o.TTY().Fd()
tv := unix.NsecToTimeval(int64(timeout))
var readfds unix.FdSet
readfds.Set(int(fd))
for {
n, err := unix.Select(int(fd)+1, &readfds, nil, nil, &tv)
if err == unix.EINTR {
continue
}
if err != nil {
return err
}
if n == 0 {
return fmt.Errorf("timeout")
}
break
}
return nil
}
func (o *Output) readNextByte() (byte, error) {
if !o.unsafe {
if err := o.waitForData(OSCTimeout); err != nil {
return 0, err
}
}
var b [1]byte
n, err := o.TTY().Read(b[:])
if err != nil {
return 0, err
}
if n == 0 {
panic("read returned no data")
}
return b[0], nil
}
// readNextResponse reads either an OSC response or a cursor position response:
// - OSC response: "\x1b]11;rgb:1111/1111/1111\x1b\\"
// - cursor position response: "\x1b[42;1R"
func (o *Output) readNextResponse() (response string, isOSC bool, err error) {
start, err := o.readNextByte()
if err != nil {
return "", false, err
}
// first byte must be ESC
for start != ESC {
start, err = o.readNextByte()
if err != nil {
return "", false, err
}
}
response += string(start)
// next byte is either '[' (cursor position response) or ']' (OSC response)
tpe, err := o.readNextByte()
if err != nil {
return "", false, err
}
response += string(tpe)
var oscResponse bool
switch tpe {
case '[':
oscResponse = false
case ']':
oscResponse = true
default:
return "", false, ErrStatusReport
}
for {
b, err := o.readNextByte()
if err != nil {
return "", false, err
}
response += string(b)
if oscResponse {
// OSC can be terminated by BEL (\a) or ST (ESC)
if b == BEL || strings.HasSuffix(response, string(ESC)) {
return response, true, nil
}
} else {
// cursor position response is terminated by 'R'
if b == 'R' {
return response, false, nil
}
}
// both responses have less than 25 bytes, so if we read more, that's an error
if len(response) > 25 {
break
}
}
return "", false, ErrStatusReport
}
func (o Output) termStatusReport(sequence int) (string, error) {
// screen/tmux can't support OSC, because they can be connected to multiple
// terminals concurrently.
term := o.environ.Getenv("TERM")
if strings.HasPrefix(term, "screen") || strings.HasPrefix(term, "tmux") || strings.HasPrefix(term, "dumb") {
return "", ErrStatusReport
}
tty := o.TTY()
if tty == nil {
return "", ErrStatusReport
}
if !o.unsafe {
fd := int(tty.Fd())
// if in background, we can't control the terminal
if !isForeground(fd) {
return "", ErrStatusReport
}
t, err := unix.IoctlGetTermios(fd, tcgetattr)
if err != nil {
return "", fmt.Errorf("%s: %s", ErrStatusReport, err)
}
defer unix.IoctlSetTermios(fd, tcsetattr, t) //nolint:errcheck
noecho := *t
noecho.Lflag = noecho.Lflag &^ unix.ECHO
noecho.Lflag = noecho.Lflag &^ unix.ICANON
if err := unix.IoctlSetTermios(fd, tcsetattr, &noecho); err != nil {
return "", fmt.Errorf("%s: %s", ErrStatusReport, err)
}
}
// first, send OSC query, which is ignored by terminal which do not support it
fmt.Fprintf(tty, OSC+"%d;?"+ST, sequence)
// then, query cursor position, should be supported by all terminals
fmt.Fprintf(tty, CSI+"6n")
// read the next response
res, isOSC, err := o.readNextResponse()
if err != nil {
return "", fmt.Errorf("%s: %s", ErrStatusReport, err)
}
// if this is not OSC response, then the terminal does not support it
if !isOSC {
return "", ErrStatusReport
}
// read the cursor query response next and discard the result
_, _, err = o.readNextResponse()
if err != nil {
return "", err
}
// fmt.Println("Rcvd", res[1:])
return res, nil
}
// EnableVirtualTerminalProcessing enables virtual terminal processing on
// Windows for w and returns a function that restores w to its previous state.
// On non-Windows platforms, or if w does not refer to a terminal, then it
// returns a non-nil no-op function and no error.
func EnableVirtualTerminalProcessing(_ io.Writer) (func() error, error) {
return func() error { return nil }, nil
}