wmtools/wlan.nim

62 lines
1.4 KiB
Nim

import base
import std/os
import std/osproc
import strutils
const wlan_nic*: string ="wlan0"
# /sys/class/net/wlp2s0/operstate up or down if connected
proc get_essid(): string =
let essid = execCmdEx("iwgetid -r")
return strip(essid.output)
proc get_signal_quality(): string =
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 get_wifi(): (string, string) =
let essid = get_essid()
if essid == "":
return ("disconnected", "")
let quality = get_signal_quality()
return (essid, quality)
proc getObject(conn: string): Info =
var data = newInfo()
data.title = "WiFi : "
data.full_text = conn
data.border = purple
data.selected_background = purple
data.selected_color = black
return data
proc get_wifi_info*() =
var last_qual = ""
while true:
let (essid, quality) = get_wifi()
if quality != last_qual:
let data = getObject(quality & " " & essid)
let args = @["nmtui-connect"]
let output = outputJSON(data, args)
echo output
case output:
of "nmtui-connect":
discard execCmd("alacritty -e nmtui-connect")
last_qual = quality
if stoploop:
break
sleep(1000)
proc main() =
if dirExists("/sys/class/net/" & wlan_nic):
get_wifi_info()
else:
echo "No WLAN"
if isMainModule:
main()