2022-02-12 14:20:21 +01:00
|
|
|
import i3bar_base
|
|
|
|
import std/os
|
|
|
|
import std/osproc
|
|
|
|
import strutils
|
|
|
|
|
2022-04-28 16:52:45 +02:00
|
|
|
const wlan_nic*: string ="wlan0"
|
2022-02-12 14:20:21 +01:00
|
|
|
|
|
|
|
# /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): i3barData =
|
2022-03-21 16:59:52 +01:00
|
|
|
var data = newi3barData()
|
2022-04-28 16:52:45 +02:00
|
|
|
data.title = "WiFi : "
|
2022-03-21 16:59:52 +01:00
|
|
|
data.full_text = conn
|
|
|
|
data.border = purple
|
2022-02-12 14:20:21 +01:00
|
|
|
return data
|
|
|
|
|
2022-04-28 16:52:45 +02:00
|
|
|
proc get_wifi_info*() =
|
2022-02-12 14:20:21 +01:00
|
|
|
var last_qual = ""
|
|
|
|
while true:
|
|
|
|
let (essid, quality) = get_wifi()
|
|
|
|
if quality != last_qual:
|
|
|
|
let data = getObject(quality & " " & essid)
|
2022-04-28 16:52:45 +02:00
|
|
|
let args = @["nmtui-connect"]
|
|
|
|
let output = outputJSON(data, args)
|
|
|
|
echo output
|
|
|
|
case output:
|
|
|
|
of "nmtui-connect":
|
|
|
|
discard execCmd("alacritty -e nmtui-connect")
|
2022-02-12 14:20:21 +01:00
|
|
|
last_qual = quality
|
2022-04-28 16:52:45 +02:00
|
|
|
if stoploop:
|
|
|
|
break
|
2022-02-12 14:20:21 +01:00
|
|
|
sleep(1000)
|
|
|
|
|
|
|
|
proc await_click_info() =
|
|
|
|
while true:
|
|
|
|
let input = parseInput()
|
|
|
|
if input.button == 1:
|
|
|
|
discard execCmd("alacritty -e nmtui-connect")
|
|
|
|
|
|
|
|
proc main() =
|
|
|
|
if dirExists("/sys/class/net/" & wlan_nic):
|
2022-04-28 16:52:45 +02:00
|
|
|
get_wifi_info()
|
2022-03-21 16:59:52 +01:00
|
|
|
else:
|
|
|
|
echo "No WLAN"
|
2022-02-12 14:20:21 +01:00
|
|
|
|
2022-04-28 16:52:45 +02:00
|
|
|
if isMainModule:
|
|
|
|
main()
|