Skip to content

Commit

Permalink
chore: upgrade x/exp/term and use the latest ansi parser (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas authored Apr 3, 2024
1 parent 05a366c commit 887dc08
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
36 changes: 24 additions & 12 deletions ansi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/beevik/etree"
"github.com/charmbracelet/x/exp/term/ansi"
"github.com/mattn/go-runewidth"
)

Expand All @@ -18,6 +19,17 @@ type dispatcher struct {
bgWidth int
}

func (p *dispatcher) dispatch(s ansi.Sequence) {
switch s := s.(type) {
case ansi.Rune:
p.Print(rune(s))
case ansi.ControlCode:
p.Execute(byte(s))
case ansi.CsiSequence:
p.CsiDispatch(s)
}
}

func (p *dispatcher) Print(r rune) {
p.row = clamp(p.row, 0, len(p.lines)-1)
// insert the rune in the last tspan
Expand Down Expand Up @@ -97,8 +109,8 @@ func (p *dispatcher) endBackground() {
p.bgWidth = 0
}

func (p *dispatcher) CsiDispatch(marker byte, params [][]uint, inter byte, final byte, ignore bool) {
if ignore || final != 'm' || marker != 0 {
func (p *dispatcher) CsiDispatch(s ansi.CsiSequence) {
if s.Cmd != 'm' {
// ignore incomplete or non Style (SGR) sequences
return
}
Expand All @@ -113,15 +125,15 @@ func (p *dispatcher) CsiDispatch(marker byte, params [][]uint, inter byte, final
p.endBackground()
}

if len(params) == 0 {
if len(s.Params) == 0 {
// zero params means reset
reset()
return
}

var i int
for i < len(params) {
v := params[i][0]
for i < len(s.Params) {
v := s.Param(i)
switch v {
case 0:
reset()
Expand All @@ -142,29 +154,29 @@ func (p *dispatcher) CsiDispatch(marker byte, params [][]uint, inter byte, final
p.lines[p.row].AddChild(span)
case 38:
i++
switch params[i][0] {
switch s.Param(i) {
case 5:
n := params[i+1][0]
n := s.Param(i + 1)
i++
fill := palette[n]
span.CreateAttr("fill", fill)
p.lines[p.row].AddChild(span)
case 2:
span.CreateAttr("fill", fmt.Sprintf("#%02x%02x%02x", params[i+1][0], params[i+2][0], params[i+3][0]))
span.CreateAttr("fill", fmt.Sprintf("#%02x%02x%02x", s.Param(i+1), s.Param(i+2), s.Param(i+3)))
p.lines[p.row].AddChild(span)
i += 3
}
case 48:
p.endBackground()
i++
switch params[i][0] {
switch s.Param(i) {
case 5:
n := params[i+1][0]
n := s.Param(i + 1)
i++
fill := palette[n]
p.beginBackground(fill)
case 2:
fill := fmt.Sprintf("#%02x%02x%02x", params[i+1][0], params[i+2][0], params[i+3][0])
fill := fmt.Sprintf("#%02x%02x%02x", s.Param(i+1), s.Param(i+2), s.Param(i+3))
p.beginBackground(fill)
i += 3
}
Expand All @@ -175,7 +187,7 @@ func (p *dispatcher) CsiDispatch(marker byte, params [][]uint, inter byte, final
}
}

var ansiPalette = map[uint]string{
var ansiPalette = map[int]string{
30: "#282a2e", // black
31: "#D74E6F", // red
32: "#31BB71", // green
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/charmbracelet/huh v0.3.1-0.20240327025511-ec643317aa10
github.com/charmbracelet/lipgloss v0.10.1-0.20240325130315-f16ea2bdcb88
github.com/charmbracelet/log v0.4.0
github.com/charmbracelet/x/exp/term v0.0.0-20240327191656-1e1cd98f30d4
github.com/charmbracelet/x/exp/term v0.0.0-20240403043919-dea9035a27d4
github.com/creack/pty v1.1.21
github.com/kanrichan/resvg-go v0.0.2-0.20231001163256-63db194ca9f5
github.com/mattn/go-isatty v0.0.20
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ github.com/charmbracelet/x/errors v0.0.0-20240117030013-d31dba354651 h1:3RXpZWGW
github.com/charmbracelet/x/errors v0.0.0-20240117030013-d31dba354651/go.mod h1:2P0UgXMEa6TsToMSuFqKFQR+fZTO9CNGUNokkPatT/0=
github.com/charmbracelet/x/exp/strings v0.0.0-20240327191656-1e1cd98f30d4 h1:E3qdydPjQJIGnT6t6lY0P70sou+Vmh+VcQHAYR4qTtA=
github.com/charmbracelet/x/exp/strings v0.0.0-20240327191656-1e1cd98f30d4/go.mod h1:pBhA0ybfXv6hDjQUZ7hk1lVxBiUbupdw5R31yPUViVQ=
github.com/charmbracelet/x/exp/term v0.0.0-20240327191656-1e1cd98f30d4 h1:WckoQq/mT+RkBq1JaCWNp65snXleZzQEEu/c3AL0TTU=
github.com/charmbracelet/x/exp/term v0.0.0-20240327191656-1e1cd98f30d4/go.mod h1:madZtB2OVDOG+ZnLruGITVZceYy047W+BLQ1MNQzbWg=
github.com/charmbracelet/x/exp/term v0.0.0-20240403043919-dea9035a27d4 h1:LewLBFkff+bCxgMZn1m8xNYQbUksWaY71d1QARHA11s=
github.com/charmbracelet/x/exp/term v0.0.0-20240403043919-dea9035a27d4/go.mod h1:6GZ13FjIP6eOCqWU4lqgveGnYxQo9c3qBzHPeFu4HBE=
github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=
github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0=
Expand Down
14 changes: 8 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/charmbracelet/x/exp/term/ansi"
"github.com/charmbracelet/x/exp/term/ansi/parser"
"github.com/mattn/go-isatty"
)

Expand Down Expand Up @@ -339,13 +340,14 @@ func main() {
svg.SetDimensions(terminal, terminalWidth, terminalHeight)

if isAnsi {
parser := ansi.Parser{
Print: d.Print,
Execute: d.Execute,
CsiDispatch: d.CsiDispatch,
}
parser := ansi.NewParser(parser.MaxParamsSize, 0)
// parser := ansi.Parser{
// Print: d.Print,
// Execute: d.Execute,
// CsiDispatch: d.CsiDispatch,
// }
for _, line := range strings.Split(input, "\n") {
parser.Parse([]byte(line))
parser.Parse(d.dispatch, []byte(line))
d.Execute(ansi.LF) // simulate a newline
}
}
Expand Down

0 comments on commit 887dc08

Please sign in to comment.