Update powermenu to use hyprctl

This commit is contained in:
Evie Litherland-Smith 2023-06-27 15:11:01 +01:00
parent 0278f0a7bc
commit 121eebc2b2

View file

@ -58,53 +58,53 @@ preferred_launcher="rofi"
usage="$(basename "$0") [-h] [-c] [-p name] -- display a menu for shutdown, reboot, lock etc. usage="$(basename "$0") [-h] [-c] [-p name] -- display a menu for shutdown, reboot, lock etc.
where: where:
-h show this help text -h show this help text
-c ask for user confirmation -c ask for user confirmation
-p preferred launcher (rofi or zenity) -p preferred launcher (rofi or zenity)
This script depends on: This script depends on:
- systemd, - systemd,
- rofi or zenity." - rofi or zenity."
# Check whether the user-defined launcher is valid # Check whether the user-defined launcher is valid
launcher_list=(rofi zenity) launcher_list=(rofi zenity)
function check_launcher() { function check_launcher() {
if [[ ! "${launcher_list[@]}" =~ (^|[[:space:]])"$1"($|[[:space:]]) ]]; then if [[ ! "${launcher_list[@]}" =~ (^|[[:space:]])"$1"($|[[:space:]]) ]]; then
echo "Supported launchers: ${launcher_list[*]}" echo "Supported launchers: ${launcher_list[*]}"
exit 1 exit 1
else else
# Get array with unique elements and preferred launcher first # Get array with unique elements and preferred launcher first
# Note: uniq expects a sorted list, so we cannot use it # Note: uniq expects a sorted list, so we cannot use it
i=1 i=1
launcher_list=($(for l in "$1" "${launcher_list[@]}"; do printf "%i %s\n" "$i" "$l"; let i+=1; done \ launcher_list=("$(for l in "$1" "${launcher_list[@]}"; do printf "%i %s\n" "$i" "$l"; let i+=1; done \
| sort -uk2 | sort -nk1 | cut -d' ' -f2- | tr '\n' ' ')) | sort -uk2 | sort -nk1 | cut -d' ' -f2- | tr '\n' ' ')")
fi fi
} }
# Parse CLI arguments # Parse CLI arguments
while getopts "hcp:" option; do while getopts "hcp:" option; do
case "${option}" in case "$option" in
h) echo "${usage}" h) echo "$usage"
exit 0 exit 0
;; ;;
c) enable_confirmation=true c) enable_confirmation=true
;; ;;
p) preferred_launcher="${OPTARG}" p) preferred_launcher="$OPTARG"
check_launcher "${preferred_launcher}" check_launcher "$preferred_launcher"
;; ;;
*) exit 1 *) exit 1
;; ;;
esac esac
done done
# Check whether a command exists # Check whether a command exists
function command_exists() { function command_exists() {
command -v "$1" &> /dev/null 2>&1 command -v "$1" &> /dev/null 2>&1
} }
# systemctl required # systemctl required
if ! command_exists systemctl ; then if ! command_exists systemctl ; then
exit 1 exit 1
fi fi
# menu defined as an associative array # menu defined as an associative array
@ -113,13 +113,13 @@ typeset -A menu
# Menu with keys/commands # Menu with keys/commands
menu=( menu=(
[ Shutdown]="systemctl poweroff" [ Shutdown]="systemctl poweroff"
[ Reboot]="systemctl reboot" [ Reboot]="systemctl reboot"
[ Suspend]="systemctl suspend" [ Suspend]="systemctl suspend"
[ Hibernate]="systemctl hibernate" [ Hibernate]="systemctl hibernate"
[ Lock]="swaymsg exec swaylock" [ Lock]="hyprctl dispatch exec swaylock"
[ Logout]="swaymsg exit" [ Logout]="hyprctl dispatch exit"
[ Cancel]="" [ Cancel]=""
) )
menu_nrows=${#menu[@]} menu_nrows=${#menu[@]}
@ -132,54 +132,54 @@ launcher_options=""
rofi_colors="" rofi_colors=""
function prepare_launcher() { function prepare_launcher() {
if [[ "$1" == "rofi" ]]; then if [[ "$1" == "rofi" ]]; then
rofi_colors=(-bc "${BORDER_COLOR}" -bg "${BG_COLOR}" -fg "${FG_COLOR}" \ rofi_colors=(-bc "$BORDER_COLOR" -bg "$BG_COLOR" -fg "$FG_COLOR" \
-hlfg "${HLFG_COLOR}" -hlbg "${HLBG_COLOR}") -hlfg "$HLFG_COLOR" -hlbg "$HLBG_COLOR")
launcher_exe="rofi" launcher_exe="rofi"
launcher_options=(-dmenu -i -lines "${menu_nrows}" -p "${ROFI_TEXT}" \ launcher_options=(-dmenu -i -lines "$menu_nrows" -p "$ROFI_TEXT" \
"${rofi_colors}" "${ROFI_OPTIONS[@]}") "$rofi_colors" "${ROFI_OPTIONS[@]}")
elif [[ "$1" == "zenity" ]]; then elif [[ "$1" == "zenity" ]]; then
launcher_exe="zenity" launcher_exe="zenity"
launcher_options=(--list --title="${ZENITY_TITLE}" --text="${ZENITY_TEXT}" \ launcher_options=(--list --title="$ZENITY_TITLE" --text="$ZENITY_TEXT" \
"${ZENITY_OPTIONS[@]}") "${ZENITY_OPTIONS[@]}")
fi fi
} }
for l in "${launcher_list[@]}"; do for l in "${launcher_list[@]}"; do
if command_exists "${l}" ; then if command_exists "$l" ; then
prepare_launcher "${l}" prepare_launcher "$l"
break break
fi fi
done done
# No launcher available # No launcher available
if [[ -z "${launcher_exe}" ]]; then if [[ -z "$launcher_exe" ]]; then
exit 1 exit 1
fi fi
launcher=(${launcher_exe} "${launcher_options[@]}") launcher=("$launcher_exe" "${launcher_options[@]}")
selection="$(printf '%s\n' "${!menu[@]}" | sort | "${launcher[@]}")" selection="$(printf '%s\n' "${!menu[@]}" | sort | "${launcher[@]}")"
function ask_confirmation() { function ask_confirmation() {
if [ "${launcher_exe}" == "rofi" ]; then if [ "$launcher_exe" == "rofi" ]; then
confirmed=$(echo -e "Yes\nNo" | rofi -dmenu -i -lines 2 -p "${selection}?" \ confirmed=$(echo -e "Yes\nNo" | rofi -dmenu -i -lines 2 -p "${selection}?" \
"${rofi_colors}" "${ROFI_OPTIONS[@]}") "$rofi_colors" "${ROFI_OPTIONS[@]}")
[ "${confirmed}" == "Yes" ] && confirmed=0 [ "$confirmed" == "Yes" ] && confirmed=0
elif [ "${launcher_exe}" == "zenity" ]; then elif [ "$launcher_exe" == "zenity" ]; then
zenity --question --text "Are you sure you want to ${selection,,}?" zenity --question --text "Are you sure you want to ${selection,,}?"
confirmed=$? confirmed=$?
fi fi
if [ "${confirmed}" == 0 ]; then if [ "$confirmed" == 0 ]; then
${menu[${selection}]} "${menu[${selection}]}"
fi fi
} }
if [[ $? -eq 0 && ! -z ${selection} ]]; then if [[ $? -eq 0 && ! -z ${selection} ]]; then
if [[ "${enable_confirmation}" = true && \ if [[ "$enable_confirmation" = true && \
${menu_confirm} =~ (^|[[:space:]])"${selection}"($|[[:space:]]) ]]; then ${menu_confirm} =~ (^|[[:space:]])"$selection"($|[[:space:]]) ]]; then
ask_confirmation ask_confirmation
else else
${menu[${selection}]} "${menu[${selection}]}"
fi fi
fi fi