-
Notifications
You must be signed in to change notification settings - Fork 14
/
ui.go
145 lines (133 loc) · 3.08 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"fmt"
"runtime"
"sync"
"time"
"unsafe"
"github.com/fumiama/terasu/ip"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)
type screen struct {
sync.RWMutex
sysinfo *widgets.Paragraph
logroll *widgets.List
speedln *widgets.Plot
prgbars []ui.Drawable
reusepg []*widgets.Gauge
currh, w int
totaldl int
lastclr time.Time
}
func newscreen() (s screen) {
w, h := ui.TerminalDimensions()
s.w = w
s.sysinfo = widgets.NewParagraph()
s.sysinfo.Title = "System Info"
s.sysinfo.BorderStyle.Fg = ui.ColorGreen
s.sysinfo.Text = fmt.Sprintf(
"[Time](mod:bold): %s\n[OS](mod:bold): %s, [Architecture](mod:bold): %s\n[Is IPv6 available](mod:bold): %v",
time.Now().Local().Format(time.DateTime+" (MST)"),
runtime.GOOS, runtime.GOARCH,
ip.IsIPv6Available.Get(),
)
s.sysinfo.SetRect(0, s.currh, w/2, s.currh+5)
s.currh += 5
s.logroll = widgets.NewList()
s.logroll.Title = "Logs"
s.logroll.BorderStyle.Fg = ui.ColorBlue
s.logroll.WrapText = false
s.logroll.SetRect(w/2, 0, w, h/2)
s.speedln = widgets.NewPlot()
s.speedln.Title = "Speed (MB/s)"
s.speedln.Data = make([][]float64, 1)
s.speedln.Data[0] = []float64{0, 0}
s.speedln.AxesColor = ui.ColorWhite
s.speedln.LineColors[0] = ui.ColorYellow
s.speedln.BorderStyle.Fg = ui.ColorYellow
s.speedln.SetRect(w/2, h/2, w, h)
return
}
func (s *screen) logwrite(sz int) {
s.Lock()
defer s.Unlock()
w := s.speedln.Size().X
s.totaldl += sz
tdiff := time.Since(s.lastclr)
if tdiff > time.Second {
s.speedln.Data[0] = append(s.speedln.Data[0],
float64(s.totaldl/1024)/1024/(float64(tdiff)/float64(time.Second)),
)
if len(s.speedln.Data[0]) > w-5 {
s.speedln.Data[0] = s.speedln.Data[0][1:]
}
s.totaldl = 0
s.lastclr = time.Now()
}
}
func (s *screen) show(interval time.Duration) {
t := time.NewTicker(interval)
defer t.Stop()
s.flush()
for {
select {
case e := <-ui.PollEvents():
s.flush()
if e.Type == ui.KeyboardEvent {
switch e.ID {
case "q", "<Escape>", "<C-c>":
return
}
}
case <-t.C:
s.flush()
}
}
}
func (s *screen) flush() {
s.RLock()
defer s.RUnlock()
ui.Render(s.sysinfo, s.logroll, s.speedln)
if len(s.prgbars) > 0 {
ui.Render(s.prgbars...)
}
}
func (s *screen) addfile(name string, size int) *widgets.Gauge {
s.Lock()
defer s.Unlock()
var g *widgets.Gauge
if len(s.reusepg) > 0 {
b := len(s.reusepg) - 1
g = s.reusepg[b]
s.reusepg = s.reusepg[:b]
} else {
g = widgets.NewGauge()
g.SetRect(0, s.currh, s.w/2, s.currh+3)
s.currh += 3
}
g.Title = name
g.Label = fmt.Sprintf("0%% (0MB/%dMB)", size/1024/1024)
s.prgbars = append(s.prgbars, g)
return g
}
func (s *screen) removefile(g *widgets.Gauge) {
s.Lock()
defer s.Unlock()
for i, obj := range s.prgbars {
if *(**widgets.Gauge)(unsafe.Add(
unsafe.Pointer(&obj), unsafe.Sizeof(uintptr(0)),
)) == g {
switch i {
case 0:
s.prgbars = s.prgbars[1:]
case len(s.prgbars) - 1:
s.prgbars = s.prgbars[:len(s.prgbars)-1]
default:
s.prgbars = append(s.prgbars[:i], s.prgbars[i+1:]...)
}
s.reusepg = append(s.reusepg, g)
return
}
}
}