mirror of
https://github.com/walles/moar.git
synced 2024-11-30 02:34:13 +03:00
Sanity check input
This commit is contained in:
parent
3f35a545f9
commit
7b935e7c49
@ -2,6 +2,7 @@ package m
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@ -213,7 +214,7 @@ func _UpdateStyle(logger *log.Logger, style tcell.Style, escapeSequence string)
|
|||||||
case "38":
|
case "38":
|
||||||
var err error = nil
|
var err error = nil
|
||||||
var color *tcell.Color
|
var color *tcell.Color
|
||||||
index, color, err = consumeCompositeColor(numbers, index-1, style)
|
index, color, err = consumeCompositeColor(numbers, index-1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Printf("Foreground: %s", err.Error())
|
logger.Printf("Foreground: %s", err.Error())
|
||||||
return style
|
return style
|
||||||
@ -242,7 +243,7 @@ func _UpdateStyle(logger *log.Logger, style tcell.Style, escapeSequence string)
|
|||||||
case "48":
|
case "48":
|
||||||
var err error = nil
|
var err error = nil
|
||||||
var color *tcell.Color
|
var color *tcell.Color
|
||||||
index, color, err = consumeCompositeColor(numbers, index-1, style)
|
index, color, err = consumeCompositeColor(numbers, index-1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Printf("Background: %s", err.Error())
|
logger.Printf("Background: %s", err.Error())
|
||||||
return style
|
return style
|
||||||
@ -259,13 +260,20 @@ func _UpdateStyle(logger *log.Logger, style tcell.Style, escapeSequence string)
|
|||||||
return style
|
return style
|
||||||
}
|
}
|
||||||
|
|
||||||
// numbers is from a ANSI SGR string
|
// numbers is a list of numbers from a ANSI SGR string
|
||||||
// index points to either 38 or 48 in that string
|
// index points to either 38 or 48 in that string
|
||||||
// style is the base style that this function will modify
|
|
||||||
//
|
//
|
||||||
// This method will return:
|
// This method will return:
|
||||||
// * The first index in the string that this function did not consume
|
// * The first index in the string that this function did not consume
|
||||||
// * A color value that can be applied to a style
|
// * A color value that can be applied to a style
|
||||||
func consumeCompositeColor(numbers []string, index int, style tcell.Style) (int, *tcell.Color, error) {
|
func consumeCompositeColor(numbers []string, index int) (int, *tcell.Color, error) {
|
||||||
|
if numbers[index] != "38" && numbers[index] != "48" {
|
||||||
|
err := fmt.Errorf(
|
||||||
|
"Unknown start of color sequence <%s>, expected 38 (foreground) or 48 (background): <CSI %sm>",
|
||||||
|
numbers[index],
|
||||||
|
strings.Join(numbers[index:], ";"))
|
||||||
|
return -1, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return -1, nil, errors.New("Unimplemented")
|
return -1, nil, errors.New("Unimplemented")
|
||||||
}
|
}
|
||||||
|
@ -52,13 +52,13 @@ func TestTokenize(t *testing.T) {
|
|||||||
func TestConsumeCompositeColorHappy(t *testing.T) {
|
func TestConsumeCompositeColorHappy(t *testing.T) {
|
||||||
// 8 bit color
|
// 8 bit color
|
||||||
// Example from: https://github.com/walles/moar/issues/14
|
// Example from: https://github.com/walles/moar/issues/14
|
||||||
newIndex, color, err := consumeCompositeColor([]string{"38", "5", "74"}, 0, tcell.StyleDefault)
|
newIndex, color, err := consumeCompositeColor([]string{"38", "5", "74"}, 0)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.Equal(t, newIndex, 3)
|
assert.Equal(t, newIndex, 3)
|
||||||
assert.Equal(t, color, tcell.Color74)
|
assert.Equal(t, color, tcell.Color74)
|
||||||
|
|
||||||
// 24 bit color
|
// 24 bit color
|
||||||
newIndex, color, err = consumeCompositeColor([]string{"38", "2", "10", "20", "30"}, 0, tcell.StyleDefault)
|
newIndex, color, err = consumeCompositeColor([]string{"38", "2", "10", "20", "30"}, 0)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.Equal(t, newIndex, 3)
|
assert.Equal(t, newIndex, 3)
|
||||||
assert.Equal(t, color, tcell.NewRGBColor(10, 20, 30))
|
assert.Equal(t, color, tcell.NewRGBColor(10, 20, 30))
|
||||||
@ -67,13 +67,13 @@ func TestConsumeCompositeColorHappy(t *testing.T) {
|
|||||||
func TestConsumeCompositeColorHappyMidSequence(t *testing.T) {
|
func TestConsumeCompositeColorHappyMidSequence(t *testing.T) {
|
||||||
// 8 bit color
|
// 8 bit color
|
||||||
// Example from: https://github.com/walles/moar/issues/14
|
// Example from: https://github.com/walles/moar/issues/14
|
||||||
newIndex, color, err := consumeCompositeColor([]string{"whatever", "38", "5", "74"}, 1, tcell.StyleDefault)
|
newIndex, color, err := consumeCompositeColor([]string{"whatever", "38", "5", "74"}, 1)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.Equal(t, newIndex, 4)
|
assert.Equal(t, newIndex, 4)
|
||||||
assert.Equal(t, color, tcell.Color74)
|
assert.Equal(t, color, tcell.Color74)
|
||||||
|
|
||||||
// 24 bit color
|
// 24 bit color
|
||||||
newIndex, color, err = consumeCompositeColor([]string{"whatever", "38", "2", "10", "20", "30"}, 1, tcell.StyleDefault)
|
newIndex, color, err = consumeCompositeColor([]string{"whatever", "38", "2", "10", "20", "30"}, 1)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
assert.Equal(t, newIndex, 4)
|
assert.Equal(t, newIndex, 4)
|
||||||
assert.Equal(t, color, tcell.NewRGBColor(10, 20, 30))
|
assert.Equal(t, color, tcell.NewRGBColor(10, 20, 30))
|
||||||
@ -82,46 +82,46 @@ func TestConsumeCompositeColorHappyMidSequence(t *testing.T) {
|
|||||||
func TestConsumeCompositeColorBadPrefix(t *testing.T) {
|
func TestConsumeCompositeColorBadPrefix(t *testing.T) {
|
||||||
// 8 bit color
|
// 8 bit color
|
||||||
// Example from: https://github.com/walles/moar/issues/14
|
// Example from: https://github.com/walles/moar/issues/14
|
||||||
_, color, err := consumeCompositeColor([]string{"29"}, 0, tcell.StyleDefault)
|
_, color, err := consumeCompositeColor([]string{"29"}, 0)
|
||||||
assert.Equal(t, err.Error(), "Unknown start of color sequence <29>, expected 38 (foreground) or 48 (background): <CSI 29m>")
|
assert.Equal(t, err.Error(), "Unknown start of color sequence <29>, expected 38 (foreground) or 48 (background): <CSI 29m>")
|
||||||
assert.Equal(t, color, nil)
|
assert.Equal(t, color, nil)
|
||||||
|
|
||||||
// Same test but mid-sequence, with initial index > 0
|
// Same test but mid-sequence, with initial index > 0
|
||||||
_, color, err = consumeCompositeColor([]string{"whatever", "29"}, 1, tcell.StyleDefault)
|
_, color, err = consumeCompositeColor([]string{"whatever", "29"}, 1)
|
||||||
assert.Equal(t, err.Error(), "Unknown start of color sequence <29>, expected 38 (foreground) or 48 (background): <CSI 29m>")
|
assert.Equal(t, err.Error(), "Unknown start of color sequence <29>, expected 38 (foreground) or 48 (background): <CSI 29m>")
|
||||||
assert.Equal(t, color, nil)
|
assert.Equal(t, color, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConsumeCompositeColorBadType(t *testing.T) {
|
func TestConsumeCompositeColorBadType(t *testing.T) {
|
||||||
_, color, err := consumeCompositeColor([]string{"38", "4"}, 0, tcell.StyleDefault)
|
_, color, err := consumeCompositeColor([]string{"38", "4"}, 0)
|
||||||
// https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
|
// https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
|
||||||
assert.Equal(t, err.Error(), "Unknown color type <4>, expected 5 (8 bit color) or 2 (24 bit color): <CSI 38;4m>")
|
assert.Equal(t, err.Error(), "Unknown color type <4>, expected 5 (8 bit color) or 2 (24 bit color): <CSI 38;4m>")
|
||||||
assert.Equal(t, color, nil)
|
assert.Equal(t, color, nil)
|
||||||
|
|
||||||
// Same test but mid-sequence, with initial index > 0
|
// Same test but mid-sequence, with initial index > 0
|
||||||
_, color, err = consumeCompositeColor([]string{"whatever", "38", "4"}, 1, tcell.StyleDefault)
|
_, color, err = consumeCompositeColor([]string{"whatever", "38", "4"}, 1)
|
||||||
assert.Equal(t, err.Error(), "Unknown color type <4>, expected 5 (8 bit color) or 2 (24 bit color): <CSI 38;4m>")
|
assert.Equal(t, err.Error(), "Unknown color type <4>, expected 5 (8 bit color) or 2 (24 bit color): <CSI 38;4m>")
|
||||||
assert.Equal(t, color, nil)
|
assert.Equal(t, color, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConsumeCompositeColorIncomplete8Bit(t *testing.T) {
|
func TestConsumeCompositeColorIncomplete8Bit(t *testing.T) {
|
||||||
_, color, err := consumeCompositeColor([]string{"38", "5"}, 0, tcell.StyleDefault)
|
_, color, err := consumeCompositeColor([]string{"38", "5"}, 0)
|
||||||
assert.Equal(t, err.Error(), "Incomplete 8 bit color sequence: <CSI 38;5m>")
|
assert.Equal(t, err.Error(), "Incomplete 8 bit color sequence: <CSI 38;5m>")
|
||||||
assert.Equal(t, color, nil)
|
assert.Equal(t, color, nil)
|
||||||
|
|
||||||
// Same test, mid-sequence
|
// Same test, mid-sequence
|
||||||
_, color, err = consumeCompositeColor([]string{"whatever", "38", "5"}, 1, tcell.StyleDefault)
|
_, color, err = consumeCompositeColor([]string{"whatever", "38", "5"}, 1)
|
||||||
assert.Equal(t, err.Error(), "Incomplete 8 bit color sequence: <CSI 38;5m>")
|
assert.Equal(t, err.Error(), "Incomplete 8 bit color sequence: <CSI 38;5m>")
|
||||||
assert.Equal(t, color, nil)
|
assert.Equal(t, color, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConsumeCompositeColorIncomplete24Bit(t *testing.T) {
|
func TestConsumeCompositeColorIncomplete24Bit(t *testing.T) {
|
||||||
_, color, err := consumeCompositeColor([]string{"38", "2", "10", "20"}, 0, tcell.StyleDefault)
|
_, color, err := consumeCompositeColor([]string{"38", "2", "10", "20"}, 0)
|
||||||
assert.Equal(t, err.Error(), "Incomplete 24 bit color sequence, expected N8;2;R;G;Bm: <CSI 38;2;10;20m>")
|
assert.Equal(t, err.Error(), "Incomplete 24 bit color sequence, expected N8;2;R;G;Bm: <CSI 38;2;10;20m>")
|
||||||
assert.Equal(t, color, nil)
|
assert.Equal(t, color, nil)
|
||||||
|
|
||||||
// Same test, mid-sequence
|
// Same test, mid-sequence
|
||||||
_, color, err = consumeCompositeColor([]string{"whatever", "38", "2", "10", "20"}, 1, tcell.StyleDefault)
|
_, color, err = consumeCompositeColor([]string{"whatever", "38", "2", "10", "20"}, 1)
|
||||||
assert.Equal(t, err.Error(), "Incomplete 24 bit color sequence, expected N8;2;R;G;Bm: <CSI 38;2;10;20m>")
|
assert.Equal(t, err.Error(), "Incomplete 24 bit color sequence, expected N8;2;R;G;Bm: <CSI 38;2;10;20m>")
|
||||||
assert.Equal(t, color, nil)
|
assert.Equal(t, color, nil)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user