Skip to content

Commit 096d0e4

Browse files
committed
feat: add toolbar and support on-click item refresh
1 parent 8d7841c commit 096d0e4

File tree

3 files changed

+67
-20
lines changed

3 files changed

+67
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ _**DISCLAIMER:** This tool is still under wild development!_
2424
```bash
2525
usage: s3mon [-h|--help] [-v|--version] [-V|--verbose] [-c|--config "<value>"]
2626

27-
s3mon - simple service status monitor v1.0 by stuchl4n3k
27+
s3mon - simple service status monitor v1.1 by stuchl4n3k
2828

2929
Arguments:
3030

main.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
const (
1313
prog = "s3mon"
14-
version = "1.0"
14+
version = "1.1"
1515
author = "stuchl4n3k"
1616
description = "s3mon - simple service status monitor v" + version + " by " + author
1717
)
@@ -54,25 +54,27 @@ func main() {
5454
// GUI:
5555
gui := initGui(config)
5656
defer closeGui(gui)
57-
go monitorItems(config.Items, gui)
57+
go updateItems(config.Items, gui)
5858
runMainLoop(gui)
5959
}
6060

61-
func monitorItems(items []*Item, gui *gocui.Gui) {
61+
func updateItems(items []*Item, gui *gocui.Gui) {
6262
for {
6363
for _, item := range items {
64-
result := monitorItem(item)
65-
updateItemView(item, result, gui)
66-
64+
updateItem(item, gui)
6765
time.Sleep(1 * time.Second)
6866
}
6967
}
7068
}
7169

70+
func updateItem(item *Item, gui *gocui.Gui) {
71+
result := monitorItem(item)
72+
updateItemView(item, result, gui)
73+
}
74+
7275
func monitorItem(item *Item) string {
7376
output, err := runScript(item.Script, item.Label)
7477
if err != nil {
75-
// LogErr(err.Error())
7678
return ResultErr
7779
}
7880

ui.go

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ package main
33
import (
44
"fmt"
55
"github.com/jroimartin/gocui"
6+
"net"
67
"strconv"
78
"strings"
89
)
910

1011
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
1420
)
1521

1622
func initGui(config *Config) *gocui.Gui {
@@ -19,12 +25,11 @@ func initGui(config *Config) *gocui.Gui {
1925
LogPanic(err.Error())
2026
}
2127

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)
2833

2934
if err := gui.SetKeybinding("", 'q', gocui.ModNone, quit); err != nil {
3035
LogPanic(err.Error())
@@ -36,7 +41,28 @@ func initGui(config *Config) *gocui.Gui {
3641
return gui
3742
}
3843

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 {
4066
itemsPerLine := maxX / (itemWidth + itemPadding)
4167
row := 0
4268

@@ -45,8 +71,8 @@ func createItemView(item *Item, col int, maxX int, maxY int, gui *gocui.Gui) err
4571
col %= itemsPerLine
4672
}
4773

48-
x := col * (itemWidth + itemPadding)
49-
y := row * (itemHeight + itemPadding)
74+
x := plotterX + col*(itemWidth+itemPadding)
75+
y := plotterY + row*(itemHeight+itemPadding)
5076

5177
view, err := gui.SetView(item.Label, x, y, x+itemWidth, y+itemHeight)
5278
if err != nil && err != gocui.ErrUnknownView {
@@ -59,6 +85,13 @@ func createItemView(item *Item, col int, maxX int, maxY int, gui *gocui.Gui) err
5985
view.Overwrite = true
6086

6187
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+
6295
return nil
6396
}
6497

@@ -118,3 +151,15 @@ func runMainLoop(gui *gocui.Gui) {
118151
func quit(gui *gocui.Gui, v *gocui.View) error {
119152
return gocui.ErrQuit
120153
}
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

Comments
 (0)