diff --git a/i3bar_base.nim b/i3bar_base.nim index 6080246..4715e47 100644 --- a/i3bar_base.nim +++ b/i3bar_base.nim @@ -62,4 +62,3 @@ proc getArguments*(): seq[string] = proc outputJSON*(data: i3barData) = echo $$data - diff --git a/i3bar_nic.nim b/i3bar_nic.nim new file mode 100644 index 0000000..dd71b11 --- /dev/null +++ b/i3bar_nic.nim @@ -0,0 +1,68 @@ +import i3bar_base +import std/os +import std/osproc +import std/threadpool +import strutils + +const nics: seq[string] = @["enp3s0","wlp2s0","enp0s20f0u3"] + +# /sys/class/net/*/operstate up or down if connected + +proc get_ip(nic: string): string = + let cmd = "ifconfig " & nic & " | grep inet | awk -F\" \" '{print $2}' | head -1 | awk '{print $1}'" + let ip = execCmdEx(cmd) + return strip(ip.output) + +proc get_online_state(nic: string): string = + let oper = readFile("/sys/class/net/" & nic & "/operstate") + let state = strip(oper) + return "[" & state & "]" + +proc get_net(nic: string): (string, string) = + let state = get_online_state(nic) + let ip = get_ip(nic) + if state == "[down]" or ip == "": + return ("disconnected", state) + return (ip, state) + +proc getObject(conn: string): i3barData = + let data = i3barData( + full_text: conn, + color: foreground, + border: purple, + background: black + ) + return data + +proc get_net_info(nic: string) = + var last_ip = "" + var last_state = "" + while true: + let (ip, state) = get_net(nic) + if ip != last_ip or state != last_state: + let data = getObject(state & " " & ip) + outputJSON(data) + last_ip = ip + last_state = state + sleep(1000) + +proc await_click_info() = + while true: + let input = parseInput() + if input.button == 1: + discard execCmd("alacritty -e nmtui-connect") + +proc get_nic(): string = + for nic in nics: + if dirExists("/sys/class/net/" & nic): + return nic + return "no-nic" + +proc main() = + let mynic = get_nic() + if dirExists("/sys/class/net/" & mynic): + spawn get_net_info(mynic) + spawn await_click_info() + sync() + +main() diff --git a/i3bar_pingclock.nim b/i3bar_pingclock.nim index c2057b4..9956ec7 100644 --- a/i3bar_pingclock.nim +++ b/i3bar_pingclock.nim @@ -5,7 +5,7 @@ import strutils import i3bar_base const host: string = "web.wilde.cloud" -const cmd: string = "ping -c 1 " & host +const cmd: string = "ping -4 -c 1 " & host const time_secs: int = 4 let ping_re = re(r"time=[0-9.]+") diff --git a/i3bar_testblock.nim b/i3bar_testblock.nim new file mode 100644 index 0000000..83f61cc --- /dev/null +++ b/i3bar_testblock.nim @@ -0,0 +1,45 @@ +import std/os +import strutils +import std/osproc +import std/math +import i3bar_base +import std/threadpool + +proc get_data() = + while true: + let data1 = i3barData( + full_text: "Hello", + color: foreground, + border: yellow, + background: black + ) + outputJSON(data1) + sleep(1000) + let data2 = i3barData( + full_text: "Bye", + color: foreground, + border: yellow, + background: black + ) + outputJSON(data2) + sleep(1000) + +proc await_click_info() = + while true: + let input = parseInput() + case input.button: + of 1,4: + get_data() + of 3,5: + get_data() + else: + let no = false + + clearInput(2) + +proc main() = + spawn get_data() + spawn await_click_info() + sync() + +main() diff --git a/i3bar_volume.nim b/i3bar_volume.nim index 267d01b..bd2706b 100644 --- a/i3bar_volume.nim +++ b/i3bar_volume.nim @@ -40,7 +40,8 @@ proc get_volume(run_once: bool = false) = let data = i3barData( full_text: text, color: foreground, - border: green + border: green, + background: black ) outputJSON(data) if run_once: