termui/_examples/tabs.go
Caleb Bassi 70fa7b8887 v3.0.0 🎉
Fix install command
2019-03-07 03:42:49 -08:00

75 lines
1.6 KiB
Go

// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
// +build ignore
package main
import (
"log"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)
func main() {
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
header := widgets.NewParagraph()
header.Text = "Press q to quit, Press h or l to switch tabs"
header.SetRect(0, 0, 50, 1)
header.Border = false
header.TextStyle.Bg = ui.ColorBlue
p2 := widgets.NewParagraph()
p2.Text = "Press q to quit\nPress h or l to switch tabs\n"
p2.Title = "Keys"
p2.SetRect(5, 5, 40, 15)
p2.BorderStyle.Fg = ui.ColorYellow
bc := widgets.NewBarChart()
bc.Title = "Bar Chart"
bc.Data = []float64{3, 2, 5, 3, 9, 5, 3, 2, 5, 8, 3, 2, 4, 5, 3, 2, 5, 7, 5, 3, 2, 6, 7, 4, 6, 3, 6, 7, 8, 3, 6, 4, 5, 3, 2, 4, 6, 4, 8, 5, 9, 4, 3, 6, 5, 3, 6}
bc.SetRect(5, 5, 35, 10)
bc.Labels = []string{"S0", "S1", "S2", "S3", "S4", "S5"}
tabpane := widgets.NewTabPane("pierwszy", "drugi", "trzeci", "żółw", "four", "five")
tabpane.SetRect(0, 1, 50, 4)
tabpane.Border = true
renderTab := func() {
switch tabpane.ActiveTabIndex {
case 0:
ui.Render(p2)
case 1:
ui.Render(bc)
}
}
ui.Render(header, tabpane, p2)
uiEvents := ui.PollEvents()
for {
e := <-uiEvents
switch e.ID {
case "q", "<C-c>":
return
case "h":
tabpane.FocusLeft()
ui.Clear()
ui.Render(header, tabpane)
renderTab()
case "l":
tabpane.FocusRight()
ui.Clear()
ui.Render(header, tabpane)
renderTab()
}
}
}