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.
This commit is contained in:
elenapan 2024-12-18 11:27:57 +02:00
parent e720d3ec01
commit 50cad1adc2

View file

@ -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")"