From 50cad1adc2bf2d84f1abc7523daa28950525a2bb Mon Sep 17 00:00:00 2001 From: elenapan Date: Wed, 18 Dec 2024 11:27:57 +0200 Subject: [PATCH] networks: Get saved networks using `iwctl` Using `/var/lib/iwd` to get saved networks does not work on all setups. For example, if using NetworkManager with iwd as its backend, the `/var/lib/iwd` directory has permissions that do not allow reading the files inside. --- config/eww/scripts/networks.sh | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/config/eww/scripts/networks.sh b/config/eww/scripts/networks.sh index f459137..58a51fb 100755 --- a/config/eww/scripts/networks.sh +++ b/config/eww/scripts/networks.sh @@ -2,14 +2,8 @@ # Network widget controls # Intended for IWD not NetworkManager. interface=wlan0 -networks_dir=/var/lib/iwd/ rofi="$HOME/.config/eww/scripts/rofi-with-mode-restore networks" -is_saved_network() { - local key="$1" - [[ -v saved_networks_assoc["$key"] ]] -} - update_networks() { iwctl station $interface scan iwctl_out="$(iwctl station $interface get-networks)" @@ -40,10 +34,11 @@ update_networks() { # Find saved networks # We can connect to those without entering a password - saved_networks="$(find "$networks_dir" -maxdepth 1 -name "*.psk" | sed 's@/var/lib/iwd/@@; s@\.psk$@@')" + saved_networks="$(iwctl known-networks list | sed -e '1,4d;s/\x1b\[[0-9;]*m//g')" declare -A saved_networks_assoc - while IFS= read -r network; do - saved_networks_assoc["$network"]=true + while read -r line; do + name="$(awk '{for (i=1; i<=NF-5; i++) printf $i " "}' <<< "$line" | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//')" + saved_networks_assoc["$name"]=true done <<< "$saved_networks" json='[' @@ -58,7 +53,11 @@ update_networks() { name="$(awk '{for (i=1; i<=NF-2; i++) printf $i " "}' <<< "$line" | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//')" fi - saved="$(is_saved_network "$name" && echo "true" || echo "false")" + if [[ -v saved_networks_assoc["$name"] ]]; then + saved="true" + else + saved="false" + fi signal_strength="$(awk '{ last_col = $NF; star_count = gsub("\\*", "", last_col); print star_count }' <<< "$line")"