Skip to content

Commit

Permalink
Support backslash-less report termination, as used by rxvt
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Apr 2, 2021
1 parent 5f42203 commit d1b90ad
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions color.go
Expand Up @@ -160,6 +160,8 @@ func xTermColor(s string) (RGBColor, error) {
switch {
case strings.HasSuffix(s, "\a"):
s = strings.TrimSuffix(s, "\a")
case strings.HasSuffix(s, "\033"):
s = strings.TrimSuffix(s, "\033")
case strings.HasSuffix(s, "\033\\"):
s = strings.TrimSuffix(s, "\033\\")
default:
Expand Down
5 changes: 5 additions & 0 deletions color_test.go
Expand Up @@ -8,6 +8,11 @@ func TestXTermColor(t *testing.T) {
color RGBColor
valid bool
}{
{
"\033]11;rgb:fafa/fafa/fafa\033",
RGBColor("#fafafa"),
true,
},
{
"\033]11;rgb:fafa/fafa/fafa\033\\",
RGBColor("#fafafa"),
Expand Down
19 changes: 15 additions & 4 deletions termenv_unix.go
Expand Up @@ -101,12 +101,21 @@ func readNextByte(f *os.File) (byte, error) {
// * 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) {
// first byte must be ESC
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
}
}

// first byte must be ESC
if start != '\033' {
return "", false, ErrStatusReport
}
Expand Down Expand Up @@ -140,8 +149,8 @@ func readNextResponse(fd *os.File) (response string, isOSC bool, err error) {
response += string(b)

if oscResponse {
// OSC can be terminated by BEL (\a) or ST (ESC \)
if b == '\a' || strings.HasSuffix(response, "\033\\") {
// OSC can be terminated by BEL (\a) or ST (ESC)
if b == '\a' || strings.HasSuffix(response, "\033") {
return response, true, nil
}
} else {
Expand All @@ -161,6 +170,8 @@ func readNextResponse(fd *os.File) (response string, isOSC bool, err error) {
}

func termStatusReport(sequence int) (string, error) {
// 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
Expand Down Expand Up @@ -202,6 +213,6 @@ func termStatusReport(sequence int) (string, error) {
return "", err
}

// fmt.Println("Rcvd", s[1:])
// fmt.Println("Rcvd", res[1:])
return res, nil
}

0 comments on commit d1b90ad

Please sign in to comment.