58 lines
2.0 KiB
Bash
Executable File
58 lines
2.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# 1. Get current connection info
|
|
current_ssid=$(nmcli -t -f active,ssid dev wifi | grep '^yes' | cut -d: -f2)
|
|
|
|
# 2. Get the list of networks
|
|
# Format: IN-USE:SIGNAL:BARS:SSID:SECURITY
|
|
wifi_list=$(nmcli -f IN-USE,SIGNAL,BARS,SSID,SECURITY device wifi list | tail -n +2)
|
|
|
|
# 3. Process the list for Rofi
|
|
# We use AWK to format and ensure we don't show duplicate SSIDs (common with dual-band routers)
|
|
formatted_list=$(echo "$wifi_list" | awk -F' +' '{
|
|
icon = ($1 == "*") ? "" : "";
|
|
# Use different icons based on signal strength if not connected
|
|
if ($1 != "*") {
|
|
if ($2 > 70) icon = "";
|
|
else if ($2 > 40) icon = "";
|
|
else icon = "";
|
|
}
|
|
# Mark secure networks
|
|
lock = ($5 ~ /WPA/ || $5 ~ /WEP/) ? " " : "";
|
|
|
|
printf "%s %-20s %s %s\n", icon, $4, $3, lock
|
|
}' | sort -u -k2,2)
|
|
|
|
# 4. Show the menu
|
|
selected=$(echo -e "$formatted_list" | rofi -dmenu -i -p "Wi-Fi" -config "$HOME/.config/rofi/wifi.rasi")
|
|
|
|
[ -z "$selected" ] && exit
|
|
|
|
# Extract SSID (Removes icons, signal bars, and lock icon)
|
|
# This looks for the name specifically in the second column
|
|
target_ssid=$(echo "$selected" | awk '{print $2}')
|
|
|
|
# 5. Connection Logic
|
|
if [[ "$selected" == ""* ]]; then
|
|
# Already connected? Ask to disconnect
|
|
res=$(echo -e "Yes\nNo" | rofi -dmenu -p "Disconnect from $target_ssid?")
|
|
[ "$res" == "Yes" ] && nmcli device disconnect wlan0
|
|
else
|
|
# Check if it's a known connection
|
|
known=$(nmcli -t -f name connection show | grep "^$target_ssid$")
|
|
|
|
if [ -n "$known" ]; then
|
|
notify-send "Wi-Fi" "Connecting to known network: $target_ssid"
|
|
nmcli connection up "$target_ssid"
|
|
else
|
|
# New network - ask for password if it has the lock icon
|
|
if [[ "$selected" == *""* ]]; then
|
|
pass=$(rofi -dmenu -p "Password for $target_ssid" -password)
|
|
[ -z "$pass" ] && exit
|
|
nmcli device wifi connect "$target_ssid" password "$pass"
|
|
else
|
|
nmcli device wifi connect "$target_ssid"
|
|
fi
|
|
fi
|
|
fi
|