scripts/simple-power-menu/spm
Guilherme Rugai Freire 579dd7d3f2 initial commit
2021-06-03 03:00:52 -03:00

71 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
fn_logout() {
if [[ $DESKTOP_SESSION =~ ^.*openbox$ ]]; then
openbox --exit
elif [[ $DESKTOP_SESSION =~ ^.*i3$ ]]; then
i3-msg exit
elif [[ $DESKTOP_SESSION =~ ^.*qtile$ ]]; then
qtile shell -c 'shutdown()'
elif [[ $DESKTOP_SESSION =~ ^.*fluxbox$ ]]; then
killall fluxbox
elif [[ $DESKTOP_SESSION =~ ^.*bspwm$ ]]; then
bspc quit 1
else
loginctl terminate-session ${XDG_SESSION_ID-}
fi
}
OPTIONS=(
"Shutdown"
"Reboot"
"Suspend"
"Log out"
)
OPTIONS_CMD=(
"systemctl poweroff"
"systemctl reboot"
"systemctl suspend"
"fn_logout"
)
PROMPT=$(printf "%s\\\\n" "${OPTIONS[@]}")
PROMPT="${PROMPT%??}"
PROMPT_LENGHT="${#OPTIONS[@]}"
CHOICE=$(echo -e $PROMPT | rofi -dmenu -l $PROMPT_LENGHT -width 20 -p "Power Menu")
if [[ -z $CHOICE ]]; then
exit 0;
fi
CAN_PROCEDE=0
for i in "${!OPTIONS[@]}"; do
if [[ "${OPTIONS[$i]}" = ${CHOICE} ]]; then
CAN_PROCEDE=1
fi
done
if [ $CAN_PROCEDE -eq 0 ]; then
exit 0;
fi
CONFIRMATION_CHOICES="no\nyes"
CONFIRMATION=$(echo -e $CONFIRMATION_CHOICES | rofi -dmenu -l 2 -width 20 -p "Do you want to $CHOICE")
if [ -z $CONFIRMATION ]; then
exit 0;
fi
if [ $CONFIRMATION != yes ]; then
exit 0;
fi
for i in "${!OPTIONS[@]}"; do
if [[ "${OPTIONS[$i]}" = ${CHOICE} ]]; then
${OPTIONS_CMD[i]}
fi
done