43 lines
888 B
Nim
43 lines
888 B
Nim
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() =
|
|
var last_ping = ""
|
|
while true:
|
|
let ping = get_pingms()
|
|
if ping != last_ping:
|
|
let data = getObject(ping)
|
|
outputJSON(data)
|
|
last_ping = ping
|
|
|
|
sleep(time_secs * 1000)
|
|
|
|
main()
|