-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTUICreator.go
115 lines (95 loc) · 2.43 KB
/
TUICreator.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"github.com/marcusolsson/tui-go"
"github.com/mmcdole/gofeed"
"log"
"os/exec"
"regexp"
"strconv"
)
func createTUI(feeds []*gofeed.Feed) tui.UI {
var currView int
var tables []*tui.Table
var labels []*tui.Label
var boxes []*tui.Box
var views []tui.Widget
for _, feed := range feeds {
table := createTable()
table = fillTable(table, feed)
tables = append(tables, table)
label := createLabel()
labels = append(labels, label)
box := createScreen(table, label, feed.Title)
boxes = append(boxes, box)
views = append(views, box)
onSelectionChanged(table, feed, label)
onItemActivated(table, feed)
}
ui, err := tui.New(views[0])
if err != nil {
log.Fatal(err)
}
ui.SetKeybinding("Esc", func() { ui.Quit() })
ui.SetKeybinding("q", func() { ui.Quit() })
ui.SetKeybinding("Left", func() {
currView = clamp(currView-1, 0, len(views)-1)
ui.SetWidget(views[currView])
})
ui.SetKeybinding("Right", func() {
currView = clamp(currView+1, 0, len(views)-1)
ui.SetWidget(views[currView])
})
return ui
}
func createScreen(t *tui.Table, l *tui.Label, title string) *tui.Box {
tableBox := tui.NewVBox(t)
tableBox.SetBorder(true)
labelBox := tui.NewVBox(l)
labelBox.SetBorder(true)
rootBox := tui.NewVBox(tableBox, labelBox)
tableBox.SetTitle(title)
return rootBox
}
func createLabel() *tui.Label {
article := tui.NewLabel("")
article.SetWordWrap(true)
return article
}
func createTable() *tui.Table {
table := tui.NewTable(0, 0)
table.SetFocused(true)
return table
}
func fillTable(t *tui.Table, items *gofeed.Feed) *tui.Table {
for i, item := range items.Items {
t.AppendRow(tui.NewLabel(strconv.Itoa(i) + " " + item.Title))
}
return t
}
func onItemActivated(table *tui.Table, feed *gofeed.Feed) {
table.OnItemActivated(func(t *tui.Table) {
item := t.Selected()
articleLink := feed.Items[item].Link
cmd := exec.Command("firefox", articleLink)
cmd.Start()
})
}
func onSelectionChanged(table *tui.Table, feed *gofeed.Feed, label *tui.Label) {
table.OnSelectionChanged(func(t *tui.Table) {
item := t.Selected()
htmlTags, _ := regexp.Compile("<.*?>")
articleDescription := feed.Items[item].Description
articleDescription = cleanup(feed.Title, articleDescription)
articleDescription = htmlTags.ReplaceAllString(articleDescription, "")
label.SetText(articleDescription)
})
}
func clamp(n, min, max int) int {
if n < min {
return max
}
if n > max {
return min
}
return n
}