Skip to content

Commit

Permalink
fix: incomplete ansi escape codes break kitty terminal (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 authored Sep 17, 2024
1 parent 1d41267 commit 90e6d02
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 36 deletions.
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.22

require (
github.com/awalterschulze/gographviz v2.0.3+incompatible
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.27.0
github.com/charmbracelet/lipgloss v0.12.1
github.com/charmbracelet/bubbles v0.20.0
github.com/charmbracelet/bubbletea v1.1.0
github.com/charmbracelet/lipgloss v0.13.0
github.com/charmbracelet/x/exp/teatest v0.0.0-20240329185201-62a6965a9fad
github.com/davecgh/go-spew v1.1.1
github.com/go-logfmt/logfmt v0.6.0
Expand All @@ -16,6 +16,7 @@ require (
github.com/leg100/go-runewidth v0.0.16-0.20240513191656-9e28d2bebd46
github.com/leg100/reflow v0.0.0-20240513191534-e77d7e432a72
github.com/mitchellh/iochan v1.0.0
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6
github.com/otiai10/copy v1.14.0
github.com/peterbourgon/ff/v4 v4.0.0-alpha.4
github.com/stretchr/testify v1.9.0
Expand All @@ -35,8 +36,8 @@ require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymanbagabas/go-udiff v0.2.0 // indirect
github.com/charmbracelet/x/ansi v0.2.3 // indirect
github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8 // indirect
github.com/charmbracelet/x/ansi v0.3.1 // indirect
github.com/charmbracelet/x/exp/golden v0.0.0-20240815200342-61de596daa2b // indirect
github.com/charmbracelet/x/term v0.2.0 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/fatih/color v1.17.0 // indirect
Expand All @@ -56,7 +57,6 @@ require (
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/panicwrap v1.0.0 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -67,7 +67,7 @@ require (
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
24 changes: 12 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB
github.com/bmatcuk/doublestar v1.1.5/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0=
github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw=
github.com/charmbracelet/bubbletea v0.27.0 h1:Mznj+vvYuYagD9Pn2mY7fuelGvP0HAXtZYGgRBCbHvU=
github.com/charmbracelet/bubbletea v0.27.0/go.mod h1:5MdP9XH6MbQkgGhnlxUqCNmBXf9I74KRQ8HIidRxV1Y=
github.com/charmbracelet/lipgloss v0.12.1 h1:/gmzszl+pedQpjCOH+wFkZr/N90Snz40J/NR7A0zQcs=
github.com/charmbracelet/lipgloss v0.12.1/go.mod h1:V2CiwIuhx9S1S1ZlADfOj9HmxeMAORuz5izHb0zGbB8=
github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqosGDEY=
github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8 h1:kyT+aGp1z5jwlus3OY0cP6FuT05jYeeExx/4TYxnyrs=
github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQWD9LIutE=
github.com/charmbracelet/bubbles v0.20.0/go.mod h1:39slydyswPy+uVOHZ5x/GjwVAFkCsV8IIVy+4MhzwwU=
github.com/charmbracelet/bubbletea v1.1.0 h1:FjAl9eAL3HBCHenhz/ZPjkKdScmaS5SK69JAK2YJK9c=
github.com/charmbracelet/bubbletea v1.1.0/go.mod h1:9Ogk0HrdbHolIKHdjfFpyXJmiCzGwy+FesYkZr7hYU4=
github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=
github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY=
github.com/charmbracelet/x/ansi v0.3.1 h1:CRO6lc/6HCx2/D6S/GZ87jDvRvk6GtPyFP+IljkNtqI=
github.com/charmbracelet/x/ansi v0.3.1/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/exp/golden v0.0.0-20240815200342-61de596daa2b h1:MnAMdlwSltxJyULnrYbkZpp4k58Co7Tah3ciKhSNo0Q=
github.com/charmbracelet/x/exp/golden v0.0.0-20240815200342-61de596daa2b/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
github.com/charmbracelet/x/exp/teatest v0.0.0-20240329185201-62a6965a9fad h1:7NyOJ06kOnx24u3Qx5WWaLz9wP6lBP1B49celRIH/78=
github.com/charmbracelet/x/exp/teatest v0.0.0-20240329185201-62a6965a9fad/go.mod h1:SG24wGkG/mix5V2dZLXfQ6Bod43HGvk9CkTDxATwKN4=
github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0=
Expand Down Expand Up @@ -691,8 +691,8 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
4 changes: 2 additions & 2 deletions internal/tui/task/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,12 @@ func (m model) getOutput() tea.Msg {
} else if err != nil {
return tui.ReportError(errors.New("reading task output"))()
}
msg.output = string(m.buf[:n])
msg.output = m.buf[:n]
return msg
}

type outputMsg struct {
modelID uuid.UUID
output string
output []byte
eof bool
}
38 changes: 24 additions & 14 deletions internal/tui/viewport.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package tui

import (
"bytes"
"fmt"
"io"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner"
Expand All @@ -10,7 +12,8 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/hokaccha/go-prettyjson"
"github.com/leg100/pug/internal/tui/keys"
"github.com/leg100/reflow/wrap"
"github.com/leg100/reflow/wordwrap"
"github.com/muesli/ansi"
)

// Viewport is a wrapper of the upstream viewport bubble.
Expand All @@ -19,7 +22,7 @@ type Viewport struct {

Autoscroll bool

content string
content []byte
json bool
spinner *spinner.Model
}
Expand Down Expand Up @@ -89,7 +92,7 @@ func (m Viewport) View() string {
Render(percent)

var output string
if m.content == "" {
if len(m.content) == 0 {
msg := "Awaiting output"
if m.spinner != nil {
msg += " " + m.spinner.View()
Expand Down Expand Up @@ -119,24 +122,21 @@ func (m *Viewport) SetDimensions(width, height int) {
}
}

func (m *Viewport) AppendContent(content string, finished bool) (err error) {
m.content += content
func (m *Viewport) AppendContent(content []byte, finished bool) (err error) {
m.content = append(m.content, content...)
if finished {
if m.content == "" {
m.content = "No output"
if len(m.content) == 0 {
m.content = []byte("No output")
} else if m.json {
// Prettify JSON output from task. This can only be done once
// the task has finished and has produced complete and
// syntactically valid json object(s).
//
// TODO: avoid casting to string and back, thereby avoiding
// unnecessary allocations.
if b, fmterr := prettyjson.Format([]byte(m.content)); fmterr != nil {
if b, fmterr := prettyjson.Format(m.content); fmterr != nil {
// In the event of an error, still set unprettified content
// below.
err = fmt.Errorf("pretty printing json content: %w", fmterr)
} else {
m.content = string(b)
m.content = b
}
}
}
Expand All @@ -148,6 +148,16 @@ func (m *Viewport) AppendContent(content string, finished bool) (err error) {
}

func (m *Viewport) setContent() {
wrapped := wrap.String(m.content, m.viewport.Width)
m.viewport.SetContent(wrapped)
// First ensure no incomplete ANSI escape codes are written by using the raw
// ANSI writer. And secondly, wrap content to the width of the viewport,
// whilst respecting ANSI escape codes (i.e. don't split codes across
// lines).
wrapper := wordwrap.NewWriter(m.viewport.Width)
w := &ansi.Writer{Forward: wrapper}
if _, err := io.Copy(w, bytes.NewReader(m.content)); err != nil {
m.viewport.SetContent(fmt.Sprintf("unable to parse ANSI escape codes: %s", err.Error()))
return
}
w.ResetAnsi()
m.viewport.SetContent(wrapper.String())
}
2 changes: 1 addition & 1 deletion internal/tui/workspace/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (mm *ResourceMaker) Make(id resource.ID, width, height int) (tea.Model, err
Height: m.viewportHeight(height),
JSON: true,
})
m.viewport.AppendContent(string(marshaled), true)
m.viewport.AppendContent(marshaled, true)

return m, nil
}
Expand Down

0 comments on commit 90e6d02

Please sign in to comment.