import base import std/[osproc, re, strutils] 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 let ping_re = re(r"time=[0-9.]+") const ping_cmd: string = "ping -4 -c 1 " & host proc getPing(): float = var ping: float = -1 let cmdOut = execCmdEx(ping_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): Info = let pingstr = split($ping,".") let niceping = pingstr[0] & "." & pingstr[1][0] var text = "🏓 " & niceping & " ms" var state = 0 if ping < 0: text = "❌ No Pong" state = 1 else: case ping: of 0..100: state = 0 of 101..400: state = 1 of 401..1000: state = 2 else: state = 9 var data = newInfo("Ping Clock") data.full_text = text # i3bar stuff data.color = default_fg data.border = default_bg 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 return data proc main() = let ping = get_ping() let data = getObject(ping) let output = outputData(data) if output == data.full_text: main() if isMainModule: main()