forked from Mirrors/bubbletea
Only render if view has actually changed
This commit is contained in:
parent
e79ee747c9
commit
99df3d4226
30
tea.go
30
tea.go
|
@ -48,6 +48,8 @@ type Program struct {
|
|||
view View
|
||||
|
||||
mutex sync.Mutex
|
||||
linesRendered int
|
||||
contentRendered string
|
||||
}
|
||||
|
||||
// Quit is a command that tells the program to exit.
|
||||
|
@ -81,7 +83,6 @@ func (p *Program) Start() error {
|
|||
msgs = make(chan Msg)
|
||||
errs = make(chan error)
|
||||
done = make(chan struct{})
|
||||
linesRendered int
|
||||
)
|
||||
|
||||
err := initTerminal()
|
||||
|
@ -99,7 +100,7 @@ func (p *Program) Start() error {
|
|||
}
|
||||
|
||||
// Render initial view
|
||||
linesRendered = p.render(model, linesRendered)
|
||||
p.render(model)
|
||||
|
||||
// Subscribe to user input
|
||||
go func() {
|
||||
|
@ -152,28 +153,35 @@ func (p *Program) Start() error {
|
|||
|
||||
model, cmd = p.update(msg, model) // run update
|
||||
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.
|
||||
func (p *Program) render(model Model, linesRendered int) int {
|
||||
func (p *Program) render(model 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
|
||||
// start of a column after a newline.
|
||||
p.contentRendered = view
|
||||
|
||||
// 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)
|
||||
|
||||
if linesRendered > 0 {
|
||||
termenv.ClearLines(linesRendered)
|
||||
p.mutex.Lock()
|
||||
if p.linesRendered > 0 {
|
||||
termenv.ClearLines(p.linesRendered)
|
||||
}
|
||||
_, _ = io.WriteString(os.Stdout, view)
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue