2020-09-19 16:00:50 +00:00
|
|
|
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
|
|
|
|
package termenv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
|
|
|
func colorProfile() Profile {
|
2020-11-09 15:25:54 +00:00
|
|
|
term := os.Getenv("TERM")
|
2020-09-19 16:00:50 +00:00
|
|
|
colorTerm := os.Getenv("COLORTERM")
|
2020-11-09 15:25:54 +00:00
|
|
|
|
|
|
|
switch strings.ToLower(colorTerm) {
|
|
|
|
case "24bit":
|
|
|
|
fallthrough
|
|
|
|
case "truecolor":
|
|
|
|
if term == "screen" || !strings.HasPrefix(term, "screen") {
|
|
|
|
// enable TrueColor in tmux, but not for old-school screen
|
|
|
|
return TrueColor
|
|
|
|
}
|
|
|
|
case "yes":
|
|
|
|
fallthrough
|
|
|
|
case "true":
|
|
|
|
return ANSI256
|
2020-09-19 16:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(term, "256color") {
|
|
|
|
return ANSI256
|
|
|
|
}
|
|
|
|
if strings.Contains(term, "color") {
|
|
|
|
return ANSI
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ascii
|
|
|
|
}
|
|
|
|
|
|
|
|
func foregroundColor() Color {
|
|
|
|
s, err := termStatusReport(10)
|
|
|
|
if err == nil {
|
|
|
|
c, err := xTermColor(s)
|
|
|
|
if err == nil {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
colorFGBG := os.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 backgroundColor() Color {
|
|
|
|
s, err := termStatusReport(11)
|
|
|
|
if err == nil {
|
|
|
|
c, err := xTermColor(s)
|
|
|
|
if err == nil {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
colorFGBG := os.Getenv("COLORFGBG")
|
|
|
|
if strings.Contains(colorFGBG, ";") {
|
|
|
|
c := strings.Split(colorFGBG, ";")
|
|
|
|
i, err := strconv.Atoi(c[1])
|
|
|
|
if err == nil {
|
|
|
|
return ANSIColor(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// default black
|
|
|
|
return ANSIColor(0)
|
|
|
|
}
|
|
|
|
|
2021-08-30 15:18:50 +00:00
|
|
|
func readNextByte(f *os.File) (byte, error) {
|
|
|
|
var b [1]byte
|
|
|
|
n, err := f.Read(b[:])
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-09-19 16:00:50 +00:00
|
|
|
|
2021-08-30 15:18:50 +00:00
|
|
|
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 readNextResponse(fd *os.File) (response string, isOSC bool, err error) {
|
|
|
|
start, err := readNextByte(fd)
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we encounter a backslash, this is a left-over from the previous OSC
|
|
|
|
// response, which can be terminated by an optional backslash
|
|
|
|
if start == '\\' {
|
|
|
|
start, err = readNextByte(fd)
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
2020-09-19 16:00:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 15:18:50 +00:00
|
|
|
// first byte must be ESC
|
|
|
|
if start != '\033' {
|
|
|
|
return "", false, ErrStatusReport
|
|
|
|
}
|
|
|
|
|
|
|
|
response += string(start)
|
|
|
|
|
|
|
|
// next byte is either '[' (cursor position response) or ']' (OSC response)
|
|
|
|
tpe, err := readNextByte(fd)
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response += string(tpe)
|
|
|
|
|
|
|
|
var oscResponse bool
|
|
|
|
switch tpe {
|
|
|
|
case '[':
|
|
|
|
oscResponse = false
|
|
|
|
case ']':
|
|
|
|
oscResponse = true
|
|
|
|
default:
|
|
|
|
return "", false, ErrStatusReport
|
2020-09-19 16:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2021-08-30 15:18:50 +00:00
|
|
|
b, err := readNextByte(os.Stdout)
|
2020-09-19 16:00:50 +00:00
|
|
|
if err != nil {
|
2021-08-30 15:18:50 +00:00
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response += string(b)
|
|
|
|
|
|
|
|
if oscResponse {
|
|
|
|
// OSC can be terminated by BEL (\a) or ST (ESC)
|
|
|
|
if b == '\a' || strings.HasSuffix(response, "\033") {
|
|
|
|
return response, true, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// cursor position response is terminated by 'R'
|
|
|
|
if b == 'R' {
|
|
|
|
return response, false, nil
|
|
|
|
}
|
2020-09-19 16:00:50 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 15:18:50 +00:00
|
|
|
// both responses have less than 25 bytes, so if we read more, that's an error
|
|
|
|
if len(response) > 25 {
|
2020-09-19 16:00:50 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 15:18:50 +00:00
|
|
|
return "", false, ErrStatusReport
|
2020-09-19 16:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func termStatusReport(sequence int) (string, error) {
|
2021-08-30 15:18:50 +00:00
|
|
|
// screen/tmux can't support OSC, because they can be connected to multiple
|
|
|
|
// terminals concurrently.
|
|
|
|
term := os.Getenv("TERM")
|
|
|
|
if strings.HasPrefix(term, "screen") {
|
|
|
|
return "", ErrStatusReport
|
|
|
|
}
|
|
|
|
|
2020-09-19 16:00:50 +00:00
|
|
|
t, err := unix.IoctlGetTermios(unix.Stdout, tcgetattr)
|
|
|
|
if err != nil {
|
|
|
|
return "", ErrStatusReport
|
|
|
|
}
|
|
|
|
defer unix.IoctlSetTermios(unix.Stdout, tcsetattr, t)
|
|
|
|
|
|
|
|
noecho := *t
|
|
|
|
noecho.Lflag = noecho.Lflag &^ unix.ECHO
|
|
|
|
noecho.Lflag = noecho.Lflag &^ unix.ICANON
|
|
|
|
if err := unix.IoctlSetTermios(unix.Stdout, tcsetattr, &noecho); err != nil {
|
|
|
|
return "", ErrStatusReport
|
|
|
|
}
|
|
|
|
|
2021-08-30 15:18:50 +00:00
|
|
|
// first, send OSC query, which is ignored by terminal which do not support it
|
|
|
|
fmt.Printf("\033]%d;?\033\\", sequence)
|
|
|
|
|
|
|
|
// then, query cursor position, should be supported by all terminals
|
|
|
|
fmt.Printf("\033[6n")
|
|
|
|
|
|
|
|
// read the next response
|
|
|
|
res, isOSC, err := readNextResponse(os.Stdout)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if this is not OSC response, then the terminal does not support it
|
|
|
|
if !isOSC {
|
2020-09-19 16:00:50 +00:00
|
|
|
return "", ErrStatusReport
|
|
|
|
}
|
2021-08-30 15:18:50 +00:00
|
|
|
|
|
|
|
// read the cursor query response next and discard the result
|
|
|
|
_, _, err = readNextResponse(os.Stdout)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// fmt.Println("Rcvd", res[1:])
|
|
|
|
return res, nil
|
2020-09-19 16:00:50 +00:00
|
|
|
}
|