wmtools/wlan.nim

65 lines
1.5 KiB
Nim
Raw Normal View History

2022-04-28 21:58:40 +02:00
import base
import std/[os,osproc,strutils,sequtils]
2022-02-12 14:20:21 +01:00
const default_bg = purple
const default_fg = white
const wlan_nics: seq[string] = @["wlan0"]
const get_ssid_cmd = "iwgetid -r"
const mng_cmd = "alacritty -e nmtui-connect"
2022-02-12 14:20:21 +01:00
proc getSsid(): string =
let ssid = execCmdEx(get_ssid_cmd)
return strip(ssid.output)
2022-02-12 14:20:21 +01:00
proc getSignalQuality(): string =
2022-02-12 14:20:21 +01:00
let wl = readFile("/proc/net/wireless")
let ln = splitLines(wl)[2]
let links = split(ln," ")
var qual = strip(links[1])
qual = replace(qual,".","")
return "[" & qual & "]"
proc getWifi(nic: string): (string, string) =
let ssid = getSsid()
if ssid == "":
return ("disconnected", "disconnected")
let quality = getSignalQuality()
return (ssid, quality)
proc getObject(): Info =
var data = newInfo("WiFi")
data.border = purple
data.selected_bg = default_bg
data.selected_fg = default_fg
2022-02-12 14:20:21 +01:00
return data
proc getWifiInfo*(nics: seq[string]) =
var lst: seq[string] = @[]
for nic in nics:
let (essid, quality) = getWifi(nic)
lst.add(nic & ":" & quality & " " & essid)
let data = getObject()
let args = concat(lst,@["---", "manage","exit"])
let output = outputData(data, args)
case output:
of "manage":
discard execCmd(mng_cmd)
of "exit":
return
of "---":
return
2022-02-12 14:20:21 +01:00
proc main() =
var my_nics: seq[string] = @[]
for nic in wlan_nics:
if dirExists("/sys/class/net/" & nic):
my_nics.add(nic)
if len(my_nics) > 0:
getWifiInfo(my_nics)
else:
switchTwmMode()
echo "No WLAN"
2022-02-12 14:20:21 +01:00
if isMainModule:
main()