Only render if view has actually changed

This commit is contained in:
Christian Rocha 2020-05-28 10:20:59 -04:00
parent e79ee747c9
commit 99df3d4226
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
1 changed files with 28 additions and 20 deletions

30
tea.go
View File

@ -48,6 +48,8 @@ type Program struct {
view View view View
mutex sync.Mutex mutex sync.Mutex
linesRendered int
contentRendered string
} }
// Quit is a command that tells the program to exit. // Quit is a command that tells the program to exit.
@ -81,7 +83,6 @@ func (p *Program) Start() error {
msgs = make(chan Msg) msgs = make(chan Msg)
errs = make(chan error) errs = make(chan error)
done = make(chan struct{}) done = make(chan struct{})
linesRendered int
) )
err := initTerminal() err := initTerminal()
@ -99,7 +100,7 @@ func (p *Program) Start() error {
} }
// Render initial view // Render initial view
linesRendered = p.render(model, linesRendered) p.render(model)
// Subscribe to user input // Subscribe to user input
go func() { go func() {
@ -152,28 +153,35 @@ func (p *Program) Start() error {
model, cmd = p.update(msg, model) // run update model, cmd = p.update(msg, model) // run update
cmds <- cmd // process command (if any) cmds <- cmd // process command (if any)
linesRendered = p.render(model, linesRendered) // render to terminal p.render(model) // render to terminal
} }
} }
} }
// Render a view to the terminal. Returns the number of lines rendered. // Render a view to the terminal. Returns the number of lines rendered.
func (p *Program) render(model Model, linesRendered int) int { func (p *Program) render(model Model) {
view := p.view(model) view := p.view(model)
p.mutex.Lock() // The view hasn't changed; no need to render
if view == p.contentRendered {
return
}
// We need to add carriage returns to ensure that the cursor travels to the p.contentRendered = view
// start of a column after a newline.
// Add carriage returns to ensure that the cursor travels to the start of a
// column after a newline. Keep in mind that this means that in the rest
// of the Tea program newlines should be a normal unix newline (\n).
view = strings.Replace(view, "\n", "\r\n", -1) view = strings.Replace(view, "\n", "\r\n", -1)
if linesRendered > 0 { p.mutex.Lock()
termenv.ClearLines(linesRendered) if p.linesRendered > 0 {
termenv.ClearLines(p.linesRendered)
} }
_, _ = io.WriteString(os.Stdout, view) _, _ = io.WriteString(os.Stdout, view)
p.mutex.Unlock() p.mutex.Unlock()
return strings.Count(view, "\r\n")
p.linesRendered = strings.Count(view, "\r\n")
} }
// AltScreen exits the altscreen. This is just a wrapper around the termenv // AltScreen exits the altscreen. This is just a wrapper around the termenv