Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- wifi_on='直'
- wifi_off='睊'
- wifi_disconnect='泌'
- wifi_connected=''
- wifi_lock=''
- wifi_unlock=''
- signal_weak='◔'
- signal_fair='◑'
- signal_good='◕'
- signal_strong='●'
- rofi_command="rofi -dmenu -no-fixed-num-lines \
- -yoffset -100 -i -p"
- divider="━━━━━━━━━━━━━━━━━━━━━━━"
- power_p() {
- if nmcli --fields WIFI g | grep -q "enabled"; then
- return 0
- else
- return 1
- fi
- }
- connected_p() {
- local wifi=$(nmcli d | grep "wifi")
- if echo $wifi | grep -q "disconnected"; then
- return 1
- else
- return 0
- fi
- }
- connection_current(){
- echo $(nmcli d | grep "wifi" | sed 's|.*connected||;s/ //g')
- }
- power_toggle() {
- if power_p; then
- nmcli radio wifi off
- else
- nmcli radio wifi on
- fi
- }
- connections_get() {
- nmcli -t d wifi rescan
- local connection_list=$(
- nmcli --fields SECURITY,SSID,BARS device wifi list |
- # sed '/^--/d' |
- sed 1d |
- sed "s/▂___/~$signal_weak/;s/▂▄__/~$signal_fair/;s/▂▄▆_/~$signal_good/;s/▂▄▆█/~$signal_strong/" |
- sed -E "s/WPA*.?\S/$wifi_lock~/g" |
- sed "s/$wifi_lock~ $wifi_lock~/$wifi_lock~/g;s/802\.1X//g;s/--/$wifi_unlock~/g;s/ *~/~/g;s/~ */~/g")
- if connected_p; then
- local connection_current_=$(connection_current)
- local connection_new=$(
- echo "$connection_list" |
- sed "s/$connection_current_/$wifi_connected $connection_current_ $wifi_connected/" |
- column -t -s '~')
- echo "$connection_new"
- else
- local connection_new=$(
- echo "$connection_list" |
- column -t -s '~')
- echo "$connection_new"
- fi
- }
- ssid_get() {
- local if_current=$(connection_current)
- if [[ "$1" =~ "$if_current" ]]; then
- echo "$if_current"
- return 0
- else
- echo "$1" | awk -F " " '{ print $2 }'
- return 1
- fi
- }
- password_enter() {
- rofi -dmenu\
- -i\
- -no-fixed-num-lines\
- -p "Enter password"
- }
- confirm() {
- rofi -dmenu\
- -i\
- -no-fixed-num-lines\
- -p "Are you sure? ((Y)es): "
- }
- yes_p() {
- local ans=$(confirm &)
- if [[ $ans == "Y" || $ans == "y" || $ans == "Н" || $ans == "н" ]]; then
- return 0
- else
- return 1
- fi
- }
- connection_menu() {
- local ssid=$(ssid_get "$1")
- if ssid_get "$1"; then
- local options="Disconnect\nDelete\n$divider\nBack"
- else
- local options="Connect\n$divider\nBack"
- fi
- local chosen="$(echo -e "$options" | $rofi_command "$ssid")"
- case $chosen in
- "")
- echo "No option chosen."
- ;;
- $divider)
- connection_menu "$1"
- ;;
- "Connect")
- if nmcli connection show | grep -q "$ssid"; then
- if nmcli con up "$ssid"; then
- notify-send "Connected to $ssid"
- else
- notify-send "Connection failed"
- connection_menu "$1"
- fi
- elif [[ "$1" =~ "$wifi_lock" ]]; then
- local password=$(password_enter)
- if nmcli dev wifi con "$ssid" password "$password"; then
- notify-send "Connected to $ssid"
- else
- notify-send "Connection failed"
- connection_menu "$1"
- fi
- else
- if nmcli dev wifi con "$ssid"; then
- notify-send "Connected to $ssid"
- else
- notify-send "Connection failed"
- connection_menu "$1"
- fi
- fi
- ;;
- "Disconnect")
- nmcli con down "$ssid"
- notify-send "Disconnected"
- ;;
- "Delete")
- if yes_p; then
- nmcli connection delete id "$ssid"
- notify-send "Connection $ssid deleted"
- else
- connection_menu "$1"
- fi
- ;;
- "Back")
- main_menu
- ;;
- esac
- }
- main_menu() {
- if power_p; then
- local power="WIFI: on"
- local list=$(connections_get)
- local options="$list\n$divider\n$power\nExit"
- else
- local power="WIFI: off"
- local options="$power\nExit"
- fi
- local chosen="$(echo -e "$options" | $rofi_command "WiFi")"
- case $chosen in
- "")
- echo "No option chosen."
- ;;
- "Exit")
- exit 0
- ;;
- $divider)
- main_menu
- ;;
- $power)
- power_toggle
- ;;
- *)
- if [[ $chosen ]] ; then connection_menu "$chosen"; fi
- ;;
- esac
- }
- print_status() {
- if power_p; then
- if connected_p; then
- echo $wifi_on
- else
- echo $wifi_disconnect
- fi
- else
- echo $wifi_off
- fi
- }
- case "$1" in
- --status)
- print_status
- ;;
- --toggle)
- power_toggle
- ;;
- *)
- main_menu
- ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement