-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.go
84 lines (74 loc) · 1.45 KB
/
ui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"errors"
"fmt"
"strconv"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)
type nodeValue string
func (nv nodeValue) String() string {
return string(nv)
}
func generateTreeNodes(n int) ([]*widgets.TreeNode, error) {
hns, err := GetHackerNews(n)
if err != nil {
return nil, err
}
var nodes []*widgets.TreeNode
for _, hn := range hns {
fmt.Println(hn.Score)
node := widgets.TreeNode{
Value: nodeValue(hn.Title),
Nodes: []*widgets.TreeNode{
{
Value: nodeValue("Score: " + strconv.Itoa(hn.Score)),
Nodes: nil,
},
{
Value: nodeValue("Type: " + hn.Type),
Nodes: nil,
},
{
Value: nodeValue("Author: " + hn.By),
Nodes: nil,
},
{
Value: nodeValue("cmd+click → " + hn.URL),
Nodes: nil,
},
{
Value: nodeValue("Description"),
Nodes: []*widgets.TreeNode{
{
Value: nodeValue(hn.Description),
Nodes: nil,
},
},
},
},
}
nodes = append(nodes, &node)
}
return nodes, nil
}
func hnUI(n int) error {
if err := ui.Init(); err != nil {
return errors.New("failed to init")
}
defer ui.Close()
nodes, err := generateTreeNodes(n)
if err != nil {
return err
}
t := widgets.NewTree()
t.Title = "Hacker News ClI"
t.TextStyle = ui.NewStyle(ui.ColorYellow)
t.WrapText = false
t.SetNodes(nodes)
x, y := ui.TerminalDimensions()
t.SetRect(0, 0, x, y)
ui.Render(t)
Keybindings(t)
return nil
}