rofi:powermenu: Small fixes and refactoring

Remove ": " from prompts since rofi adds them already.

Change order of yes and no when conforming so that <Enter> cannot
accidentally be pressed twice.

Use tabs instead of spaces.
This commit is contained in:
2020-10-26 15:08:49 +01:00
parent a1990a751c
commit e403a03eca

View File

@@ -3,15 +3,18 @@
# entries with associated commands # entries with associated commands
declare -A entries declare -A entries
entries=( [lock]="xset s activate" entries=(
[lock]="xset s activate"
[logout]="i3-msg exit" [logout]="i3-msg exit"
[reboot]="systemctl reboot" [reboot]="systemctl reboot"
[shutdown]="systemctl poweroff" [shutdown]="systemctl poweroff"
[suspend]="systemctl suspend" [suspend]="systemctl suspend"
[suspend (scheduled)]="scheduled_suspend") [suspend (scheduled)]="scheduled_suspend"
)
declare -a rofi_args declare -a rofi_args
rofi_args=( -no-config rofi_args=(
-no-config
-theme /usr/share/rofi/themes/android_notification.rasi -theme /usr/share/rofi/themes/android_notification.rasi
-lines ${#entries[@]} -lines ${#entries[@]}
-width 12 -width 12
@@ -19,11 +22,12 @@ rofi_args=( -no-config
-yoffset 32 -yoffset 32
-dmenu -dmenu
-no-case-sensitive -no-case-sensitive
-p) # has to end with -p! -p # -p at end for dynamic prompt
)
function scheduled_suspend { scheduled_suspend() {
declare -i min=0 declare -i min=0
min=$(rofi "${rofi_args[@]}" "minutes: ") min=$(rofi "${rofi_args[@]}" "minutes")
# make sure the input was a valid number # make sure the input was a valid number
# side effect: 0 minutes is not possible # side effect: 0 minutes is not possible
@@ -35,18 +39,21 @@ function scheduled_suspend {
# Choose option over rofi # Choose option over rofi
# Note: bash does not keep the order of the keys thus they get sorted # Note: bash does not keep the order of the keys thus they get sorted
chosen="$(printf '%s\n' "${!entries[@]}" | sort | rofi "${rofi_args[@]}" "power: ")" chosen="$(
printf '%s\n' "${!entries[@]}" \
| sort \
| rofi "${rofi_args[@]}" "power"
)"
# exit if nothing was selected # exit if nothing was selected
[[ -n "$chosen" ]] || exit 1 [[ -n "$chosen" ]] || exit 1
# handle scheduled suspend different # handle scheduled suspend different
[[ "$chosen" != "suspend (scheduled)" ]] || { ${entries[$chosen]}; exit $?; } [[ "$chosen" != "suspend (scheduled)" ]] || { ${entries[$chosen]}; exit $?; }
# Confirm choice # Confirm choice
yesNo="$(echo -e "yes\nno" | rofi "${rofi_args[@]}" "Are you sure you want to ${chosen}? ")" yesNo="$(rofi "${rofi_args[@]}" "Confirm to ${chosen}" <<<$'no\nyes')"
# Exit if "No" # Exit if "No"
[[ "$yesNo" == "yes" ]] || exit 1 [[ "$yesNo" == "yes" ]] || exit 1
# Execute # Execute
${entries[$chosen]} ${entries[$chosen]}