-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (67 loc) · 1.61 KB
/
main.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
package main
import (
"fmt"
"log"
"runtime"
"strings"
"fyne.io/systray"
probing "github.com/prometheus-community/pro-bing"
"ipingtray/icons"
)
func main() {
systray.Run(onReady, onExit)
}
func onReady() {
systray.SetTitle("Initializing...")
systray.SetIcon(icons.White)
mLatencyLabel := systray.AddMenuItem("Latency", "Current Latency")
mLatencyLabel.Disable()
systray.AddSeparator()
mQuit := systray.AddMenuItem("Quit", "Quit the app")
// Update tray title
pinger.OnRecv = func(pkt *probing.Packet) {
latency := pkt.Rtt.Milliseconds()
icon := icons.Red
switch {
case latency < 50:
icon = icons.Green
case latency < 75:
icon = icons.Orange
}
updateTray(fmt.Sprintf("%d ms", latency), icon)
mLatencyLabel.SetTitle(fmt.Sprintf("%s: %d ms", pinger.IPAddr().String(), latency))
}
pinger.OnSendError = func(_ *probing.Packet, err error) {
updateTray("Network Unavailable", icons.Red)
if strings.Contains(err.Error(), "sendto") {
parts := strings.Split(err.Error(), ": ")
if len(parts) >= 2 {
mLatencyLabel.SetTitle(fmt.Sprintf("%s: %s", pinger.Addr(), parts[len(parts)-1]))
return
}
}
mLatencyLabel.SetTitle(fmt.Sprintf("%s: %s", pinger.Addr(), err.Error()))
}
// Running the ping
go func() {
if err := pinger.Run(); err != nil {
log.Printf("pinger has crashed: %v\n", err)
systray.Quit()
}
}()
// mQuit click
go func() {
<-mQuit.ClickedCh
systray.Quit()
}()
}
func updateTray(title string, icon []byte) {
systray.SetTitle(title)
systray.SetIcon(icon)
if runtime.GOOS == "windows" {
systray.SetTooltip(title)
}
}
func onExit() {
pinger.Stop()
}