Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
124 lines (102 loc) · 2.09 KB
/
Copy pathmain.go
File metadata and controls
124 lines (102 loc) · 2.09 KB
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
116
117
118
119
120
121
122
123
124
package main
import (
"fmt"
"log"
"os"
"time"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
"github.com/buildfarm/bf-client/client"
"github.com/buildfarm/bf-client/view"
tm "github.com/nsf/termbox-go"
)
type baseComponent struct {
a *client.App
v view.View
dimensions ui.Resize
done bool
}
func (c *baseComponent) Open(args []string) {
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
tm.SetInputMode(tm.InputEsc)
reapiHost := args[1]
var ca string
if len(args) > 2 {
ca = args[2]
}
c.a = client.NewApp(reapiHost, ca)
c.v = view.NewQueue(c, 3)
c.a.Connect()
}
func (c *baseComponent) Close() {
ui.Close()
c.a.Conn.Close()
}
func (c *baseComponent) Run() client.Component {
c.dimensions = view.UISize()
uiEvents := ui.PollEvents()
lastFrameLimit := c.a.FrameLimit
ticker := time.NewTicker(time.Second / time.Duration(c.a.FrameLimit)).C
for !c.done {
select {
case e := <-uiEvents:
c.handle(e)
if lastFrameLimit != c.a.FrameLimit {
ticker = time.NewTicker(time.Second / time.Duration(c.a.FrameLimit)).C
lastFrameLimit = c.a.FrameLimit
}
case <-ticker:
if c.a.UpdateCountdown == 0 {
c.a.UpdateCountdown = c.a.SkipFrames
c.a.Fetches = 0
c.update()
} else {
c.a.UpdateCountdown--
}
c.render()
}
}
return nil
}
func (c *baseComponent) App() *client.App {
return c.a
}
func (c *baseComponent) Finish() {
c.done = true
}
func (c *baseComponent) Dimensions() ui.Resize {
return c.dimensions
}
func (c *baseComponent) handle(e ui.Event) {
if e.ID == "<Resize>" {
c.dimensions = e.Payload.(ui.Resize)
}
c.v = c.v.Handle(e)
}
func (c *baseComponent) update() {
c.v.Update()
}
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 main() {
var c client.Component
c = &baseComponent {}
var nc client.Component = nil
for c != nil {
if nc != c {
c.Open(os.Args)
}
nc = c.Run()
if nc != c {
c.Close()
}
c = nc
}
}