60 lines
1.3 KiB
Nim
Executable file
60 lines
1.3 KiB
Nim
Executable file
import i3bar_base
|
|
import std/os
|
|
import std/osproc
|
|
import std/threadpool
|
|
import strutils
|
|
|
|
const wlan_nic: string ="wlp2s0"
|
|
|
|
# /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 =
|
|
let data = i3barData(
|
|
full_text: conn,
|
|
color: foreground,
|
|
border: purple,
|
|
)
|
|
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)
|
|
outputJSON(data)
|
|
last_qual = quality
|
|
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):
|
|
spawn get_wifi_info()
|
|
spawn await_click_info()
|
|
sync()
|
|
|
|
main()
|