2022-04-28 21:58:40 +02:00
|
|
|
import base
|
2022-05-04 17:17:02 +02:00
|
|
|
import std/[osproc, re, strutils]
|
2022-02-12 14:20:21 +01:00
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
const host: string = "9.9.9.9"
|
|
|
|
const default_bg = blue
|
|
|
|
const default_fg = white
|
|
|
|
const medium_bg = alert
|
|
|
|
const medium_fg = black
|
|
|
|
const alert_bg = alert
|
|
|
|
const alert_fg = black
|
|
|
|
const warning_bg = red
|
|
|
|
const warning_fg = white
|
2022-02-12 14:20:21 +01:00
|
|
|
|
2022-02-12 16:36:19 +01:00
|
|
|
let ping_re = re(r"time=[0-9.]+")
|
2022-05-04 17:17:02 +02:00
|
|
|
const ping_cmd: string = "ping -4 -c 1 " & host
|
2022-02-12 14:20:21 +01:00
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc getPing(): float =
|
2022-03-07 10:37:31 +01:00
|
|
|
var ping: float = -1
|
2022-05-04 17:17:02 +02:00
|
|
|
let cmdOut = execCmdEx(ping_cmd)
|
|
|
|
let lines = splitLines(cmdOut.output)
|
2022-02-12 14:20:21 +01:00
|
|
|
let ping_line = lines[1]
|
|
|
|
let bounds = findBounds(ping_line, ping_re)
|
|
|
|
if bounds.first > 0:
|
2022-03-07 10:37:31 +01:00
|
|
|
let png = ping_line[bounds.first+5..bounds.last]
|
|
|
|
ping = parseFloat(png)
|
2022-03-21 16:59:52 +01:00
|
|
|
return ping
|
2022-02-12 16:36:19 +01:00
|
|
|
|
2022-04-28 21:58:40 +02:00
|
|
|
proc getObject(ping: float): Info =
|
2022-03-21 17:12:51 +01:00
|
|
|
let pingstr = split($ping,".")
|
|
|
|
let niceping = pingstr[0] & "." & pingstr[1][0]
|
2022-03-21 16:59:52 +01:00
|
|
|
var text = "🏓 " & niceping & " ms"
|
2022-05-04 17:17:02 +02:00
|
|
|
var state = 0
|
2022-02-12 16:36:19 +01:00
|
|
|
if ping < 0:
|
|
|
|
text = "❌ No Pong"
|
2022-05-04 17:17:02 +02:00
|
|
|
state = 1
|
2022-02-12 16:36:19 +01:00
|
|
|
else:
|
|
|
|
case ping:
|
|
|
|
of 0..100:
|
2022-05-04 17:17:02 +02:00
|
|
|
state = 0
|
2022-02-12 16:36:19 +01:00
|
|
|
of 101..400:
|
2022-05-04 17:17:02 +02:00
|
|
|
state = 1
|
2022-02-12 16:36:19 +01:00
|
|
|
of 401..1000:
|
2022-05-04 17:17:02 +02:00
|
|
|
state = 2
|
2022-02-12 16:36:19 +01:00
|
|
|
else:
|
2022-05-04 17:17:02 +02:00
|
|
|
state = 9
|
2022-02-12 14:20:21 +01:00
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
var data = newInfo("Ping Clock")
|
2022-04-28 21:58:40 +02:00
|
|
|
data.full_text = text
|
2022-05-04 17:17:02 +02:00
|
|
|
# i3bar stuff
|
|
|
|
data.color = default_fg
|
|
|
|
data.border = default_bg
|
2022-04-28 21:58:40 +02:00
|
|
|
data.background = black
|
2022-05-04 17:17:02 +02:00
|
|
|
data.selected_bg = default_bg
|
|
|
|
data.selected_fg = default_fg
|
|
|
|
case state:
|
|
|
|
of 1:
|
|
|
|
data.selected_bg = medium_bg
|
|
|
|
data.selected_fg = medium_fg
|
|
|
|
# i3bar stuff
|
|
|
|
data.color = medium_bg
|
|
|
|
of 2:
|
|
|
|
data.selected_bg = alert_bg
|
|
|
|
data.selected_fg = alert_fg
|
|
|
|
# i3bar stuff
|
|
|
|
data.color = alert_bg
|
|
|
|
of 9:
|
|
|
|
data.selected_bg = warning_bg
|
|
|
|
data.selected_fg = warning_fg
|
|
|
|
# i3bar stuff
|
|
|
|
data.color = warning_bg
|
|
|
|
else:
|
|
|
|
#default options already set
|
|
|
|
let ok = true
|
2022-02-12 14:20:21 +01:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
proc main() =
|
2022-05-04 17:17:02 +02:00
|
|
|
let ping = get_ping()
|
|
|
|
let data = getObject(ping)
|
|
|
|
let output = outputData(data)
|
|
|
|
if output == data.full_text:
|
|
|
|
main()
|
2022-02-12 14:20:21 +01:00
|
|
|
|
2022-04-28 16:52:45 +02:00
|
|
|
if isMainModule:
|
|
|
|
main()
|