Skip to content

Commit

Permalink
Support another terminal color response format
Browse files Browse the repository at this point in the history
Related to (but I can't see how this would fix) #190.
  • Loading branch information
walles committed Jan 18, 2024
1 parent 35b0ffc commit cc66577
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions twin/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,12 @@ func (screen *UnixScreen) Size() (width int, height int) {

func parseTerminalBgColorResponse(responseBytes []byte) *Color {
prefix := "\x1b]11;rgb:"
suffix := "\x07"
sampleResponse := prefix + "0000/0000/0000" + suffix
suffix1 := "\x07"
suffix2 := "\x1b\\"
sampleResponse1 := prefix + "0000/0000/0000" + suffix1
sampleResponse2 := prefix + "0000/0000/0000" + suffix2

if len(responseBytes) != len(sampleResponse) {
if len(responseBytes) != len(sampleResponse1) && len(responseBytes) != len(sampleResponse2) {
// Not a bg color response
return nil
}
Expand All @@ -541,11 +543,12 @@ func parseTerminalBgColorResponse(responseBytes []byte) *Color {
}
response = strings.TrimPrefix(response, prefix)

if !strings.HasSuffix(response, suffix) {
if !strings.HasSuffix(response, suffix1) && !strings.HasSuffix(response, suffix2) {
log.Debug("Got unexpected suffix in bg color response from terminal: ", string(responseBytes))
return nil
}
response = strings.TrimSuffix(response, suffix)
response = strings.TrimSuffix(response, suffix1)
response = strings.TrimSuffix(response, suffix2)

// response is now "RRRR/GGGG/BBBB"
red, err := strconv.ParseUint(response[0:4], 16, 16)
Expand Down

0 comments on commit cc66577

Please sign in to comment.