Improved Wifi menu

This commit is contained in:
John Mertz 2024-08-22 21:40:56 -06:00
parent 20757931d0
commit 1ec8462e91
1 changed files with 19 additions and 32 deletions

View File

@ -7,17 +7,8 @@
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
FIELDS=SSID,SECURITY FIELDS=SECURITY,SSID
if [ -r "$DIR/config" ]; then
source "$DIR/config"
elif [ -r "$HOME/.dotfiles/wofi/wifi" ]; then
source "$HOME/.dotfiles/wofi/wifi"
else
echo "WARNING: config file not found! Using default values."
fi
LIST=$(nmcli --fields "$FIELDS" device wifi list | sed '/^--/d')
# Gives a list of known connections so we can parse it later # Gives a list of known connections so we can parse it later
KNOWNCON=$(nmcli connection show) KNOWNCON=$(nmcli connection show)
# Really janky way of telling if there is currently a connection # Really janky way of telling if there is currently a connection
@ -25,9 +16,7 @@ CONSTATE=$(nmcli -fields WIFI g)
CURRSSID=$(LANGUAGE=C nmcli -t -f active,ssid dev wifi | awk -F: '$1 ~ /^yes/ {print $2}') CURRSSID=$(LANGUAGE=C nmcli -t -f active,ssid dev wifi | awk -F: '$1 ~ /^yes/ {print $2}')
if [[ ! -z $CURRSSID ]]; then 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')
HIGHLINE=$(echo "$(echo "$LIST" | awk -F "[ ]{2,}" '{print $1}' | grep -Fxn -m 1 "$CURRSSID" | awk -F ":" '{print $1}') + 1" | bc )
fi
if [[ "$CONSTATE" =~ "enabled" ]]; then if [[ "$CONSTATE" =~ "enabled" ]]; then
TOGGLE="toggle off" TOGGLE="toggle off"
@ -35,24 +24,27 @@ elif [[ "$CONSTATE" =~ "disabled" ]]; then
TOGGLE="toggle on" TOGGLE="toggle on"
fi fi
CHENTRY=$(echo -e "$TOGGLE\nmanual\n$LIST" | uniq -u | wofi -s $HOME/.dotfiles/wofi/style.css -i -d --prompt "Wi-Fi SSID: " -c $HOME/.dotfiles/wofi/sidebar) 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 if [[ $CHENTRY == "" ]]; then
exit exit
fi fi
#echo "$CHENTRY"
CHSSID=$(echo "$CHENTRY" | sed 's/\s\{2,\}/\|/g' | awk -F "|" '{print $1}') CHSSID=$(echo "$CHENTRY" | sed -e 's/\([]\) //g' | sed -e 's/ *$//g')
#echo "$CHSSID"
# If the user inputs "manual" as their SSID in the start window, it will bring them to this screen # If the user inputs "manual" as their SSID in the start window, it will bring them to this screen
if [ "$CHENTRY" = "manual" ] ; then if [ "$CHENTRY" = "manual" ] ; then
# Manual entry of the SSID and password (if appplicable) # Manual entry of the SSID and password (if appplicable)
MSSID=$(echo "enter the SSID of the network (SSID,password)" | wofi -s $HOME/.dotfiles/wofi/style.css -d "Manual Entry: " -c $HOME/.dotfiles/wofi/sidebar) 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)
# Separating the password from the entered string # Separating the password from the entered string
MPASS=$(echo "$MSSID" | awk -F "," '{print $2}') MPASS=$(echo "$MSSID" | awk -F "," '{print $2}')
#echo "$MSSID"
#echo "$MPASS"
# If the user entered a manual password, then use the password nmcli command # If the user entered a manual password, then use the password nmcli command
if [ "$MPASS" = "" ]; then if [ "$MPASS" = "" ]; then
nmcli dev wifi con "$MSSID" nmcli dev wifi con "$MSSID"
@ -67,20 +59,15 @@ elif [ "$CHENTRY" = "toggle off" ]; then
nmcli radio wifi off nmcli radio wifi off
else else
if grep -q "$CHSSID" <<< `echo $KNOWNCON`; then
# If the connection is already in use, then this will still be able to get the SSID
if [ "$CHSSID" = "*" ]; then
CHSSID=$(echo "$CHENTRY" | sed 's/\s\{2,\}/\|/g' | awk -F "|" '{print $3}')
fi
# Parses the list of preconfigured connections to see if it already contains the chosen SSID. This speeds up the connection process
if [[ $(echo "$KNOWNCON" | grep "$CHSSID") = "$CHSSID" ]]; then
nmcli con up "$CHSSID" nmcli con up "$CHSSID"
else else
if [[ "$CHENTRY" =~ "WPA2" ]] || [[ "$CHENTRY" =~ "WEP" ]]; then if [[ "$CHENTRY" =~ "" ]]; then
WIFIPASS=$(echo "if connection is stored, hit enter" | wofi -s $HOME/.dotfiles/wofi/style.css -P -d --prompt "password" -c $HOME/.dotfiles/wofi/sidebar) 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
fi fi
nmcli dev wifi con "$CHSSID" password "$WIFIPASS" nmcli dev wifi con "$CHSSID" password "$WIFIPASS"
fi fi
fi fi