tview/demos/table/main.go

46 lines
1.6 KiB
Go
Raw Normal View History

2018-01-04 04:13:32 +08:00
// Demo code for the Table primitive.
2017-12-27 23:05:00 +08:00
package main
import (
"strings"
"github.com/gdamore/tcell/v2"
2017-12-27 23:05:00 +08:00
"github.com/rivo/tview"
)
func main() {
app := tview.NewApplication()
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))
2017-12-27 23:05:00 +08:00
word = (word + 1) % len(lorem)
}
}
2017-12-30 05:27:10 +08:00
table.Select(0, 0).SetFixed(1, 1).SetDoneFunc(func(key tcell.Key) {
2017-12-27 23:05:00 +08:00
if key == tcell.KeyEscape {
app.Stop()
}
if key == tcell.KeyEnter {
table.SetSelectable(true, true)
}
}).SetSelectedFunc(func(row int, column int) {
table.GetCell(row, column).SetTextColor(tcell.ColorRed)
2017-12-27 23:05:00 +08:00
table.SetSelectable(false, false)
})
if err := app.SetRoot(table, true).EnableMouse(true).Run(); err != nil {
2017-12-27 23:05:00 +08:00
panic(err)
}
}