simple-power-menu: convert to posix compliant

This commit is contained in:
Guilherme Rugai Freire 2021-08-20 08:13:51 -03:00
parent 693905cb08
commit 6a8321b312
No known key found for this signature in database
GPG Key ID: 0F9FE41723A8A297
3 changed files with 57 additions and 80 deletions

View File

@ -1 +1 @@
../simple-power-menu/simple-power-menu.bash
../simple-power-menu/simple-power-menu.sh

View File

@ -1,79 +0,0 @@
#!/bin/bash
if [ "$(command -v rofi)" ]; then
CMD='rofi -dmenu';
elif [ "$(command -v dmenu)" ]; then
CMD='dmenu';
else
echo 'Could not find either dmenu or rofi, exiting'
exit 1;
fi
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" | $CMD -l "$PROMPT_LENGHT" -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 | $CMD -l 2 -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

View File

@ -0,0 +1,56 @@
#!/bin/sh
if [ "$(command -v rofi)" ]; then
CMD='rofi -dmenu';
elif [ "$(command -v dmenu)" ]; then
CMD='dmenu';
else
echo 'Could not find either dmenu or rofi, exiting'
exit 1;
fi
fn_logout() {
case "$DESKTOP_SESSION" in
*openbox*) openbox --exit;;
*i3*) i3-msg exit;;
*qtile*) qtile shell -c 'shutdown()';;
*fluxbox*) killall fluxbox;;
*bspwm*) bspc quit 1;;
*) loginctl terminate-session "${XDG_SESSION_ID-}";;
esac
}
OPTIONS="\
Shutdown \tsystemctl poweroff
Reboot \tsystemctl reboot
Suspend \tsystemctl suspend
Log out \tfn_logout"
PROMPT="$(echo "$OPTIONS" | awk -F"\t" '{print $1}')"
PROMPT_LENGHT="$(echo "$PROMPT" | wc -l)"
CHOICE="$(echo "$PROMPT" | $CMD -l "$PROMPT_LENGHT" -p "Power Menu" | xargs)"
if [ -z "$CHOICE" ]; then
exit 0;
fi
COMMAND="$(echo "$OPTIONS" | grep "$CHOICE" | awk -F"\t" '{print $2}')"
if [ -z "$COMMAND" ]; then
exit 0;
fi
CONFIRMATION_CHOICES="no\nyes"
CONFIRMATION="$(echo $CONFIRMATION_CHOICES | $CMD -l 2 -p "Do you want to $CHOICE")"
if [ -z "$CONFIRMATION" ]; then
exit 0;
fi
if [ "$CONFIRMATION" != yes ]; then
exit 0;
fi
$COMMAND