forked from blockpane/pvtop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.go
114 lines (100 loc) · 2.24 KB
/
term.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
package prevotes
import (
"fmt"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
"log"
"os"
"time"
)
func DrawScreen(network string, voteChan chan []VoteState, pctChan chan float64, summaryChan chan string) {
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
pctGuage := widgets.NewGauge()
p := widgets.NewParagraph()
p.Title = network
lists := make([]*widgets.List, 3)
for i := range lists {
lists[i] = widgets.NewList()
lists[i].Border = false
}
grid := ui.NewGrid()
termWidth, termHeight := ui.TerminalDimensions()
grid.SetRect(0, 0, termWidth, termHeight)
grid.Set(
ui.NewRow(0.1,
ui.NewCol(1.0/2, p),
ui.NewCol(1.0/2, pctGuage),
),
ui.NewRow(0.9,
ui.NewCol(.9/3, lists[0]),
ui.NewCol(.9/3, lists[1]),
ui.NewCol(1.2/3, lists[2]),
),
)
ui.Render(grid)
refresh := false
tick := time.NewTicker(100 * time.Millisecond)
uiEvents := ui.PollEvents()
for {
select {
case <-tick.C:
if !refresh {
continue
}
refresh = false
ui.Render(grid)
case e := <-uiEvents:
switch e.ID {
case "q", "<C-c>":
ui.Clear()
ui.Close()
os.Exit(0)
case "<Resize>":
payload := e.Payload.(ui.Resize)
grid.SetRect(0, 0, payload.Width, payload.Height)
ui.Clear()
ui.Render(grid)
}
case votes := <-voteChan:
refresh = true
split, max := splitVotes(votes)
for i := 0; i < max; i++ {
lists[i].Rows = make([]string, len(split[i]))
for j, voter := range split[i] {
missing := "❌"
if voter.Voted {
missing = "✅"
}
lists[i].Rows[j] = fmt.Sprintf("%-3s %s", missing, voter.Description)
}
}
case pct := <-pctChan:
refresh = true
pctGuage.Percent = int(pct * 100)
case summary := <-summaryChan:
refresh = true
p.Text = summary
}
}
}
func splitVotes(votes []VoteState) ([][]VoteState, int) {
split := make([][]VoteState, 0)
var max int
switch {
case len(votes) < 50:
max = 1
split = append(split, votes)
case len(votes) < 100:
max = 2
split = append(split, votes[:50])
split = append(split, votes[50:])
default:
max = 3
split = append(split, votes[:50])
split = append(split, votes[50:100])
split = append(split, votes[100:])
}
return split, max
}