added colour output to pingclock

This commit is contained in:
Paul Wilde 2022-02-12 15:36:19 +00:00
parent 0821886de9
commit 8433ce8155

View file

@ -4,35 +4,52 @@ import std/re
import strutils import strutils
import i3bar_base import i3bar_base
const host: string = "8.8.8.8" const host: string = "web.wilde.cloud"
const cmd: string = "ping -c 1 " & host const cmd: string = "ping -c 1 " & host
const time_secs: int = 4 const time_secs: int = 4
let ping_re = re(r"time=[0-9.]+\sms") let ping_re = re(r"time=[0-9.]+")
proc get_pingms(): string = proc get_ping(): float =
let cmdOut = execCmdEx(cmd) let cmdOut = execCmdEx(cmd)
let lines = splitLines(cmdOut.output) let lines = splitLines(cmdOut.output)
let ping_line = lines[1] let ping_line = lines[1]
let bounds = findBounds(ping_line, ping_re) let bounds = findBounds(ping_line, ping_re)
if bounds.first > 0: if bounds.first > 0:
let ping = ping_line[bounds.first+5..bounds.last] let ping = ping_line[bounds.first+5..bounds.last]
return "🏓 " & ping return parseFloat(ping)
return "❌ No Pong" return -1
proc getObject(ping: float): i3barData =
var text = "🏓 " & $ping & " ms"
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
proc getObject(ping: string): i3barData =
let data = i3barData( let data = i3barData(
full_text: ping, full_text: text,
color: foreground, color: col,
border: lightblue, border: blue,
background: black
) )
return data return data
proc main() = proc main() =
var last_ping = "" var last_ping: float = 0
while true: while true:
let ping = get_pingms() let ping = get_ping()
if ping != last_ping: if ping != last_ping:
let data = getObject(ping) let data = getObject(ping)
outputJSON(data) outputJSON(data)