2022-02-12 14:20:21 +01:00
|
|
|
import std/os
|
|
|
|
import std/osproc
|
|
|
|
import std/re
|
|
|
|
import strutils
|
|
|
|
import i3bar_base
|
|
|
|
|
|
|
|
const host: string = "8.8.8.8"
|
|
|
|
const cmd: string = "ping -c 1 " & host
|
|
|
|
const time_secs: int = 4
|
|
|
|
|
|
|
|
let ping_re = re(r"time=[0-9.]+\sms")
|
|
|
|
|
|
|
|
proc get_pingms(): string =
|
|
|
|
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 "🏓 " & ping
|
|
|
|
return "❌ No Pong"
|
|
|
|
|
|
|
|
proc getObject(ping: string): i3barData =
|
|
|
|
let data = i3barData(
|
|
|
|
full_text: ping,
|
|
|
|
color: foreground,
|
|
|
|
border: lightblue,
|
|
|
|
)
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
proc main() =
|
2022-02-12 16:19:43 +01:00
|
|
|
var last_ping = ""
|
2022-02-12 14:20:21 +01:00
|
|
|
while true:
|
|
|
|
let ping = get_pingms()
|
2022-02-12 16:19:43 +01:00
|
|
|
if ping != last_ping:
|
|
|
|
let data = getObject(ping)
|
|
|
|
outputJSON(data)
|
|
|
|
last_ping = ping
|
2022-02-12 14:20:21 +01:00
|
|
|
|
|
|
|
sleep(time_secs * 1000)
|
|
|
|
|
|
|
|
main()
|