1
0
mirror of https://github.com/dcarrillo/dotfiles.git synced 2024-12-24 17:36:47 +00:00
dotfiles/.config/polybar/scripts/nmcli_manager

63 lines
1.3 KiB
Plaintext
Raw Normal View History

2019-07-18 16:45:46 +00:00
#!/usr/bin/env bash
#
# nmcli connection switching from a rofi menu.
# When a network is selected its status is switched from UP to DOWN or vice versa.
#
# WARNING, connection name can't contain a # or | symbol
#
ROFI="rofi -dmenu
-sep #
-i -p Network:
-theme $ROFI_THEME
2019-07-18 16:45:46 +00:00
-location 3
-yoffset +45
-xoffset -80
-width 30
"
build_rofi_menu()
2019-07-18 16:45:46 +00:00
{
while read -r line
do
name="$(echo "$line" | cut -d':' -f1)"
dtype="$(echo "$line" | cut -d':' -f2)"
device="$(echo "$line" | cut -d':' -f3)"
2019-07-18 16:45:46 +00:00
[ -z "$device" ] && status= || status=
2019-07-18 16:45:46 +00:00
dtype=${dtype##*-}
option=$(printf '%-35s | %-15s | %s\n' "$name" "$dtype" $status)
2019-08-07 14:44:17 +00:00
2019-07-18 16:45:46 +00:00
menu="$menu # $option"
done < <(nmcli -c no --terse -f NAME,TYPE,DEVICE connection show)
echo "$menu" | cut -c 3-
2019-07-18 16:45:46 +00:00
}
switch_connections()
2019-07-18 16:45:46 +00:00
{
name="$1"
status=$2
2019-07-18 16:45:46 +00:00
if [ "$status" = "" ]; then
2019-07-18 16:45:46 +00:00
nmcli connection down "$name"
else
nmcli connection up "$name"
fi
}
main()
2019-07-18 16:45:46 +00:00
{
menu=$(build_rofi_menu)
choice=$(echo "$menu" | $ROFI)
[ "$?" -ne 0 ] && return 1
2019-07-18 16:45:46 +00:00
name=$(echo "$choice" | cut -d '|' -f 1 | awk '{$1=$1;print}')
stat=$(echo "$choice" | cut -d '|' -f 3 | awk '{$1=$1;print}')
2019-07-18 16:45:46 +00:00
switch_connections "$name" "$stat"
2019-07-18 16:45:46 +00:00
}
main