Skip to content

Commit

Permalink
Improve CSI sequence error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Nov 12, 2023
1 parent 93d01ee commit bcda779
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
20 changes: 12 additions & 8 deletions m/ansiTokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strconv"
"strings"

log "github.com/sirupsen/logrus"
"github.com/walles/moar/twin"
)

Expand Down Expand Up @@ -426,7 +425,14 @@ type _StyledString struct {

// rawUpdateStyle parses a string of the form "33m" into changes to style. This
// is what comes after ESC[ in an ANSI SGR sequence.
func rawUpdateStyle(style twin.Style, escapeSequenceWithoutHeader string) twin.Style {
func rawUpdateStyle(style twin.Style, escapeSequenceWithoutHeader string) (twin.Style, error) {
if len(escapeSequenceWithoutHeader) == 0 {
return style, fmt.Errorf("empty escape sequence, expected at least an ending letter")
}
if escapeSequenceWithoutHeader[len(escapeSequenceWithoutHeader)-1] != 'm' {
return style, fmt.Errorf("escape sequence does not end with 'm': %s", escapeSequenceWithoutHeader)
}

numbers := strings.FieldsFunc(escapeSequenceWithoutHeader[:len(escapeSequenceWithoutHeader)-1], func(r rune) bool {
return r == ';' || r == ':'
})
Expand Down Expand Up @@ -488,8 +494,7 @@ func rawUpdateStyle(style twin.Style, escapeSequenceWithoutHeader string) twin.S
var color *twin.Color
index, color, err = consumeCompositeColor(numbers, index-1)
if err != nil {
log.Warnf("Foreground: %s", err.Error())
return style
return style, fmt.Errorf("Foreground: %w", err)
}
style = style.Foreground(*color)
case "39":
Expand Down Expand Up @@ -517,8 +522,7 @@ func rawUpdateStyle(style twin.Style, escapeSequenceWithoutHeader string) twin.S
var color *twin.Color
index, color, err = consumeCompositeColor(numbers, index-1)
if err != nil {
log.Warnf("Background: %s", err.Error())
return style
return style, fmt.Errorf("Background: %w", err)
}
style = style.Background(*color)
case "49":
Expand Down Expand Up @@ -565,11 +569,11 @@ func rawUpdateStyle(style twin.Style, escapeSequenceWithoutHeader string) twin.S
style = style.Background(twin.NewColor16(15))

default:
log.Warnf("Unrecognized ANSI SGR code <%s>", number)
return style, fmt.Errorf("Unrecognized ANSI SGR code <%s>", number)
}
}

return style
return style, nil
}

// numbers is a list of numbers from a ANSI SGR string
Expand Down
3 changes: 2 additions & 1 deletion m/ansiTokenizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ func TestConsumeCompositeColorIncomplete24Bit(t *testing.T) {
}

func TestRawUpdateStyle(t *testing.T) {
numberColored := rawUpdateStyle(twin.StyleDefault, "33m")
numberColored, err := rawUpdateStyle(twin.StyleDefault, "33m")
assert.NilError(t, err)
assert.Equal(t, numberColored, twin.StyleDefault.Foreground(twin.NewColor16(3)))
}

Expand Down
8 changes: 7 additions & 1 deletion m/styledStringSplitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"
"unicode/utf8"

log "github.com/sirupsen/logrus"
"github.com/walles/moar/twin"
)

Expand Down Expand Up @@ -158,7 +159,12 @@ func (s *styledStringSplitter) handleCompleteControlSequence(charAfterEsc rune,

lastChar := sequence[len(sequence)-1]
if lastChar == 'm' {
newStyle := rawUpdateStyle(s.inProgressStyle, sequence)
newStyle, err := rawUpdateStyle(s.inProgressStyle, sequence)
if err != nil {
log.Warnf("Failed to parse style %s: %v", sequence, err)
return false
}

s.startNewPart(newStyle)
return true
}
Expand Down

0 comments on commit bcda779

Please sign in to comment.