diff --git a/vt/go.mod b/vt/go.mod index cb15f4fa..a1ba4007 100644 --- a/vt/go.mod +++ b/vt/go.mod @@ -3,10 +3,12 @@ module github.com/charmbracelet/x/vt go 1.19 require ( - github.com/charmbracelet/x/ansi v0.5.0 + github.com/charmbracelet/x/ansi v0.5.1 github.com/charmbracelet/x/wcwidth v0.0.0-20241011142426-46044092ad91 - github.com/lucasb-eyer/go-colorful v1.2.0 github.com/rivo/uniseg v0.4.7 ) -require golang.org/x/text v0.20.0 // indirect +require ( + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + golang.org/x/text v0.20.0 // indirect +) diff --git a/vt/go.sum b/vt/go.sum index 2c26e852..3df2f344 100644 --- a/vt/go.sum +++ b/vt/go.sum @@ -1,5 +1,5 @@ -github.com/charmbracelet/x/ansi v0.5.0 h1:o94cKkWJRaiNQYbfz5WOLrbg9hULaryyKvWm+oLX7js= -github.com/charmbracelet/x/ansi v0.5.0/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= +github.com/charmbracelet/x/ansi v0.5.1 h1:+mg6abP9skvsu/JQZrIJ9Z/4O1YDnLVkpfutar3dUnc= +github.com/charmbracelet/x/ansi v0.5.1/go.mod h1:KBUFw1la39nl0dLl10l5ORDAqGXaeurTQmwyyVKse/Q= github.com/charmbracelet/x/wcwidth v0.0.0-20241011142426-46044092ad91 h1:D5OO0lVavz7A+Swdhp62F9gbkibxmz9B2hZ/jVdMPf0= github.com/charmbracelet/x/wcwidth v0.0.0-20241011142426-46044092ad91/go.mod h1:Ey8PFmYwH+/td9bpiEx07Fdx9ZVkxfIjWXxBluxF4Nw= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= diff --git a/vt/osc.go b/vt/osc.go index 1c6be47f..563326e5 100644 --- a/vt/osc.go +++ b/vt/osc.go @@ -3,11 +3,8 @@ package vt import ( "bytes" "image/color" - "strconv" - "strings" "github.com/charmbracelet/x/ansi" - "github.com/lucasb-eyer/go-colorful" ) // handleOsc handles an OSC escape sequence. @@ -77,7 +74,7 @@ func (t *Terminal) handleOsc(seq ansi.OscSequence) { t.buf.WriteString(enc(ansi.XRGBColorizer{Color: col})) } } else { - col := xParseColor(string(parts[1])) + col := ansi.XParseColor(string(parts[1])) if col == nil { return } @@ -104,50 +101,3 @@ func (t *Terminal) handleOsc(seq ansi.OscSequence) { t.logf("unhandled OSC: %s", seq) } } - -type shiftable interface { - ~uint | ~uint16 | ~uint32 | ~uint64 -} - -func shift[T shiftable](x T) T { - if x > 0xff { - x >>= 8 - } - return x -} - -func xParseColor(s string) color.Color { - switch { - case strings.HasPrefix(s, "#"): - c, err := colorful.Hex(s) - if err != nil { - return nil - } - - return c - case strings.HasPrefix(s, "rgb:"): - parts := strings.Split(s[4:], "/") - if len(parts) != 3 { - return nil - } - - r, _ := strconv.ParseUint(parts[0], 16, 32) - g, _ := strconv.ParseUint(parts[1], 16, 32) - b, _ := strconv.ParseUint(parts[2], 16, 32) - - return color.RGBA{uint8(shift(r)), uint8(shift(g)), uint8(shift(b)), 255} //nolint:gosec - case strings.HasPrefix(s, "rgba:"): - parts := strings.Split(s[5:], "/") - if len(parts) != 4 { - return nil - } - - r, _ := strconv.ParseUint(parts[0], 16, 32) - g, _ := strconv.ParseUint(parts[1], 16, 32) - b, _ := strconv.ParseUint(parts[2], 16, 32) - a, _ := strconv.ParseUint(parts[3], 16, 32) - - return color.RGBA{uint8(shift(r)), uint8(shift(g)), uint8(shift(b)), uint8(shift(a))} //nolint:gosec - } - return nil -}