@@ -3,14 +3,20 @@ package main
3
3
import (
4
4
"fmt"
5
5
"github.com/jroimartin/gocui"
6
+ "net"
6
7
"strconv"
7
8
"strings"
8
9
)
9
10
10
11
var (
11
- itemWidth = 10
12
- itemHeight = 4
13
- itemPadding = 1
12
+ toolBarHeight = 2
13
+ itemWidth = 10
14
+ itemHeight = 4
15
+ itemPadding = 1
16
+ maxX = 0
17
+ maxY = 0
18
+ plotterX = 0
19
+ plotterY = 0
14
20
)
15
21
16
22
func initGui (config * Config ) * gocui.Gui {
@@ -19,12 +25,11 @@ func initGui(config *Config) *gocui.Gui {
19
25
LogPanic (err .Error ())
20
26
}
21
27
22
- maxX , maxY := gui .Size ()
23
- for i , item := range config .Items {
24
- if err := createItemView (item , i , maxX , maxY , gui ); err != nil {
25
- LogPanic (err .Error ())
26
- }
27
- }
28
+ maxX , maxY = gui .Size ()
29
+ gui .Mouse = true
30
+
31
+ createToolBarView (gui )
32
+ createMonitoringView (config .Items , gui )
28
33
29
34
if err := gui .SetKeybinding ("" , 'q' , gocui .ModNone , quit ); err != nil {
30
35
LogPanic (err .Error ())
@@ -36,7 +41,28 @@ func initGui(config *Config) *gocui.Gui {
36
41
return gui
37
42
}
38
43
39
- func createItemView (item * Item , col int , maxX int , maxY int , gui * gocui.Gui ) error {
44
+ func createToolBarView (gui * gocui.Gui ) {
45
+ view , err := gui .SetView ("toolbar" , plotterX , plotterY , maxX - 1 , plotterY + toolBarHeight )
46
+ if err != nil && err != gocui .ErrUnknownView {
47
+ LogPanic (err .Error ())
48
+ }
49
+
50
+ fmt .Fprintf (view , "%s v%s | IP: %s" , prog , version , getOutboundIP ())
51
+
52
+ plotterY += toolBarHeight + itemPadding
53
+ }
54
+
55
+ func createMonitoringView (items []* Item , gui * gocui.Gui ) {
56
+ for i , item := range items {
57
+ if err := createMonitoringItemView (item , i , maxX , maxY , gui ); err != nil {
58
+ LogPanic (err .Error ())
59
+ }
60
+ }
61
+
62
+ plotterY += itemHeight + itemPadding
63
+ }
64
+
65
+ func createMonitoringItemView (item * Item , col int , maxX int , maxY int , gui * gocui.Gui ) error {
40
66
itemsPerLine := maxX / (itemWidth + itemPadding )
41
67
row := 0
42
68
@@ -45,8 +71,8 @@ func createItemView(item *Item, col int, maxX int, maxY int, gui *gocui.Gui) err
45
71
col %= itemsPerLine
46
72
}
47
73
48
- x := col * (itemWidth + itemPadding )
49
- y := row * (itemHeight + itemPadding )
74
+ x := plotterX + col * (itemWidth + itemPadding )
75
+ y := plotterY + row * (itemHeight + itemPadding )
50
76
51
77
view , err := gui .SetView (item .Label , x , y , x + itemWidth , y + itemHeight )
52
78
if err != nil && err != gocui .ErrUnknownView {
@@ -59,6 +85,13 @@ func createItemView(item *Item, col int, maxX int, maxY int, gui *gocui.Gui) err
59
85
view .Overwrite = true
60
86
61
87
fmt .Fprint (view , "\n \n loading" )
88
+
89
+ // Bind mouse-click to item refresh.
90
+ gui .SetKeybinding (item .Label , gocui .MouseLeft , gocui .ModNone , func (gui * gocui.Gui , view * gocui.View ) error {
91
+ updateItem (item , gui )
92
+ return nil
93
+ })
94
+
62
95
return nil
63
96
}
64
97
@@ -118,3 +151,15 @@ func runMainLoop(gui *gocui.Gui) {
118
151
func quit (gui * gocui.Gui , v * gocui.View ) error {
119
152
return gocui .ErrQuit
120
153
}
154
+
155
+ func getOutboundIP () string {
156
+ conn , err := net .Dial ("udp" , "1.1.1.1:80" )
157
+ if err != nil {
158
+ return "?"
159
+ }
160
+ defer conn .Close ()
161
+
162
+ localAddr := conn .LocalAddr ().(* net.UDPAddr )
163
+
164
+ return localAddr .IP .String ()
165
+ }
0 commit comments