Skip to content

Commit

Permalink
chore: update dependencies
Browse files Browse the repository at this point in the history
* chore: use the latest version of ansi package from vt

* chore: update dependencies
  • Loading branch information
aymanbagabas authored Nov 21, 2024
1 parent 37d661c commit d619dad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/charmbracelet/colorprofile
go 1.18

require (
github.com/charmbracelet/x/ansi v0.4.5
github.com/charmbracelet/x/ansi v0.5.0
github.com/charmbracelet/x/term v0.2.1
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/charmbracelet/x/ansi v0.4.5 h1:LqK4vwBNaXw2AyGIICa5/29Sbdq58GbGdFngSexTdRM=
github.com/charmbracelet/x/ansi v0.4.5/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
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/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
Expand Down
29 changes: 15 additions & 14 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (w *Writer) downsample(p []byte) (int, error) {
seq, _, read, newState := ansi.DecodeSequence(p, state, parser)

switch {
case ansi.HasCsiPrefix(seq) && parser.Cmd == 'm':
case ansi.HasCsiPrefix(seq) && parser.Cmd() == 'm':
handleSgr(w, parser, &buf)
default:
// If we're not a style SGR sequence, just write the bytes.
Expand All @@ -81,11 +81,12 @@ func (w *Writer) WriteString(s string) (n int, err error) {

func handleSgr(w *Writer, p *ansi.Parser, buf *bytes.Buffer) {
var style ansi.Style
for i := 0; i < p.ParamsLen; i++ {
param := ansi.Param(p.Params[i])
params := p.Params()
for i := 0; i < len(params); i++ {
param := params[i]

switch param := param.Param(); param {
case -1:
switch param := param.Param(0); param {
case 0:
// SGR default parameter is 0. We use an empty string to reduce the
// number of bytes written to the buffer.
style = append(style, "")
Expand All @@ -96,7 +97,7 @@ func handleSgr(w *Writer, p *ansi.Parser, buf *bytes.Buffer) {
style = style.ForegroundColor(
w.Profile.Convert(ansi.BasicColor(param - 30))) //nolint:gosec

Check failure on line 98 in writer.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

Magic number: 30, in <argument> detected (mnd)

Check failure on line 98 in writer.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

Magic number: 30, in <argument> detected (mnd)

Check failure on line 98 in writer.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

Magic number: 30, in <argument> detected (mnd)

Check failure on line 98 in writer.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

Magic number: 30, in <argument> detected (mnd)

Check failure on line 98 in writer.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

Magic number: 30, in <argument> detected (mnd)

Check failure on line 98 in writer.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

Magic number: 30, in <argument> detected (mnd)
case 38: // 16 or 24-bit foreground color
c := readColor(&i, p.Params)
c := readColor(&i, params)
if w.Profile > ANSI {
continue
}
Expand All @@ -113,7 +114,7 @@ func handleSgr(w *Writer, p *ansi.Parser, buf *bytes.Buffer) {
style = style.BackgroundColor(
w.Profile.Convert(ansi.BasicColor(param - 40))) //nolint:gosec
case 48: // 16 or 24-bit background color
c := readColor(&i, p.Params)
c := readColor(&i, params)
if w.Profile > ANSI {
continue
}
Expand All @@ -124,7 +125,7 @@ func handleSgr(w *Writer, p *ansi.Parser, buf *bytes.Buffer) {
}
style = style.DefaultBackgroundColor()
case 58: // 16 or 24-bit underline color
c := readColor(&i, p.Params)
c := readColor(&i, params)
if w.Profile > ANSI {
continue
}
Expand Down Expand Up @@ -155,30 +156,30 @@ func handleSgr(w *Writer, p *ansi.Parser, buf *bytes.Buffer) {
_, _ = buf.WriteString(style.String())
}

func readColor(idxp *int, params []int) (c ansi.Color) {
func readColor(idxp *int, params []ansi.Parameter) (c ansi.Color) {
i := *idxp
paramsLen := len(params)
if i > paramsLen-1 {
return
}
// Note: we accept both main and subparams here
switch param := ansi.Param(params[i+1]); param.Param() {
switch param := params[i+1]; param.Param(0) {
case 2: // RGB
if i > paramsLen-4 {
return
}
c = color.RGBA{
R: uint8(ansi.Param(params[i+2]).Param()), //nolint:gosec
G: uint8(ansi.Param(params[i+3]).Param()), //nolint:gosec
B: uint8(ansi.Param(params[i+4]).Param()), //nolint:gosec
R: uint8(params[i+2].Param(0)), //nolint:gosec
G: uint8(params[i+3].Param(0)), //nolint:gosec
B: uint8(params[i+4].Param(0)), //nolint:gosec
A: 0xff,
}
*idxp += 4
case 5: // 256 colors
if i > paramsLen-2 {
return
}
c = ansi.ExtendedColor(ansi.Param(params[i+2]).Param()) //nolint:gosec
c = ansi.ExtendedColor(params[i+2].Param(0)) //nolint:gosec
*idxp += 2
}
return
Expand Down

0 comments on commit d619dad

Please sign in to comment.