mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-19 06:35:15 +00:00
Cleanup previous PR
- Treat trailing empty SGR color parameters as zero, matching the terminal - Ignore the color space id in colon form RGB colors (38:2:id:r:g:b) - Fix bright colors (90-97, 100-107, 38;5;8-15) being re-emitted off by one - Handle parse errors explicitly and simplify color target selection - Add tests for SGR state parsing
This commit is contained in:
parent
f8e5ec9345
commit
b4b186a6ad
3 changed files with 91 additions and 18 deletions
|
|
@ -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:<id>: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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue