termui/_examples/tabs.go

75 lines
1.6 KiB
Go
Raw Normal View History

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