Skip to content
Myr

Buffer/ticker-based renderer

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Rocha · committed by Christian Rocha · parent 4ce9b4e · Browse files · 4 files+116 added−36 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+116 added−36 removed

  • other:+116 added−36 removed

4 files+116 added−36 removed

modified:examples/go.mod+1 added-1 removed
6 unmodified lines
7require (8 github.com/charmbracelet/bubbles v0.0.0-20200526000837-87c7cd778f809 github.com/charmbracelet/bubbletea v0.6.4-0.20200525234836-3b8b011b5a2610 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf177611 github.com/muesli/termenv v0.5.211 github.com/muesli/termenv v0.5.3-0.20200526053627-d728968dcf8312)
modified:examples/go.sum+4 added
8 unmodified lines
9github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=10github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=11github.com/muesli/termenv v0.5.2 h1:N1Y1dHRtx6OizOgaIQXd8SkJl4T/cCOV+YyWXiuLUEA=12github.com/muesli/termenv v0.5.2/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=13github.com/muesli/termenv v0.5.3-0.20200526053627-d728968dcf83 h1:AfshZBlqAwhCZ27NJ1aPlMcPBihF1squ1GpaollhLQk=14github.com/muesli/termenv v0.5.3-0.20200526053627-d728968dcf83/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=15github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03 h1:pd4YKIqCB0U7O2I4gWHgEUA2mCEOENmco0l/bM957bU=16github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03/go.mod h1:Z9+Ul5bCbBKnbCvdOWbLqTHhJiYV414CURZJba6L8qA=17golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=18golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw=
4 unmodified lines
23golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=24golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=25golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4=26golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=27golang.org/x/sys v0.0.0-20200523222454-059865788121 h1:rITEj+UZHYC927n8GT97eC3zrpzXdb/voyeOuVKS46o=28golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=29golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
added:renderer.go+99 added
1package tea23import (4 "bytes"5 "io"6 "sync"7 "time"89 "github.com/muesli/termenv"10)1112const (13 defaultFramerate = time.Millisecond * 1614)1516type renderer struct {17 out io.Writer18 buf bytes.Buffer19 framerate time.Duration20 ticker *time.Ticker21 mtx sync.Mutex22 done chan struct{}23 lastRender string24 linesRendered int25}2627func newRenderer(out io.Writer) *renderer {28 return &renderer{29 out: out,30 framerate: defaultFramerate,31 }32}3334func (r *renderer) start() {35 if r.ticker == nil {36 r.ticker = time.NewTicker(r.framerate)37 }38 r.done = make(chan struct{})39 go r.listen()40}4142func (r *renderer) stop() {43 r.flush()44 r.done <- struct{}{}45}4647func (r *renderer) listen() {48 for {49 select {50 case <-r.ticker.C:51 if r.ticker != nil {52 r.flush()53 }54 case <-r.done:55 r.mtx.Lock()56 r.ticker.Stop()57 r.ticker = nil58 r.mtx.Unlock()59 close(r.done)60 return61 }62 }63}6465func (r *renderer) flush() {66 if r.buf.Len() == 0 || r.buf.String() == r.lastRender {67 // Nothing to do68 return69 }7071 r.mtx.Lock()72 defer r.mtx.Unlock()7374 if r.linesRendered > 0 {75 termenv.ClearLines(r.linesRendered)76 }77 r.linesRendered = 07879 var out bytes.Buffer80 for _, b := range r.buf.Bytes() {81 if b == '\n' {82 r.linesRendered++83 out.Write([]byte("\r\n"))84 } else {85 // TODO: don't write past the terminal width86 _, _ = out.Write([]byte{b})87 }88 }8990 _, _ = r.out.Write(out.Bytes())91 r.lastRender = r.buf.String()92 r.buf.Reset()93}9495func (w *renderer) write(s string) {96 w.mtx.Lock()97 defer w.mtx.Unlock()98 w.buf.WriteString(s)99}
modified:tea.go+12 added-35 removed
1package tea23import (4 "io"4 "os"6 "strings"5 "sync"67 "github.com/muesli/termenv"8)
64 unmodified lines
7374// Start initializes the program.75func (p *Program) Start() error {76 var (79 model Model80 cmd Cmd81 cmds = make(chan Cmd)82 msgs = make(chan Msg)83 errs = make(chan error)84 done = make(chan struct{})77 model Model78 cmd Cmd79 cmds = make(chan Cmd)80 msgs = make(chan Msg)81 errs = make(chan error)82 done = make(chan struct{})83 mrRenderer = newRenderer(os.Stdout)84 )8586 err := initTerminal()87 if err != nil {
8 unmodified lines
96 cmds <- cmd97 }()98 }99100 // Start renderer101 mrRenderer.start()102103 // Render initial view102 p.render(model)104 mrRenderer.write(p.view(model))105106 // Subscribe to user input107 go func() {108 for {
44 unmodified lines
153 }154155 model, cmd = p.update(msg, model) // run update156 cmds <- cmd // process command (if any)155 p.render(model) // render to terminal157 mrRenderer.write(p.view(model)) // send to renderer158 }159 }160}161160// Render a view to the terminal. Returns the number of lines rendered.161func (p *Program) render(model Model) {162 view := p.view(model)163164 // The view hasn't changed; no need to render165 if view == p.currentRender {166 return167 }168169 p.currentRender = view170 linesRendered := strings.Count(p.currentRender, "\n")171172 // Add carriage returns to ensure that the cursor travels to the start of a173 // column after a newline. Keep in mind that this means that in the rest174 // of the Tea program newlines should be a normal unix newline (\n).175 view = strings.Replace(view, "\n", "\r\n", -1)176177 p.mutex.Lock()178 if linesRendered > 0 {179 termenv.ClearLines(linesRendered)180 }181 _, _ = io.WriteString(os.Stdout, view)182 p.mutex.Unlock()183}184162// AltScreen exits the altscreen. This is just a wrapper around the termenv163// function.164func AltScreen() {165 termenv.AltScreen()
7 unmodified lines