81 lines
1.8 KiB
Nim
81 lines
1.8 KiB
Nim
import std/os
|
|
import std/osproc
|
|
import std/re
|
|
import strutils
|
|
import i3bar_base
|
|
import std/math
|
|
|
|
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 =
|
|
var ping: float = -1
|
|
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 png = ping_line[bounds.first+5..bounds.last]
|
|
ping = parseFloat(png)
|
|
return ping
|
|
|
|
proc getObject(ping: float): i3barData =
|
|
let pingstr = split($ping,".")
|
|
let niceping = pingstr[0] & "." & pingstr[1][0]
|
|
var text = "🏓 " & niceping & " 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(
|
|
title: "Ping Clock:",
|
|
full_text: text,
|
|
color: col,
|
|
border: blue,
|
|
background: black
|
|
)
|
|
return data
|
|
|
|
proc runDMenu(data: i3BarData) =
|
|
var cmd = "echo " & $data.full_text & " | dmenu "
|
|
#cmd = cmd & " -l 2 "
|
|
cmd = cmd & " -p \"Ping Clock : \""
|
|
cmd = cmd & " -nb \"" & $data.background & "\""
|
|
cmd = cmd & " -nf \"" & $data.color & "\""
|
|
cmd = cmd & " -sb \"" & $data.border & "\""
|
|
cmd = cmd & " -sf \"" & $data.color & "\""
|
|
discard execCmd(cmd)
|
|
discard execCmd("i3-msg mode \"default\"")
|
|
|
|
|
|
proc main() =
|
|
var last_ping: float = 0
|
|
while loop:
|
|
let ping = get_ping()
|
|
if ping != last_ping:
|
|
let data = getObject(ping)
|
|
if dmenu:
|
|
runDMenu(data)
|
|
else:
|
|
outputJSON(data)
|
|
last_ping = ping
|
|
loop = not stoploop
|
|
if loop:
|
|
sleep(time_secs * 1000)
|
|
|
|
|
|
main()
|