1
0
mirror of https://github.com/gizak/termui.git synced 2025-04-26 13:48:54 +08:00
termui/_examples/tabpane.go
Caleb Bassi 08e68490f3 Add TextBox widget
- TextBox is a superset of Paragraph, so Paragraph is deprecated
- adds support for a cursor, which can be moved
- the `Text` field is now private, replaced with `SetText`, `InsertText`, and `ClearText` methods

TODO:

- adding focusable widgets support to termui might clean up the API since we could potentially move the event handling from client side to library side
- add support for scrolling the TextBox view
- fix the cursor position on linewrap
- fix linewrapping
- add line number support
2019-03-15 22:40:01 -07: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.NewTextBox()
header.SetText("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.NewTextBox()
p2.SetText("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()
}
}
}