scripts/tofi/tofi-wifi-menu.sh

74 lines
2.7 KiB
Bash
Raw Permalink Normal View History

#!/usr/bin/env bash
2022-09-17 21:03:01 +00:00
# Modified from: https://github.com/fourstepper/wofi-wifi-menu
# Starts a scan of available broadcasting SSIDs
# nmcli dev wifi rescan
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2024-08-23 03:40:56 +00:00
FIELDS=SECURITY,SSID
# Gives a list of known connections so we can parse it later
KNOWNCON=$(nmcli connection show)
# Really janky way of telling if there is currently a connection
CONSTATE=$(nmcli -fields WIFI g)
CURRSSID=$(LANGUAGE=C nmcli -t -f active,ssid dev wifi | awk -F: '$1 ~ /^yes/ {print $2}')
2024-08-23 03:40:56 +00:00
LIST=$(nmcli --fields "$FIELDS" device wifi list | sort | uniq | sed -r '/^SECURITY *SSID/d' | sed '/ --/d' | sed "/$CURRSSID/d" | sed -r 's/((WPA[123]|WEP) )+ */ /g')
if [[ "$CONSTATE" =~ "enabled" ]]; then
2023-01-06 19:41:31 +00:00
TOGGLE="toggle off"
elif [[ "$CONSTATE" =~ "disabled" ]]; then
2023-01-06 19:41:31 +00:00
TOGGLE="toggle on"
fi
2024-08-23 03:40:56 +00:00
HEIGHT=$(swaymsg -t get_outputs | tr '\n' ' ' | sed -e 's/ */ /g' | sed -e 's/\(.*"focused": [a-z]*\),/\1\n/' | less | grep '"focused": true' | sed -e 's/.*"rect": {[^}]*"height": \([0-9]*\).*/\1/')
CURR=''
if [ ! -z "$CURRSSID" ]; then
CURR="$CURRSSID\n"
fi
CHENTRY=$(echo -e "${TOGGLE}\nmanual\n${CURR}${LIST}" | uniq -u | ${HOME}/.local/bin/tofi --prompt-text "Wi-Fi: " --height $HEIGHT --width 340 -c $HOME/.dotfiles/tofi/sidebar.toml)
if [[ $CHENTRY == "" ]]; then
2023-01-07 13:16:47 +00:00
exit
fi
2024-08-23 03:40:56 +00:00
CHSSID=$(echo "$CHENTRY" | sed -e 's/\([]\) //g' | sed -e 's/ *$//g')
# If the user inputs "manual" as their SSID in the start window, it will bring them to this screen
if [ "$CHENTRY" = "manual" ] ; then
2023-01-06 19:41:31 +00:00
# Manual entry of the SSID and password (if appplicable)
2024-08-23 03:40:56 +00:00
MSSID=$(echo "enter the SSID of the network (SSID,password)" | ${HOME}/.local/bin/tofi --prompt-text "Password" --height $HEIGHT --width 340 -c $HOME/.dotfiles/tofi/sidebar.toml)
2023-01-06 19:41:31 +00:00
# Separating the password from the entered string
MPASS=$(echo "$MSSID" | awk -F "," '{print $2}')
2023-01-06 19:41:31 +00:00
# If the user entered a manual password, then use the password nmcli command
if [ "$MPASS" = "" ]; then
nmcli dev wifi con "$MSSID"
else
nmcli dev wifi con "$MSSID" password "$MPASS"
fi
elif [ "$CHENTRY" = "toggle on" ]; then
2023-01-06 19:41:31 +00:00
nmcli radio wifi on
elif [ "$CHENTRY" = "toggle off" ]; then
2023-01-06 19:41:31 +00:00
nmcli radio wifi off
else
2024-08-23 03:40:56 +00:00
if grep -q "$CHSSID" <<< `echo $KNOWNCON`; then
2023-01-06 19:41:31 +00:00
nmcli con up "$CHSSID"
else
2024-08-23 03:40:56 +00:00
if [[ "$CHENTRY" =~ "" ]]; then
WIFIPASS=$(echo "" | ${HOME}/.local/bin/tofi --prompt-text "Password" --height $HEIGHT --width 340 -c $HOME/.dotfiles/tofi/sidebar.toml)
if [[ $WIFIPASS == "if connection is stored, hit enter" ]]; then
unset WIFIPASS
fi
2023-01-06 19:41:31 +00:00
fi
nmcli dev wifi con "$CHSSID" password "$WIFIPASS"
fi
fi