From f17a16045061222065b4d7f0c6e873561fcff8b2 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Thu, 23 Nov 2023 09:27:54 +0000 Subject: [PATCH] added network --- src/dispatcher.nim | 3 +++ src/model/config.nim | 3 ++- src/parser.nim | 4 ++- src/util/netwurrk.nim | 62 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/util/netwurrk.nim diff --git a/src/dispatcher.nim b/src/dispatcher.nim index 7cb73d8..340348f 100644 --- a/src/dispatcher.nim +++ b/src/dispatcher.nim @@ -4,6 +4,7 @@ import util/furrytime import util/pingclock import util/batturry import util/volurrme +import util/netwurrk proc dispatch*(cfg: Config) = case cfg.run @@ -15,5 +16,7 @@ proc dispatch*(cfg: Config) = batturry.go() of Volurrme: volurrme.go() + of Netwurrk: + netwurrk.go() else: echo "No valid run command given" diff --git a/src/model/config.nim b/src/model/config.nim index 136de32..61f1a04 100644 --- a/src/model/config.nim +++ b/src/model/config.nim @@ -14,7 +14,8 @@ type FurryTime, PingClock, Batturry, - Volurrme + Volurrme, + Netwurrk let config_dir* = getHomeDir() & ".config/wm_tools/" let config_file* = config_dir & "config.toml" diff --git a/src/parser.nim b/src/parser.nim index 7a92ace..216d491 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -9,7 +9,7 @@ proc parseArgs*() = var p = newParser: help("WMTools : a set of tools to output option to your program of choice i.e. Rofi") arg("input",help="the tool to run, i.e. furrytime, pingclock, volurrme, etc.") - arg("adjust", help="(up, down, mute) allows to send extra commands. i.e. when run with volurrme, allows to adjust the volume") + arg("adjust", help="(up, down, mute) allows to send extra commands. i.e. when run with volurrme, allows to adjust the volume",default=some("None")) try: var opts = p.parse(params) case opts.input @@ -21,6 +21,8 @@ proc parseArgs*() = myConfig.run = Batturry of "volurrme", "volume", "vol": myConfig.run = Volurrme + of "netwurrk", "network", "net": + myConfig.run = Netwurrk else: echo p.help quit(1) diff --git a/src/util/netwurrk.nim b/src/util/netwurrk.nim new file mode 100644 index 0000000..0fcf1aa --- /dev/null +++ b/src/util/netwurrk.nim @@ -0,0 +1,62 @@ +import os +import osproc +import strutils +import sequtils + +import ../common +import ../output + +const mng_cmd = "alacritty -e nmtui-connect" + +proc getIP(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 getOnlineState(nic: string): string = + try: + let oper = readFile("/sys/class/net/" & nic & "/operstate") + let state = strip(oper) + return "[" & state & "]" + except: + echo "Error getting operstate for " & nic & " : ", getCurrentExceptionMsg() + return "" + +proc getConnState(nic: string): (string, string) = + let state = getOnlineState(nic) + let ip = getIP(nic) + if state == "[down]" or ip == "": + return ("disconnected", state) + return (ip, state) + +proc getObject(): Info = + var data = newInfo("Netwurrk") + return data + +proc getNetInfo*(my_nics: seq[string]) = + var my_nic_states: seq[string] = @[] + for nic in my_nics: + let (ip, state) = getConnState(nic) + my_nic_states.add(nic & ":" & state & " " & ip) + let data = getObject() + let args = concat(my_nic_states,@["manage"]) + let option = outputData(data, args) + if option in my_nic_states: + discard execCmd(mng_cmd) + case option: + of "manage": + discard execCmd(mng_cmd) + +proc getNics*(): seq[string] = + var my_nics: seq[string] = @[] + for nic in os.walkDir("/sys/class/net/"): + let n = replace(nic.path, "/sys/class/net/", "") + my_nics.add(n) + + if len(my_nics) > 0: + return my_nics + return @["no-nic"] + +proc go*() = + getNetInfo(getNics()) +