Skip to content

Commit

Permalink
ANSI compression is now opt-in via the WithANSICompressor program option
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Oct 30, 2021
1 parent 56aa4ef commit 119144e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
11 changes: 11 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,14 @@ func WithoutRenderer() ProgramOption {
m.renderer = &nilRenderer{}
}
}

// WithANSICompressor removes redundant ANSI sequences to produce potentially
// smaller output, at the cost of some processing overhead.
//
// This feature is provisional, and may be changed removed in a future version
// of this package.
func WithANSICompressor() ProgramOption {
return func(p *Program) {
p.startupOptions |= withANSICompressor
}
}
39 changes: 25 additions & 14 deletions standard_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ const (
// In cases where very high performance is needed the renderer can be told
// to exclude ranges of lines, allowing them to be written to directly.
type standardRenderer struct {
out io.WriteCloser
buf bytes.Buffer
framerate time.Duration
ticker *time.Ticker
mtx *sync.Mutex
done chan struct{}
lastRender string
linesRendered int
out io.Writer
buf bytes.Buffer
framerate time.Duration
ticker *time.Ticker
mtx *sync.Mutex
done chan struct{}
lastRender string
linesRendered int
useANSICompressor bool

// essentially whether or not we're using the full size of the terminal
altScreenActive bool
Expand All @@ -45,12 +46,17 @@ type standardRenderer struct {

// newRenderer creates a new renderer. Normally you'll want to initialize it
// with os.Stdout as the first argument.
func newRenderer(out io.Writer, mtx *sync.Mutex) renderer {
return &standardRenderer{
out: &compressor.Writer{Forward: out},
mtx: mtx,
framerate: defaultFramerate,
func newRenderer(out io.Writer, mtx *sync.Mutex, useANSICompressor bool) renderer {
r := &standardRenderer{
out: out,
mtx: mtx,
framerate: defaultFramerate,
useANSICompressor: useANSICompressor,
}
if r.useANSICompressor {
r.out = &compressor.Writer{Forward: out}
}
return r
}

// start starts the renderer.
Expand All @@ -67,7 +73,12 @@ func (r *standardRenderer) stop() {
r.flush()
clearLine(r.out)
close(r.done)
r.out.Close()

if r.useANSICompressor {
if w, ok := r.out.(io.WriteCloser); ok {
_ = w.Close()
}
}
}

// kill halts the renderer. The final frame will not be rendered.
Expand Down
3 changes: 2 additions & 1 deletion tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const (
withMouseAllMotion
withInputTTY
withCustomInput
withANSICompressor
)

// Program is a terminal user interface.
Expand Down Expand Up @@ -360,7 +361,7 @@ func (p *Program) StartReturningModel() (Model, error) {

// If no renderer is set use the standard one.
if p.renderer == nil {
p.renderer = newRenderer(p.output, p.mtx)
p.renderer = newRenderer(p.output, p.mtx, p.startupOptions.has(withANSICompressor))
}

// Honor program startup options.
Expand Down

0 comments on commit 119144e

Please sign in to comment.