Skip to content

Commit

Permalink
chore: update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Nov 15, 2024
1 parent 4ca4b71 commit 56f5568
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 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.6-0.20241114164159-aea15a2cc929
github.com/charmbracelet/x/ansi v0.4.6-0.20241115012020-031d6fb4dd89
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
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/charmbracelet/x/ansi v0.4.6-0.20241114164159-aea15a2cc929 h1:CxDzlAZwEEcq5DNjRlx+RQ0acLKBg/J5ZmvW+db9kAc=
github.com/charmbracelet/x/ansi v0.4.6-0.20241114164159-aea15a2cc929/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/ansi v0.4.6-0.20241115012020-031d6fb4dd89 h1:70Ajh/OON1vof4LP1TiQmkIGxC/i3gfpc95RHmL5DYk=
github.com/charmbracelet/x/ansi v0.4.6-0.20241115012020-031d6fb4dd89/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
25 changes: 13 additions & 12 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,8 +81,9 @@ 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.Parameter(p.Params[i])
params := p.Params()
for i := 0; i < len(params); i++ {
param := params[i]

switch param := param.Param(0); param {
case 0:
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)
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.Parameter(params[i+1]); param.Param(0) {
switch param := params[i+1]; param.Param(0) {
case 2: // RGB
if i > paramsLen-4 {
return
}
c = color.RGBA{
R: uint8(ansi.Parameter(params[i+2]).Param(0)), //nolint:gosec
G: uint8(ansi.Parameter(params[i+3]).Param(0)), //nolint:gosec
B: uint8(ansi.Parameter(params[i+4]).Param(0)), //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.Parameter(params[i+2]).Param(0)) //nolint:gosec
c = ansi.ExtendedColor(params[i+2].Param(0)) //nolint:gosec
*idxp += 2
}
return
Expand Down

0 comments on commit 56f5568

Please sign in to comment.