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
Provenance
- Commit
87434a2- Tree
e6dfaba- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
4ce9b4e
4 files changed+116 added−36 removed
modified:examples/go.mod+1 added-1 removed
6 unmodified lines7 require (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 lines9 github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=10 github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=11 github.com/muesli/termenv v0.5.2 h1:N1Y1dHRtx6OizOgaIQXd8SkJl4T/cCOV+YyWXiuLUEA=12 github.com/muesli/termenv v0.5.2/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=13+github.com/muesli/termenv v0.5.3-0.20200526053627-d728968dcf83 h1:AfshZBlqAwhCZ27NJ1aPlMcPBihF1squ1GpaollhLQk=14+github.com/muesli/termenv v0.5.3-0.20200526053627-d728968dcf83/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=15 github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03 h1:pd4YKIqCB0U7O2I4gWHgEUA2mCEOENmco0l/bM957bU=16 github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03/go.mod h1:Z9+Ul5bCbBKnbCvdOWbLqTHhJiYV414CURZJba6L8qA=17 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=18 golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw=4 unmodified lines23 golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=24 golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=25 golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4=26 golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=27+golang.org/x/sys v0.0.0-20200523222454-059865788121 h1:rITEj+UZHYC927n8GT97eC3zrpzXdb/voyeOuVKS46o=28+golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=29 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=added:renderer.go+99 added
1+package tea2+3+import (4+ "bytes"5+ "io"6+ "sync"7+ "time"8+9+ "github.com/muesli/termenv"10+)11+12+const (13+ defaultFramerate = time.Millisecond * 1614+)15+16+type 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+}26+27+func newRenderer(out io.Writer) *renderer {28+ return &renderer{29+ out: out,30+ framerate: defaultFramerate,31+ }32+}33+34+func (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+}41+42+func (r *renderer) stop() {43+ r.flush()44+ r.done <- struct{}{}45+}46+47+func (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+}64+65+func (r *renderer) flush() {66+ if r.buf.Len() == 0 || r.buf.String() == r.lastRender {67+ // Nothing to do68+ return69+ }70+71+ r.mtx.Lock()72+ defer r.mtx.Unlock()73+74+ if r.linesRendered > 0 {75+ termenv.ClearLines(r.linesRendered)76+ }77+ r.linesRendered = 078+79+ 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+ }89+90+ _, _ = r.out.Write(out.Bytes())91+ r.lastRender = r.buf.String()92+ r.buf.Reset()93+}94+95+func (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
1 package tea2 3 import (4- "io"4 "os"6- "strings"5 "sync"6 7 "github.com/muesli/termenv"8 )64 unmodified lines73 74 // Start initializes the program.75 func (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 )85 86 err := initTerminal()87 if err != nil {8 unmodified lines96 cmds <- cmd97 }()98 }99 100+ // Start renderer101+ mrRenderer.start()102+103 // Render initial view102- p.render(model)104+ mrRenderer.write(p.view(model))105 106 // Subscribe to user input107 go func() {108 for {44 unmodified lines153 }154 155 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 }161 160-// Render a view to the terminal. Returns the number of lines rendered.161-func (p *Program) render(model Model) {162- view := p.view(model)163-164- // The view hasn't changed; no need to render165- if view == p.currentRender {166- return167- }168-169- p.currentRender = view170- linesRendered := strings.Count(p.currentRender, "\n")171-172- // 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)176-177- p.mutex.Lock()178- if linesRendered > 0 {179- termenv.ClearLines(linesRendered)180- }181- _, _ = io.WriteString(os.Stdout, view)182- p.mutex.Unlock()183-}184-162 // AltScreen exits the altscreen. This is just a wrapper around the termenv163 // function.164 func AltScreen() {165 termenv.AltScreen()7 unmodified lines