wmtools/pingclock.nim

86 lines
1.9 KiB
Nim
Raw Normal View History

2022-04-28 21:58:40 +02:00
import base
import std/[osproc, re, strutils]
2022-02-12 14:20:21 +01:00
const host: string = "9.9.9.9"
const default_bg = blue
const default_fg = white
const medium_bg = alert
const medium_fg = black
const alert_bg = alert
const alert_fg = black
const warning_bg = red
const warning_fg = white
2022-02-12 14:20:21 +01:00
2022-02-12 16:36:19 +01:00
let ping_re = re(r"time=[0-9.]+")
const ping_cmd: string = "ping -4 -c 1 " & host
2022-02-12 14:20:21 +01:00
proc getPing(): float =
2022-03-07 10:37:31 +01:00
var ping: float = -1
let cmdOut = execCmdEx(ping_cmd)
let lines = splitLines(cmdOut.output)
2022-02-12 14:20:21 +01:00
let ping_line = lines[1]
let bounds = findBounds(ping_line, ping_re)
if bounds.first > 0:
2022-03-07 10:37:31 +01:00
let png = ping_line[bounds.first+5..bounds.last]
ping = parseFloat(png)
return ping
2022-02-12 16:36:19 +01:00
2022-04-28 21:58:40 +02:00
proc getObject(ping: float): Info =
let pingstr = split($ping,".")
let niceping = pingstr[0] & "." & pingstr[1][0]
var text = "🏓 " & niceping & " ms"
var state = 0
2022-02-12 16:36:19 +01:00
if ping < 0:
text = "❌ No Pong"
state = 1
2022-02-12 16:36:19 +01:00
else:
case ping:
of 0..100:
state = 0
2022-02-12 16:36:19 +01:00
of 101..400:
state = 1
2022-02-12 16:36:19 +01:00
of 401..1000:
state = 2
2022-02-12 16:36:19 +01:00
else:
state = 9
2022-02-12 14:20:21 +01:00
var data = newInfo("Ping Clock")
2022-04-28 21:58:40 +02:00
data.full_text = text
# i3bar stuff
data.color = default_fg
data.border = default_bg
2022-04-28 21:58:40 +02:00
data.background = black
data.selected_bg = default_bg
data.selected_fg = default_fg
case state:
of 1:
data.selected_bg = medium_bg
data.selected_fg = medium_fg
# i3bar stuff
data.color = medium_bg
of 2:
data.selected_bg = alert_bg
data.selected_fg = alert_fg
# i3bar stuff
data.color = alert_bg
of 9:
data.selected_bg = warning_bg
data.selected_fg = warning_fg
# i3bar stuff
data.color = warning_bg
else:
#default options already set
let ok = true
2022-02-12 14:20:21 +01:00
return data
proc main() =
let ping = get_ping()
let data = getObject(ping)
let output = outputData(data)
if output == data.full_text:
main()
2022-02-12 14:20:21 +01:00
if isMainModule:
main()