wmtools/i3bar_pingclock.nim

61 lines
1.2 KiB
Nim

import std/os
import std/osproc
import std/re
import strutils
import i3bar_base
const host: string = "web.wilde.cloud"
const cmd: string = "ping -4 -c 1 " & host
const time_secs: int = 4
let ping_re = re(r"time=[0-9.]+")
proc get_ping(): float =
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:
let ping = ping_line[bounds.first+5..bounds.last]
return parseFloat(ping)
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
let data = i3barData(
full_text: text,
color: col,
border: blue,
background: black
)
return data
proc main() =
var last_ping: float = 0
while true:
let ping = get_ping()
if ping != last_ping:
let data = getObject(ping)
outputJSON(data)
last_ping = ping
sleep(time_secs * 1000)
main()