I'm using tview in the Go language.
I want to use the following code to display "hoge" on the terminal, but it does not show up.
package main
import (
"fmt"
"github.com/rivo/tview"
)
func main() {
tui := newTui()
tui.Run()
tui.WriteMessage("hoge")
}
type Tui struct {
app *tview.Application
text *tview.TextView
}
func (t *Tui) Run() {
t.app.Run()
}
func (t *Tui) WriteMessage(message string) {
fmt.Fprintln(t.text, message)
}
func newTui() *Tui {
text := tview.NewTextView()
app := tview.NewApplication()
app.SetRoot(text, true)
text.SetChangedFunc(func() { app.Draw() })
tui := &Tui{app: app, text: text}
return tui
}
I don't want to update the text in the newTui()
function.
How do I get it to show up?
Run
starts the application and thus the event loop. This function returns whenStop()
was called.
i.e. The statement tui.WriteMessage("hoge")
in your program is never reached because Run()
doesn't return until stopped explicitly. So to see hoge
printed in the terminal, you must call tui.WriteMessage("hoge")
before Run()
.
func main() {
tui := newTui()
tui.WriteMessage("hoge")
tui.Run()
}