forked from Mirrors/bubbletea
Remove an extraneous argument and auto-add a trailing newline to views
This commit is contained in:
parent
e92a2fe783
commit
c1b4d6a515
|
@ -35,7 +35,7 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
|
||||||
|
|
||||||
func view(mdl tea.Model) string {
|
func view(mdl tea.Model) string {
|
||||||
m, _ := mdl.(model)
|
m, _ := mdl.(model)
|
||||||
return fmt.Sprintf("Hi. This program will exit in %d seconds...\n", m)
|
return fmt.Sprintf("Hi. This program will exit in %d seconds...", m)
|
||||||
}
|
}
|
||||||
|
|
||||||
func tick(_ tea.Model) tea.Msg {
|
func tick(_ tea.Model) tea.Msg {
|
||||||
|
|
10
tea.go
10
tea.go
|
@ -85,7 +85,7 @@ func (p *Program) Start() error {
|
||||||
|
|
||||||
// Render initial view
|
// Render initial view
|
||||||
hideCursor()
|
hideCursor()
|
||||||
p.render(model, true)
|
p.render(model)
|
||||||
|
|
||||||
// Subscribe to user input
|
// Subscribe to user input
|
||||||
// TODO: should we move this to the end-user program level or just keep this
|
// TODO: should we move this to the end-user program level or just keep this
|
||||||
|
@ -138,21 +138,21 @@ func (p *Program) Start() error {
|
||||||
|
|
||||||
model, cmd = p.update(msg, model)
|
model, cmd = p.update(msg, model)
|
||||||
cmds <- cmd // process command (if any)
|
cmds <- cmd // process command (if any)
|
||||||
p.render(model, false)
|
p.render(model)
|
||||||
p.model = model
|
p.model = model
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render a view to the terminal
|
// Render a view to the terminal
|
||||||
func (p *Program) render(model Model, init bool) {
|
func (p *Program) render(model Model) {
|
||||||
view := p.view(model)
|
view := p.view(model) + "\n"
|
||||||
|
|
||||||
// We need to add carriage returns to ensure that the cursor travels to the
|
// We need to add carriage returns to ensure that the cursor travels to the
|
||||||
// start of a column after a newline
|
// start of a column after a newline
|
||||||
view = strings.Replace(view, "\n", "\r\n", -1)
|
view = strings.Replace(view, "\n", "\r\n", -1)
|
||||||
|
|
||||||
if !init {
|
if linesRendered > 0 {
|
||||||
clearLines(linesRendered)
|
clearLines(linesRendered)
|
||||||
}
|
}
|
||||||
io.WriteString(p.rw, view)
|
io.WriteString(p.rw, view)
|
||||||
|
|
Loading…
Reference in New Issue