-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
104 lines (82 loc) · 1.63 KB
/
main.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
package main
import (
"fmt"
"log"
"strings"
"time"
"os"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
"github.com/werkt/bf-client/client"
"github.com/werkt/bf-client/view"
tm "github.com/nsf/termbox-go"
)
type component interface {
open()
close()
handle(ui.Event)
update() component
render()
done() bool
}
type baseComponent struct {
a *client.App
v view.View
}
func (c *baseComponent) open() {
c.a.Connect()
}
func (c *baseComponent) close() {
c.a.Conn.Close()
}
func (c *baseComponent) handle(e ui.Event) {
c.v = c.v.Handle(e)
}
func (c *baseComponent) update() component {
c.v.Update()
return c
}
func (c baseComponent) render() {
w := c.v.Render()
f := widgets.NewParagraph()
f.Text = fmt.Sprintf("Fetches: %d", c.a.Fetches)
f.SetRect(0, 40, 20, 43)
ui.Render(append(w, f)...)
}
func (c baseComponent) done() bool {
return c.a.Done
}
func main() {
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
tm.SetInputMode(tm.InputEsc)
defer ui.Close()
redisHost, reapiHost := os.Args[1], os.Args[2]
var ca string
if len(os.Args) > 3 {
ca = os.Args[3]
}
if !strings.Contains(redisHost, ":") {
redisHost += ":6379"
}
a := client.NewApp(redisHost, reapiHost, ca)
var c component = &baseComponent {
a: a,
v: view.NewQueue(a, 3),
}
c.open()
uiEvents := ui.PollEvents()
ticker := time.NewTicker(time.Second / 60).C
for !c.done() {
select {
case e := <-uiEvents:
c.handle(e)
case <-ticker:
a.Fetches = 0
c = c.update()
c.render()
}
}
c.close()
}