Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support backslash-less report termination, as used by rxvt #34

Merged
merged 1 commit into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions color.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}