// Demo code for the Pages primitive.
package main
import (
"strings"
"time"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
)
const pageCount = 5
func main() {
app := tview.NewApplication()
pages := tview.NewPages()
/* Table */
table := tview.NewTable().
SetBorders(true)
lorem := strings.Split("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.", " ")
cols, rows := 10, 40
word := 0
for r := 0; r < rows; r++ {
for c := 0; c < cols; c++ {
color := tcell.ColorWhite
if c < 1 || r < 1 {
color = tcell.ColorYellow
}
table.SetCell(r, c,
tview.NewTableCell(lorem[word]).
SetTextColor(color).
SetAlign(tview.AlignCenter))
word = (word + 1) % len(lorem)
}
}
flexTable := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(tview.NewBox().SetBorder(true).SetTitle("AWS info"), 0, 1, false).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Searching"), 0, 2, false).
AddItem(table, 0, 2, false)
pages.AddPage("table", flexTable, true, false)
/* Form */
form := tview.NewForm().
AddDropDown("Title", []string{"Mr.", "Ms.", "Mrs.", "Dr.", "Prof."}, 0, nil).
AddInputField("First name", "", 20, nil, nil).
AddInputField("Last name", "", 20, nil, nil).
AddCheckbox("Age 18+", false, nil).
AddPasswordField("Password", "", 10, '*', nil).
AddButton("Quit", func() {
pages.SwitchToPage("table")
})
form.SetBorder(true).SetTitle("Enter some data").SetTitleAlign(tview.AlignLeft)
flex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(tview.NewBox().SetBorder(true).SetTitle("AWS info"), 0, 1, false).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Searching"), 0, 2, false).
AddItem(form, 0, 2, false)
pages.AddPage("form", flex, true, true)
/* Auto change from Form page to Table page after 5 seconds */
go func() {
time.Sleep(5 * time.Second)
pages.SwitchToPage("table")
}()
if err := app.SetRoot(pages, true).EnableMouse(true).Run(); err != nil {
panic(err)
}
}
I plan to change page automatically after period time from goroutine. But it doesn't work
Env:
Expected:
go run main.go
, after 5 second, Form page will switch to Table page Actual:It still works if we use button to switch page.
Code: