diff --git a/docs/changelog.rst b/docs/changelog.rst index 7ec5a8b43..018bff8a1 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -199,8 +199,10 @@ Detailed list of changes - :doc:`choose-files `: Fix incorrect cached previews when a file or symlink changes during rendering. -- :doc:`choose-files `: Fix crashes and preserve extended - colors when wrapping the output of custom preview commands. +- kittens: Fix a crash when wrapping text that contains extended SGR colors, + such as the output of custom preview commands in :doc:`choose-files + `. Also fix bright colors and colors with a color space + id being changed by the wrapping. - IME: Move cursor position within pre-edit text (:iss:`10807`) diff --git a/tools/utils/style/indent-and-wrap.go b/tools/utils/style/indent-and-wrap.go index f6830a3d6..471497442 100644 --- a/tools/utils/style/indent-and-wrap.go +++ b/tools/utils/style/indent-and-wrap.go @@ -28,7 +28,7 @@ func (self sgr_color) as_sgr(base int) string { if n <= 7 { return strconv.Itoa(base + n) } - return strconv.Itoa(base + 60 + n - 7) + return strconv.Itoa(base + 60 + n - 8) } return fmt.Sprintf("%d:5:%d", base+8, n) } @@ -50,6 +50,10 @@ func (self *sgr_color) from_extended(nums []int) bool { return true } case 2: + if len(nums) > 4 { + // Ignore the color space id in 38:2::r:g:b + nums = nums[1:] + } if len(nums) > 3 { self.number = -1 self.color.Red = as_uint8(nums[1]) @@ -61,12 +65,42 @@ func (self *sgr_color) from_extended(nums []int) bool { return false } -// Semicolons separate both SGR attributes and extended color operands. +// Empty SGR parameters default to zero +func parse_sgr_param(x string) (int, error) { + if x == "" { + return 0, nil + } + return strconv.Atoi(x) +} + +// Parses the sub-parameters of an extended color in the colon form, such as +// 38:5:200 or 38:2::r:g:b. Invalid colors leave the current color unchanged. +func (self *sgr_color) from_colon_params(parts []string) { + nums := make([]int, len(parts)) + for i, x := range parts { + n, err := parse_sgr_param(x) + if err != nil { + return + } + nums[i] = n + } + self.from_extended(nums) +} + +// Parses an extended color in the semicolon form, such as 38;5;200 or +// 38;2;r;g;b where the semicolons separate both SGR attributes and the color +// operands. Returns the parameters remaining after the color. As in the +// terminal, an unknown color type makes the rest of the parameters +// uninterpretable, so they are all discarded. Invalid colors leave the current +// color unchanged. func (self *sgr_color) from_semicolon_params(parts []string) []string { if len(parts) == 0 { return nil } - mode, _ := strconv.Atoi(parts[0]) + mode, err := parse_sgr_param(parts[0]) + if err != nil { + return nil + } var count int switch mode { case 5: @@ -81,10 +115,7 @@ func (self *sgr_color) from_semicolon_params(parts []string) []string { } nums := [4]int{mode} for i := 1; i < count; i++ { - if parts[i] == "" && i < len(parts)-1 { - continue // Empty parameters between semicolons default to zero. - } - n, err := strconv.Atoi(parts[i]) + n, err := parse_sgr_param(parts[i]) if err != nil { return parts[count:] } @@ -100,6 +131,16 @@ type sgr_state struct { fg, bg, uc sgr_color } +func (self *sgr_state) extended_color(code int) *sgr_color { + switch code { + case 48: + return &self.bg + case 58: + return &self.uc + } + return &self.fg +} + func (self *sgr_state) reset() { *self = sgr_state{} } @@ -254,17 +295,11 @@ func (self *sgr_state) apply_csi(raw string) { case 90, 91, 92, 93, 94, 95, 96, 97: self.fg.number = nums[0] + 1 - 82 case 38, 48, 58: - color := &self.fg - switch nums[0] { - case 48: - color = &self.bg - case 58: - color = &self.uc - } + color := self.extended_color(nums[0]) if len(subparts) == 1 { parts = color.from_semicolon_params(parts) } else { - color.from_extended(nums[1:]) + color.from_colon_params(subparts[1:]) } case 39: self.fg.number = 0 diff --git a/tools/utils/style/indent-and-wrap_test.go b/tools/utils/style/indent-and-wrap_test.go index 4ea0a6848..4e30eb957 100644 --- a/tools/utils/style/indent-and-wrap_test.go +++ b/tools/utils/style/indent-and-wrap_test.go @@ -91,10 +91,46 @@ func TestIncompleteExtendedColor(t *testing.T) { t.Fatalf("incomplete extended color %v changed the current color to %+v", params, color) } } - for _, params := range [][]string{{"5", ""}, {"2", "3", "4"}} { + for _, params := range [][]string{{"5"}, {"2", "3", "4"}} { color := initial if remaining := color.from_semicolon_params(params); len(remaining) != 0 || color != initial { t.Fatalf("incomplete extended color %q left parameters %q and color %+v", params, remaining, color) } } } + +func TestSGRStateParsing(t *testing.T) { + for raw, expected := range map[string]string{ + // trailing empty parameters default to zero + "38;5;m": "30", + "38;2;1;2;m": "38:2:1:2:0", + // the color space id in the colon form is ignored + "38:2:1:2:3:4m": "38:2:2:3:4", + "38:2:1:2:3m": "38:2:1:2:3", + "48:2::3:4:7m": "48:2:3:4:7", + // bright colors round trip + "90m": "90", + "97m": "97", + "100m": "100", + "107m": "107", + "38;5;8m": "90", + "38;5;15m": "97", + "48;5;9m": "101", + "58;5;9m": "58:5:9", + "38;5;16m": "38:5:16", + // attributes after an extended color are applied + "38;5;200;1;3m": "1;3;38:5:200", + "48;2;1;2;3;4:3m": "4:3;48:2:1:2:3", + // unknown color types discard the rest of the parameters + "1;38;7;3m": "1", + // invalid colors are ignored + "38;5;x;3m": "3", + "38:5:x;3m": "3", + } { + var s sgr_state + s.apply_csi(raw) + if actual := s.as_sgr(false); actual != expected { + t.Fatalf("Parsing SGR %q gave %q expected %q", raw, actual, expected) + } + } +}