summaryrefslogtreecommitdiff
path: root/scripts/dmenu-iwd
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2023-11-20 23:33:59 +0000
committerdavidovski <david@davidovski.xyz>2023-11-20 23:33:59 +0000
commit2c23da611288c6415a363c07847c82481763908e (patch)
tree29cb278b64b9cea522a5db47ec15303a26f0dc45 /scripts/dmenu-iwd
parent4c2194a7c5758637259d109df248c24a2fac62e6 (diff)
Switch to compfy
Diffstat (limited to 'scripts/dmenu-iwd')
-rwxr-xr-xscripts/dmenu-iwd96
1 files changed, 96 insertions, 0 deletions
diff --git a/scripts/dmenu-iwd b/scripts/dmenu-iwd
new file mode 100755
index 0000000..a3cc411
--- /dev/null
+++ b/scripts/dmenu-iwd
@@ -0,0 +1,96 @@
+#!/bin/bash
+
+STATION="wlan0"
+FONT="Monospace"
+NORMAL_BACKGROUND="#000000"
+NORMAL_FOREGROUND="#8f8f8f"
+SELECTED_BACKGROUND="#000000"
+SELECTED_FOREGROUND="#00ff00"
+FLAGS=(
+ "-i"
+ "-nb" "$NORMAL_BACKGROUND"
+ "-nf" "$NORMAL_FOREGROUND"
+ "-sb" "$SELECTED_BACKGROUND"
+ "-sf" "$SELECTED_FOREGROUND"
+ "-fn" "$FONT"
+)
+
+remove_colors() {
+ sed -E 's/\x1B\[[0-9;]*[JKmsu]//g'
+}
+
+connect() {
+ SSID="$1"
+ notify-send --urgency=low "Attempting to connect to network: $SSID"
+ if iwctl station "$STATION" connect "$SSID" --dont-ask; then
+ notify-send "Connected to network: $SSID"
+ exit 0
+ fi
+ if ERROR=$(
+ iwctl station "$STATION" connect "$SSID" \
+ --passphrase "$(dmenu "${FLAGS[@]}" -p Password: </dev/null)" \
+ --dont-ask 2>&1
+ ); then
+ notify-send "Connected to network: $SSID"
+ else
+ notify-send --urgency=critical \
+ "Failed to connect to network: $SSID" "$(remove_colors <<<$ERROR)"
+ fi
+}
+
+networks() {
+ iwctl station "$STATION" get-networks | awk '
+BEGIN {
+ i = -1
+}
+{
+ i++
+ if (i < 4) next # skip header
+
+ network = ""
+ strength = 0
+ for (ix = 1; ix <= NF; ix++) {
+ if (ix == NF - 1) continue # skip protocol
+
+ # calculate strength by reading *s until
+ # a color code is encountered
+ char = sprintf("%c", $ix)
+ if (char == "*") {
+ split($ix, chars, "")
+ for (c = 1; c <= length($ix); c++) {
+ char = sprintf("%c", chars[c])
+ if (char == "\033") break
+ strength++
+ }
+ continue
+ }
+
+ if (char == "\033") continue # color codes
+ if ($ix == "") continue # whitespace
+ if ($ix == ">") continue # current network
+
+ # ssids may contain spaces
+ if (network == "") {
+ network = $ix
+ continue
+ }
+ network = network " " $ix
+ }
+ if (network == "") next
+ networks[network] = strength
+}
+END {
+ for (s = 4; s >= 0; s--) {
+ for (network in networks) {
+ if (networks[network] == s) {
+ print network
+ delete networks[network]
+ }
+ }
+ }
+}
+'
+}
+
+SELECTION=$(networks | dmenu "${FLAGS[@]}") || exit 1
+connect "$SELECTION"