2022-02-12 14:20:21 +01:00
|
|
|
import std/os
|
|
|
|
import std/osproc
|
|
|
|
import std/re
|
|
|
|
import strutils
|
|
|
|
import i3bar_base
|
2022-03-07 10:37:31 +01:00
|
|
|
import std/math
|
2022-02-12 14:20:21 +01:00
|
|
|
|
2022-02-12 16:36:19 +01:00
|
|
|
const host: string = "web.wilde.cloud"
|
2022-02-26 22:44:00 +01:00
|
|
|
const cmd: string = "ping -4 -c 1 " & host
|
2022-02-12 14:20:21 +01:00
|
|
|
const time_secs: int = 4
|
|
|
|
|
2022-02-12 16:36:19 +01:00
|
|
|
let ping_re = re(r"time=[0-9.]+")
|
2022-02-12 14:20:21 +01:00
|
|
|
|
2022-02-12 16:36:19 +01:00
|
|
|
proc get_ping(): float =
|
2022-03-07 10:37:31 +01:00
|
|
|
var ping: float = -1
|
2022-02-12 14:20:21 +01:00
|
|
|
let cmdOut = execCmdEx(cmd)
|
|
|
|
let lines = splitLines(cmdOut.output)
|
|
|
|
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
|
|
|
|
|
|
|
proc getObject(ping: float): i3barData =
|
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-02-12 16:36:19 +01:00
|
|
|
var col = foreground
|
|
|
|
if ping < 0:
|
|
|
|
text = "❌ No Pong"
|
|
|
|
col = yellow
|
|
|
|
else:
|
|
|
|
case ping:
|
|
|
|
of 0..100:
|
|
|
|
col = foreground
|
|
|
|
of 101..400:
|
|
|
|
col = yellow
|
|
|
|
of 401..1000:
|
|
|
|
col = alert
|
|
|
|
else:
|
|
|
|
col = red
|
2022-02-12 14:20:21 +01:00
|
|
|
|
|
|
|
let data = i3barData(
|
2022-02-12 16:36:19 +01:00
|
|
|
full_text: text,
|
|
|
|
color: col,
|
|
|
|
border: blue,
|
|
|
|
background: black
|
2022-02-12 14:20:21 +01:00
|
|
|
)
|
|
|
|
return data
|
|
|
|
|
2022-04-28 13:34:09 +02:00
|
|
|
proc runDMenu(data: i3BarData) =
|
|
|
|
var cmd = "echo Ping Clock : " & $data.full_text & " | dmenu "
|
|
|
|
cmd = cmd & " -nb \"" & $data.background & "\""
|
|
|
|
cmd = cmd & " -nf \"" & $data.color & "\""
|
|
|
|
cmd = cmd & " -sb \"" & $data.background & "\""
|
|
|
|
cmd = cmd & " -sf \"" & $data.color & "\""
|
|
|
|
discard execCmd(cmd)
|
|
|
|
discard execCmd("i3-msg mode \"default\"")
|
|
|
|
|
2022-02-12 14:20:21 +01:00
|
|
|
|
|
|
|
proc main() =
|
2022-04-28 13:34:09 +02:00
|
|
|
let args = getArguments()
|
|
|
|
var loop = true
|
|
|
|
var stoploop = false
|
|
|
|
var dmenu = false
|
|
|
|
for arg in args:
|
|
|
|
if arg == "noloop":
|
|
|
|
stoploop = true
|
|
|
|
if arg == "dmenu":
|
|
|
|
stoploop = true
|
|
|
|
dmenu = true
|
|
|
|
|
2022-02-12 16:36:19 +01:00
|
|
|
var last_ping: float = 0
|
2022-04-28 13:34:09 +02:00
|
|
|
while loop:
|
2022-02-12 16:36:19 +01:00
|
|
|
let ping = get_ping()
|
2022-02-12 16:19:43 +01:00
|
|
|
if ping != last_ping:
|
|
|
|
let data = getObject(ping)
|
|
|
|
outputJSON(data)
|
|
|
|
last_ping = ping
|
2022-04-28 13:34:09 +02:00
|
|
|
if dmenu:
|
|
|
|
runDMenu(data)
|
|
|
|
|
|
|
|
loop = not stoploop
|
|
|
|
if loop:
|
|
|
|
sleep(time_secs * 1000)
|
2022-02-12 14:20:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
main()
|