From 2c23da611288c6415a363c07847c82481763908e Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 20 Nov 2023 23:33:59 +0000 Subject: Switch to compfy --- scripts/dmenu-iwd | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100755 scripts/dmenu-iwd (limited to 'scripts/dmenu-iwd') 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: &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" -- cgit v1.2.1