reduce code duplication

This commit is contained in:
risolij 2022-01-23 10:40:25 -07:00
parent aba9eb3d1a
commit 3c49b3d6fa
2 changed files with 48 additions and 80 deletions

View File

@ -54,30 +54,36 @@ msg() {
rofi -theme "$dir/message.rasi" -e "Available Options - yes / y / no / n"
}
run_response() {
local response="$1"
local run="$2"
case "$response" in
YES|yes|[yY])
if [[ "$run" == "systemctl suspend" ]]; then
mpc -q pause
amixer set Master mute
$run
else
$run
fi
;;
NO|no|[Nn]) exit 0; ;;
*) msg ; ;;
esac
}
# Variable passed to rofi
options="$shutdown\n$reboot\n$lock\n$suspend\n$logout"
chosen="$(echo -e "$options" | $rofi_command -p "Uptime: $uptime" -dmenu -selected-row 2)"
case $chosen in
$shutdown)
ans=$(confirm_exit &)
if [[ $ans == "yes" || $ans == "YES" || $ans == "y" || $ans == "Y" ]]; then
systemctl poweroff
elif [[ $ans == "no" || $ans == "NO" || $ans == "n" || $ans == "N" ]]; then
exit 0
else
msg
fi
run_response "$(confirm_exit &)" "systemctl poweroff"
;;
$reboot)
ans=$(confirm_exit &)
if [[ $ans == "yes" || $ans == "YES" || $ans == "y" || $ans == "Y" ]]; then
systemctl reboot
elif [[ $ans == "no" || $ans == "NO" || $ans == "n" || $ans == "N" ]]; then
exit 0
else
msg
fi
run_response "$(confirm_exit &)" "systemctl reboot"
;;
$lock)
if [[ -f /usr/bin/i3lock ]]; then
@ -87,31 +93,9 @@ case $chosen in
fi
;;
$suspend)
ans=$(confirm_exit &)
if [[ $ans == "yes" || $ans == "YES" || $ans == "y" || $ans == "Y" ]]; then
mpc -q pause
amixer set Master mute
systemctl suspend
elif [[ $ans == "no" || $ans == "NO" || $ans == "n" || $ans == "N" ]]; then
exit 0
else
msg
fi
run_response "$(confirm_exit &)" "systemctl suspend"
;;
$logout)
ans=$(confirm_exit &)
if [[ $ans == "yes" || $ans == "YES" || $ans == "y" || $ans == "Y" ]]; then
if [[ "$DESKTOP_SESSION" == "Openbox" ]]; then
openbox --exit
elif [[ "$DESKTOP_SESSION" == "bspwm" ]]; then
bspc quit
elif [[ "$DESKTOP_SESSION" == "i3" ]]; then
i3-msg exit
fi
elif [[ $ans == "no" || $ans == "NO" || $ans == "n" || $ans == "N" ]]; then
exit 0
else
msg
fi
run_response "$(confirm_exit &)" "loginctl terminate-user $USER"
;;
esac

View File

@ -54,30 +54,36 @@ msg() {
rofi -theme "$dir/message.rasi" -e "Available Options - yes / y / no / n"
}
run_response() {
local response="$1"
local run="$2"
case "$response" in
YES|yes|[yY])
if [[ "$run" == "systemctl suspend" ]]; then
mpc -q pause
amixer set Master mute
$run
else
$run
fi
;;
NO|no|[Nn]) exit 0; ;;
*) msg ; ;;
esac
}
# Variable passed to rofi
options="$shutdown\n$reboot\n$lock\n$suspend\n$logout"
chosen="$(echo -e "$options" | $rofi_command -p "Uptime: $uptime" -dmenu -selected-row 2)"
case $chosen in
$shutdown)
ans=$(confirm_exit &)
if [[ $ans == "yes" || $ans == "YES" || $ans == "y" || $ans == "Y" ]]; then
systemctl poweroff
elif [[ $ans == "no" || $ans == "NO" || $ans == "n" || $ans == "N" ]]; then
exit 0
else
msg
fi
run_response "$(confirm_exit &)" "systemctl poweroff"
;;
$reboot)
ans=$(confirm_exit &)
if [[ $ans == "yes" || $ans == "YES" || $ans == "y" || $ans == "Y" ]]; then
systemctl reboot
elif [[ $ans == "no" || $ans == "NO" || $ans == "n" || $ans == "N" ]]; then
exit 0
else
msg
fi
run_response "$(confirm_exit &)" "systemctl reboot"
;;
$lock)
if [[ -f /usr/bin/i3lock ]]; then
@ -87,31 +93,9 @@ case $chosen in
fi
;;
$suspend)
ans=$(confirm_exit &)
if [[ $ans == "yes" || $ans == "YES" || $ans == "y" || $ans == "Y" ]]; then
mpc -q pause
amixer set Master mute
systemctl suspend
elif [[ $ans == "no" || $ans == "NO" || $ans == "n" || $ans == "N" ]]; then
exit 0
else
msg
fi
run_response "$(confirm_exit &)" "systemctl suspend"
;;
$logout)
ans=$(confirm_exit &)
if [[ $ans == "yes" || $ans == "YES" || $ans == "y" || $ans == "Y" ]]; then
if [[ "$DESKTOP_SESSION" == "Openbox" ]]; then
openbox --exit
elif [[ "$DESKTOP_SESSION" == "bspwm" ]]; then
bspc quit
elif [[ "$DESKTOP_SESSION" == "i3" ]]; then
i3-msg exit
fi
elif [[ $ans == "no" || $ans == "NO" || $ans == "n" || $ans == "N" ]]; then
exit 0
else
msg
fi
run_response "$(confirm_exit &)" "loginctl terminate-user $USER"
;;
esac