termui/_examples/tabs.go

75 lines
1.6 KiB
Go
Raw Normal View History

2017-01-14 14:07:43 +08:00
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
2016-01-27 09:45:18 +08:00
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
2015-06-17 05:16:37 +08:00
// +build ignore
2015-05-26 02:55:34 +08:00
package main
import (
2019-01-24 12:12:10 +08:00
"log"
2019-03-07 18:50:20 +08:00
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
2015-05-26 02:55:34 +08:00
)
func main() {
2019-01-24 12:12:10 +08:00
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
2018-09-07 05:48:09 +08:00
defer ui.Close()
2015-05-26 02:55:34 +08:00
2019-01-24 12:12:10 +08:00
header := widgets.NewParagraph()
header.Text = "Press q to quit, Press h or l to switch tabs"
header.SetRect(0, 0, 50, 1)
2016-11-03 13:41:47 +08:00
header.Border = false
2019-01-24 12:12:10 +08:00
header.TextStyle.Bg = ui.ColorBlue
2015-05-26 02:55:34 +08:00
2019-01-24 12:12:10 +08:00
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
2015-05-26 02:55:34 +08:00
2019-01-24 12:12:10 +08:00
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"}
2015-05-26 02:55:34 +08:00
2019-01-24 12:12:10 +08:00
tabpane := widgets.NewTabPane("pierwszy", "drugi", "trzeci", "żółw", "four", "five")
tabpane.SetRect(0, 1, 50, 4)
2016-11-03 13:41:47 +08:00
tabpane.Border = true
2015-05-26 02:55:34 +08:00
2019-01-24 12:12:10 +08:00
renderTab := func() {
switch tabpane.ActiveTabIndex {
case 0:
ui.Render(p2)
case 1:
ui.Render(bc)
}
}
2015-05-26 02:55:34 +08:00
2019-01-24 12:12:10 +08:00
ui.Render(header, tabpane, p2)
2015-10-21 09:45:28 +08:00
uiEvents := ui.PollEvents()
2019-01-24 12:12:10 +08:00
2018-11-29 10:19:34 +08:00
for {
e := <-uiEvents
2018-11-29 10:19:34 +08:00
switch e.ID {
case "q", "<C-c>":
return
case "h":
2019-01-24 12:12:10 +08:00
tabpane.FocusLeft()
2018-11-29 10:19:34 +08:00
ui.Clear()
ui.Render(header, tabpane)
2019-01-24 12:12:10 +08:00
renderTab()
2018-11-29 10:19:34 +08:00
case "l":
2019-01-24 12:12:10 +08:00
tabpane.FocusRight()
2018-11-29 10:19:34 +08:00
ui.Clear()
ui.Render(header, tabpane)
2019-01-24 12:12:10 +08:00
renderTab()
2018-11-29 10:19:34 +08:00
}
}
2015-05-26 02:55:34 +08:00
}