termui-tree/main.go
2019-05-19 19:27:00 -04:00

61 lines
963 B
Go

package main
import (
"fmt"
"strings"
"github.com/gizak/termui/v3"
)
const maxDepth = 3
var offset = 0
func buildTree(depth int) *Node {
if depth > maxDepth {
return nil
}
text := strings.Repeat(fmt.Sprintf("%c ", 'A'+offset), 10)
node := &Node{
Text: text,
}
offset++
for i := 1; i <= 2; i++ {
child := buildTree(depth + 1)
if child != nil {
node.AddChild(child)
}
}
return node
}
func main() {
err := termui.Init()
if err != nil {
panic(err)
}
defer termui.Close()
tree := NewTreeWidget([]*Node{buildTree(0)})
width, height := termui.TerminalDimensions()
tree.SetRect(0, 0, width, height)
tree.Block.Border = true
tree.Block.Title = "hello world"
termui.Render(tree)
eventsChan := termui.PollEvents()
for event := range eventsChan {
if event.Type != termui.KeyboardEvent {
continue
}
switch event.ID {
case "<C-c>", "q":
return
case "<Space>":
tree.SelectNext()
termui.Render(tree)
}
}
}