cview/demos/table/main.go

46 lines
1.6 KiB
Go
Raw Normal View History

2018-01-03 20:13:32 +00:00
// Demo code for the Table primitive.
2017-12-27 15:05:00 +00:00
package main
import (
"strings"
2020-08-30 15:36:03 +00:00
"github.com/gdamore/tcell/v2"
2020-01-22 23:28:19 +00:00
"gitlab.com/tslocum/cview"
2017-12-27 15:05:00 +00:00
)
func main() {
app := cview.NewApplication()
table := cview.NewTable().
2017-12-27 15:05:00 +00:00
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++ {
2020-08-30 15:36:03 +00:00
color := tcell.ColorWhite.TrueColor()
2017-12-27 15:05:00 +00:00
if c < 1 || r < 1 {
2020-08-30 15:36:03 +00:00
color = tcell.ColorYellow.TrueColor()
2017-12-27 15:05:00 +00:00
}
table.SetCell(r, c,
cview.NewTableCell(lorem[word]).
SetTextColor(color).
SetAlign(cview.AlignCenter))
2017-12-27 15:05:00 +00:00
word = (word + 1) % len(lorem)
}
}
2017-12-29 21:27:10 +00:00
table.Select(0, 0).SetFixed(1, 1).SetDoneFunc(func(key tcell.Key) {
2017-12-27 15:05:00 +00:00
if key == tcell.KeyEscape {
app.Stop()
}
if key == tcell.KeyEnter {
table.SetSelectable(true, true)
}
}).SetSelectedFunc(func(row int, column int) {
2020-08-30 15:36:03 +00:00
table.GetCell(row, column).SetTextColor(tcell.ColorRed.TrueColor())
2017-12-27 15:05:00 +00:00
table.SetSelectable(false, false)
})
if err := app.SetRoot(table, true).EnableMouse(true).Run(); err != nil {
2017-12-27 15:05:00 +00:00
panic(err)
}
}