added network
This commit is contained in:
parent
ae35c221fe
commit
f17a160450
4 changed files with 70 additions and 2 deletions
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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)
|
||||
|
|
62
src/util/netwurrk.nim
Normal file
62
src/util/netwurrk.nim
Normal file
|
@ -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())
|
||||
|
Loading…
Reference in a new issue