Skip to content

Commit

Permalink
feat(vt): use ansi.XParseColor
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Nov 21, 2024
1 parent 8be3b5a commit 08d3e3b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 56 deletions.
8 changes: 5 additions & 3 deletions vt/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions vt/go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
52 changes: 1 addition & 51 deletions vt/osc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

0 comments on commit 08d3e3b

Please sign in to comment.