termui-tree/main.go

65 lines
1 KiB
Go

package main
import (
"fmt"
"strings"
"github.com/gizak/termui/v3"
)
const maxDepth = 3
func buildTree(depth int) *Node {
offset := 0
var f func(int) *Node
f = func(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 := f(depth + 1)
if child != nil {
node.AddChild(child)
}
}
return node
}
return f(depth)
}
func main() {
err := termui.Init()
if err != nil {
panic(err)
}
defer termui.Close()
tree := NewTreeWidget([]*Node{buildTree(0), 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)
}
}
}