-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
191 lines (157 loc) · 4.17 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"fmt"
"github.com/awesome-gocui/gocui"
"math"
"net"
"strconv"
"strings"
)
var (
toolBarHeight = 2
itemWidth = 10
itemHeight = 4
itemPadding = 1
itemAspectRatio = 0.0
maxX = 0
maxY = 0
cols = 0
plotterX = 0
plotterY = 0
)
func initGui(config *Config) *gocui.Gui {
gui, err := gocui.NewGui(gocui.Output256, false)
if err != nil {
LogPanic(err.Error())
}
maxX, maxY = gui.Size()
gui.Mouse = true
initDimensions(len(config.Items))
createToolBarView(gui)
createMonitoringView(config.Items, gui)
if err := gui.SetKeybinding("", 'q', gocui.ModNone, quit); err != nil {
LogPanic(err.Error())
}
if err := gui.SetKeybinding("", gocui.KeyCtrlQ, gocui.ModNone, quit); err != nil {
LogPanic(err.Error())
}
return gui
}
func initDimensions(itemsCount int) {
itemsCount = itemsCount + itemsCount%4
width, height := maxX, maxY-toolBarHeight-itemPadding*2
space := width * height
itemAspectRatio = float64(height) / float64(width)
itemSpace := float64(space) / float64(itemsCount)
itemWidth = int(math.Sqrt(itemSpace / itemAspectRatio))
itemHeight = int(float64(itemWidth) * itemAspectRatio)
cols = width / itemWidth
if cols > 1 {
totalPadding := (cols - 1) * itemPadding
paddingRemainder := width%itemWidth - 1
if totalPadding > paddingRemainder {
itemWidth = itemWidth - int(math.Ceil(float64(totalPadding-paddingRemainder)/float64(cols)))
}
}
}
func createToolBarView(gui *gocui.Gui) {
view, err := gui.SetView("toolbar", plotterX, plotterY, maxX-1, plotterY+toolBarHeight, 0)
if !gocui.IsUnknownView(err) {
LogPanic(err.Error())
}
fmt.Fprintf(view, "%s v%s | IP: %s", prog, version, getOutboundIP())
plotterY += toolBarHeight + itemPadding
}
func createMonitoringView(items []*Item, gui *gocui.Gui) {
for i, item := range items {
if err := createMonitoringItemView(item, i, gui); err != nil {
LogPanic(err.Error())
}
}
plotterY += itemHeight + itemPadding
}
func createMonitoringItemView(item *Item, col int, gui *gocui.Gui) error {
row := 0
if col >= cols {
row = col / cols
col %= cols
}
xPadding := itemPadding
if col == 0 {
xPadding = 0
}
x := plotterX + col*(itemWidth+xPadding)
y := plotterY + row*(itemHeight+itemPadding)
view, err := gui.SetView(item.Label, x, y, x+itemWidth, y+itemHeight, 0)
if !gocui.IsUnknownView(err) {
return err
}
view.Frame = true
view.Title = item.Label
view.Wrap = true
view.Overwrite = true
fmt.Fprint(view, "\n\n loading")
// Bind mouse-click to item refresh.
gui.SetKeybinding(item.Label, gocui.MouseLeft, gocui.ModNone, func(gui *gocui.Gui, view *gocui.View) error {
updateItem(item, gui)
return nil
})
return nil
}
func updateItemView(item *Item, result string, gui *gocui.Gui) {
gui.Update(func(gui *gocui.Gui) error {
view, err := gui.View(item.Label)
if err != nil {
LogPanic(err.Error())
}
view.Clear()
fmt.Fprint(view, "\n\n ")
if result == ResultErr {
// Something bad happened, the service may not be available.
view.BgColor = ErrBgColor
view.FgColor = ErrFgColor
fmt.Fprintln(view, result)
} else {
// Got some response.
view.BgColor = OkBgColor
view.FgColor = OkFgColor
// Try to parse the result as float.
result = strings.Replace(result, ",", ".", 1)
floatResult, err := strconv.ParseFloat(result, 32)
if err == nil {
// We can apply threshold if available for this item.
if item.Threshold > 0 && float32(floatResult) >= item.Threshold {
view.BgColor = WarnBgColor
view.FgColor = WarnFgColor
}
fmt.Fprintf(view, "%.3f", floatResult)
} else {
fmt.Fprint(view, result)
}
if item.Unit != "" {
fmt.Fprintf(view, " %s\n", item.Unit)
}
}
return nil
})
}
func closeGui(gui *gocui.Gui) {
defer gui.Close()
}
func runMainLoop(gui *gocui.Gui) {
if err := gui.MainLoop(); err != nil && err != gocui.ErrQuit {
LogPanic(err.Error())
}
}
func quit(gui *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
func getOutboundIP() string {
conn, err := net.Dial("udp", "1.1.1.1:80")
if err != nil {
return "?"
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String()
}