Parse SGR codes when wrapping text exactly as kitty does

Port the CSI tokenizer and SGR grouping logic from vt-parser.c and
cursor.c so that empty parameters terminated by a semicolon are treated
as zero everywhere, not only in extended colors. This fixes stale
attributes being re-applied on wrapped lines for codes such as ;m, ;31m
and 4:;31m. Also match kitty for codes with invalid characters or
private/intermediate bytes (ignored entirely), misplaced sub-parameters,
mixed colon/semicolon colors, out of range color indices, SGR 21 and
clamped underline styles.

Closes #10895
This commit is contained in:
Kovid Goyal 2026-11-19 09:03:09 +05:30
parent 2040665725
commit d6f038691c
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 274 additions and 142 deletions

View file

@ -39,94 +39,99 @@ func as_uint8(x int) uint8 {
return uint8(uint(x) & 0xff)
}
func (self *sgr_color) from_extended(nums []int) bool {
if len(nums) == 0 {
return false
}
switch nums[0] {
case 5:
if len(nums) > 1 {
self.number = 1 + nums[1]
// The limits used by the kitty CSI parser in vt-parser.c
const (
max_csi_params = 256
max_csi_digits = 16
)
// Splits the body of a CSI SGR escape code into its parameters, mirroring
// csi_parse_loop() in kitty/vt-parser.c. An empty field terminated by a
// semicolon is zero, while an empty field terminated by a colon or the end of
// the code is dropped. is_sub_param[i] is true when params[i] was preceded by a
// colon. Returns ok == false if the code is not a plain SGR code, for example
// because it has a private or intermediate byte or an invalid character, in
// which case the terminal ignores it.
func parse_csi_params(raw string) (params []int, is_sub_param []bool, ok bool) {
params = make([]int, 0, 16)
is_sub_param = make([]bool, 0, 16)
next_is_sub_param := false
var accumulator uint64
num_digits, mult := 0, int64(1)
commit := func() bool {
if num_digits == 0 {
return true
}
case 2:
if len(nums) > 4 {
// Ignore the color space id in 38:2:<id>:r:g:b
nums = nums[1:]
if len(params) >= max_csi_params {
return false
}
if len(nums) > 3 {
// The C parser stores into an int, truncating to 32 bits
params = append(params, int(int32(mult*int64(accumulator))))
is_sub_param = append(is_sub_param, next_is_sub_param)
accumulator, num_digits, mult = 0, 0, 1
return true
}
for i := 0; i < len(raw); i++ {
switch ch := raw[i]; {
case '0' <= ch && ch <= '9':
if num_digits < max_csi_digits {
num_digits++
accumulator = accumulator*10 + uint64(ch-'0')
}
case ch == ':':
// A colon is not valid as the first character of a CSI code
if i == 0 || !commit() {
return nil, nil, false
}
next_is_sub_param = true
case ch == ';':
if num_digits == 0 {
num_digits = 1 // Empty means zero
}
if !commit() {
return nil, nil, false
}
next_is_sub_param = false
case ch == '-' && num_digits == 0 && i < len(raw)-1:
// A minus sign starting a parameter makes it negative. Anywhere
// else it is an intermediate byte, so this is not an SGR code.
mult, num_digits = -1, 1
default:
return nil, nil, false
}
}
if !commit() {
return nil, nil, false
}
return params, is_sub_param, true
}
// Mirrors parse_color() in kitty/cursor.c, returns the number of parameters consumed
func (self *sgr_color) from_params(params []int) int {
if len(params) == 0 {
return 0
}
switch params[0] {
case 5:
if len(params) > 1 {
self.number = 1 + int(as_uint8(params[1]))
return 2
}
case 2:
if len(params) > 3 {
i := 1
if len(params) > 4 {
// Ignore the color space id in 38:2:<id>:r:g:b
i++
}
self.number = -1
self.color.Red = as_uint8(nums[1])
self.color.Green = as_uint8(nums[2])
self.color.Blue = as_uint8(nums[3])
return true
self.color.Red = as_uint8(params[i])
self.color.Green = as_uint8(params[i+1])
self.color.Blue = as_uint8(params[i+2])
return i + 3
}
}
return false
}
// Empty SGR parameters terminated by a semicolon 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, followed_by_semicolon bool) {
nums := make([]int, 0, len(parts))
for i, x := range parts {
if x == "" && (i < len(parts)-1 || !followed_by_semicolon) {
continue
}
n, err := parse_sgr_param(x)
if err != nil {
return
}
nums = append(nums, 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, err := strconv.Atoi(parts[0])
if err != nil {
return nil
}
var count int
switch mode {
case 5:
count = 2
case 2:
count = 4
default:
return nil
}
// A final empty field has no terminating semicolon and is never committed.
if len(parts) < count || (len(parts) == count && parts[count-1] == "") {
return nil
}
nums := [4]int{mode}
for i := 1; i < count; i++ {
n, err := parse_sgr_param(parts[i])
if err != nil {
return parts[count:]
}
nums[i] = n
}
self.from_extended(nums[:count])
return parts[count:]
return 1
}
type sgr_state struct {
@ -225,30 +230,14 @@ func (self sgr_state) as_escape_codes(for_close bool) string {
return fmt.Sprintf("\x1b[%sm", q)
}
func (self *sgr_state) apply_csi(raw string) {
if !strings.HasSuffix(raw, "m") {
return
}
raw = raw[:len(raw)-1]
if raw == "" {
raw = "0"
}
parts := strings.Split(raw, ";")
nums := make([]int, 0, 8)
for len(parts) > 0 {
subparts := strings.Split(parts[0], ":")
parts = parts[1:]
nums = nums[:0]
for _, b := range subparts {
q, err := strconv.Atoi(b)
if err == nil {
nums = append(nums, q)
}
}
if len(nums) == 0 {
continue
}
switch nums[0] {
// Applies a group of SGR parameters, mirroring cursor_from_sgr() in
// kitty/cursor.c. When is_group is true the parameters are a single attribute
// with its sub-parameters, such as 4:3 or 38:2:r:g:b.
func (self *sgr_state) apply_sgr(params []int, is_group bool) {
for i := 0; i < len(params); {
attr := params[i]
i++
switch attr {
case 0:
self.reset()
case 1:
@ -273,52 +262,132 @@ func (self *sgr_state) apply_csi(raw string) {
self.strikethrough = true
case 29:
self.strikethrough = false
case 21:
self.underline_style = double_underline
case 24:
self.underline_style = no_underline
case 4:
us := 1
if len(nums) > 1 {
us = nums[1]
}
switch us {
case 0:
self.underline_style = no_underline
case 1:
if is_group && i < len(params) {
// Large styles are clamped to the last style and the result is
// stored in three bits, so negative styles wrap around. The
// unused styles 6 and 7 cannot be represented, so ignore them.
if us := underline_style(min(params[i], int(dashed_underline)) & 7); us <= dashed_underline {
self.underline_style = us
}
i++
} else {
self.underline_style = straight_underline
case 2:
self.underline_style = double_underline
case 3:
self.underline_style = curly_underline
case 4:
self.underline_style = dotted_underline
case 5:
self.underline_style = dashed_underline
}
case 30, 31, 32, 33, 34, 35, 36, 37:
self.fg.number = nums[0] + 1 - 30
self.fg.number = attr + 1 - 30
case 90, 91, 92, 93, 94, 95, 96, 97:
self.fg.number = nums[0] + 1 - 82
self.fg.number = attr + 1 - 82
case 38, 48, 58:
color := self.extended_color(nums[0])
if len(subparts) == 1 {
parts = color.from_semicolon_params(parts)
} else {
color.from_colon_params(subparts[1:], len(parts) > 0)
}
i += self.extended_color(attr).from_params(params[i:])
case 39:
self.fg.number = 0
case 40, 41, 42, 43, 44, 45, 46, 47:
self.bg.number = nums[0] + 1 - 40
self.bg.number = attr + 1 - 40
case 100, 101, 102, 103, 104, 105, 106, 107:
self.bg.number = nums[0] + 1 - 92
self.bg.number = attr + 1 - 92
case 49:
self.bg.number = 0
case 59:
self.uc.number = 0
}
if is_group {
break
}
}
}
// Applies the body of a CSI escape code, such as "1;38:5:200m". Mirrors
// _parse_sgr() in kitty/vt-parser.c, which splits the parameters into
// attributes with sub-parameters and semicolon separated extended colors.
// As in the terminal, an invalid sub-parameter or color type discards the
// rest of the parameters, but the attributes before it remain applied.
func (self *sgr_state) apply_csi(raw string) {
if !strings.HasSuffix(raw, "m") {
return
}
params, is_sub_param, ok := parse_csi_params(raw[:len(raw)-1])
if !ok {
return
}
if len(params) == 0 {
params, is_sub_param = []int{0}, []bool{false}
}
type parse_state int
const (
normal parse_state = iota
sub_params
color
color1
color3
)
state := normal
first_param, num_params := 0, 0
send := func() {
if num_params > 0 {
self.apply_sgr(params[first_param:first_param+num_params], state != normal)
state = normal
first_param += num_params
num_params = 0
}
}
for pos := 0; pos < len(params); pos++ {
switch state {
case normal:
if is_sub_param[pos] {
if num_params == 0 || pos == 0 {
return
}
// The previous parameter starts a group of sub-parameters
num_params--
send()
state = sub_params
first_param = pos - 1
num_params = 1
}
switch params[pos] {
case 38, 48, 58:
send()
state = color
first_param = pos
num_params = 1
default:
num_params++
}
case sub_params:
if is_sub_param[pos] {
num_params++
} else {
send()
pos--
}
case color:
switch params[pos] {
case 2:
state = utils.IfElse(is_sub_param[pos], sub_params, color3)
case 5:
state = utils.IfElse(is_sub_param[pos], sub_params, color1)
default:
return
}
num_params++
case color1:
num_params++
send()
case color3:
num_params++
if num_params >= 5 {
send()
}
}
}
send()
}
type hyperlink_state struct {
id, url string
}

View file

@ -6,6 +6,8 @@ import (
"encoding/json"
"strings"
"testing"
"github.com/kovidgoyal/kitty/tools/utils"
)
func TestFormatWithIndent(t *testing.T) {
@ -87,16 +89,10 @@ func TestIncompleteExtendedColor(t *testing.T) {
initial := sgr_color{number: 201}
for _, params := range [][]int{nil, {5}, {2, 3, 4}} {
color := initial
if color.from_extended(params) || color != initial {
if consumed := color.from_params(params); consumed != min(len(params), 1) || color != initial {
t.Fatalf("incomplete extended color %v changed the current color to %+v", params, color)
}
}
for _, params := range [][]string{{"5"}, {"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 TestWrapTextWithEmptyColorParams(t *testing.T) {
@ -141,9 +137,66 @@ func TestSGRStateParsing(t *testing.T) {
"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",
// invalid characters cause the whole code to be ignored
"1;38;5;x;3m": "",
"1;38:5:x;3m": "",
"?1m": "",
">4;1m": "",
"1 m": "",
"1-m": "",
"-m": "",
// empty parameters terminated by a semicolon are zero, i.e. reset
";m": "",
";1m": "1",
"1;;3m": "3",
"1;38;5;200;;3m": "3",
"1;38;5;;m": "1;30",
"38;5;;1m": "1;30",
// empty parameters terminated by a colon or the end are dropped
"1;38;5;m": "1",
"1;38;2;1;2;m": "1",
"38:5::200m": "38:5:200",
"1;4:m": "1;4:1",
// empty sub-parameters terminated by a semicolon are zero
"4:;31m": "31",
"4:3;38:5:;1m": "1;4:3;30",
// a leading colon makes the code invalid
":m": "",
"::m": "",
// parameters are truncated to 16 digits and wrap at 32 bits
"4294967299m": "3",
"1;00000000000000003m": "",
// negative parameters
"-5m": "",
"-;3m": "3",
"4:-3m": "4:5",
"4:-8m": "",
"4:-1m": "",
"1;221m": "",
"2;222m": "",
"1;;:3m": "",
"58:2::1:2:3m": "58:2:1:2:3",
"38;2;1;2;3;4m": "4:1;38:2:1:2:3",
// underline styles
"4:3m": "4:3",
"4:9m": "4:5",
"21m": "4:2",
"4:3;24m": "",
"4;4:0m": "",
// sub-parameters of plain attributes are ignored
"1:2m": "1",
// misplaced sub-parameters discard the rest of the code
":1m": "",
"3;38;5;1:2;1m": "3;31",
// colon and semicolon forms can be mixed
"38;:5;1;3m": "1;3",
"38;5:200;1m": "1;38:5:200",
"38:5;200;1m": "1",
// out of range color indices wrap
"38;5;256m": "30",
"38;5;-1m": "38:5:255",
// sub-parameters after a color are not part of it
"38;2;1;2;3:4;1m": "38:2:1:2:3",
} {
var s sgr_state
s.apply_csi(raw)
@ -151,4 +204,14 @@ func TestSGRStateParsing(t *testing.T) {
t.Fatalf("Parsing SGR %q gave %q expected %q", raw, actual, expected)
}
}
// Codes with more than the maximum number of parameters are ignored
for _, n := range []int{max_csi_params, max_csi_params + 1} {
raw := strings.Repeat("1;", n-1) + "3m"
var s sgr_state
s.apply_csi(raw)
expected := utils.IfElse(n > max_csi_params, "", "1;3")
if actual := s.as_sgr(false); actual != expected {
t.Fatalf("Parsing SGR with %d parameters gave %q expected %q", n, actual, expected)
}
}
}