Fix power menu

This commit is contained in:
The-Repo-Club 2023-08-14 00:27:01 +01:00
parent a0adfd9d34
commit 28b009bedd
No known key found for this signature in database
GPG Key ID: E30EC2FBFB05C44F
3 changed files with 149 additions and 189 deletions

View File

@ -84,7 +84,7 @@ class Commands(object):
passmenu = "passmenu" passmenu = "passmenu"
autostart = ["autostart"] autostart = ["autostart"]
# configure = ['autorandr --load qtile'] configure = ["youtube_subs -d"]
################## ##################

View File

@ -12,7 +12,7 @@
#Created: #Created:
# Wed 07 December 2022, 11:01:52 PM [GMT] # Wed 07 December 2022, 11:01:52 PM [GMT]
#Modified: #Modified:
# Sat 22 July 2023, 09:46:55 PM [GMT+1] # Mon 14 August 2023, 12:26:30 AM [GMT+1]
# #
#Description: #Description:
# This script defines just a mode for rofi instead of being a self-contained # This script defines just a mode for rofi instead of being a self-contained
@ -20,17 +20,16 @@
# running rofi inside this script as now the user can call rofi as one pleases. # running rofi inside this script as now the user can call rofi as one pleases.
# For instance: # For instance:
# #
# rofi -show powermenu -modi powermenu:~/.config/rofi/powermenu # rofi -show powermenu -config ~/.config/rofi/powermenu.rasi
# #
#Dependencies: #Dependencies:
# <None> # <None>
# #
# shellcheck disable=all # shellcheck disable=all
if [ -z "${ROFI_OUTSIDE}" ] if [ -z "${ROFI_OUTSIDE}" ]; then
then echo "run this script in rofi".
echo "run this script in rofi". exit
exit
fi fi
set -e set -e
@ -75,161 +74,144 @@ dryrun=false
showsymbols=true showsymbols=true
getuptime() { getuptime() {
uptime -p >/dev/null 2>&1 state=$(uptime | sed -E 's/^[^,]*up *//; s/mins/minutes/; s/hrs?/hours/;
if [ "$?" -eq 0 ]; then
# Supports most Linux distro
# when the machine is up for less than '0' minutes then
# 'uptime -p' returns ONLY 'up', so we need to set a default value
UP_SET_OR_EMPTY=$(uptime -p | awk -F 'up ' '{print $2}')
UP=${UP_SET_OR_EMPTY:-'less than a minute'}
else
# Supports Mac OS X, Debian 7, etc
UP=$(uptime | sed -E 's/^[^,]*up *//; s/mins/minutes/; s/hrs?/hours/;
s/([[:digit:]]+):0?([[:digit:]]+)/\1 hours, \2 minutes/; s/([[:digit:]]+):0?([[:digit:]]+)/\1 hours, \2 minutes/;
s/^1 hours/1 hour/; s/ 1 hours/ 1 hour/; s/^1 hours/1 hour/; s/ 1 hours/ 1 hour/;
s/min,/minutes,/; s/ 0 minutes,/ less than a minute,/; s/ 1 minutes/ 1 minute/; s/min,/minutes,/; s/ 0 minutes,/ less than a minute,/; s/ 1 minutes/ 1 minute/;
s/ / /; s/, *[[:digit:]]* users?.*//') s/ / /; s/, *[[:digit:]]* users?.*//')
fi
state="$UP"
} }
function check_valid { function check_valid {
option="$1" option="$1"
shift 1 shift 1
for entry in "${@}" for entry in "${@}"; do
do if [ -z "${actions[$entry]+x}" ]; then
if [ -z "${actions[$entry]+x}" ] echo "Invalid choice in $option: $entry" >&2
then exit 1
echo "Invalid choice in $option: $entry" >&2 fi
exit 1 done
fi
done
} }
# Parse command-line options # Parse command-line options
parsed=$(getopt --options=h --longoptions=help,dry-run,confirm:,choices:,choose:,symbols,no-symbols --name "$0" -- "$@") parsed=$(getopt --options=h --longoptions=help,dry-run,confirm:,choices:,choose:,symbols,no-symbols --name "$0" -- "$@")
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo 'Terminating...' >&2 echo 'Terminating...' >&2
exit 1 exit 1
fi fi
eval set -- "$parsed" eval set -- "$parsed"
unset parsed unset parsed
while true; do while true; do
case "$1" in case "$1" in
"-h"|"--help") "-h" | "--help")
echo "rofi-power-menu - a power menu mode for Rofi" echo "rofi-power-menu - a power menu mode for Rofi"
echo echo
echo "Usage: rofi-power-menu [--choices CHOICES] [--confirm CHOICES]" echo "Usage: rofi-power-menu [--choices CHOICES] [--confirm CHOICES]"
echo " [--choose CHOICE] [--dry-run] [--symbols|--no-symbols]" echo " [--choose CHOICE] [--dry-run] [--symbols|--no-symbols]"
echo echo
echo "Use with Rofi in script mode. For instance, to ask for shutdown or reboot:" echo "Use with Rofi in script mode. For instance, to ask for shutdown or reboot:"
echo echo
echo " rofi -show menu -modi \"menu:rofi-power-menu --choices=shutdown/reboot\"" echo " rofi -show menu -modi \"menu:rofi-power-menu --choices=shutdown/reboot\""
echo echo
echo "Available options:" echo "Available options:"
echo " --dry-run Don't perform the selected action but print it to stderr." echo " --dry-run Don't perform the selected action but print it to stderr."
echo " --choices CHOICES Show only the selected choices in the given order. Use / " echo " --choices CHOICES Show only the selected choices in the given order. Use / "
echo " as the separator. Available choices are lockscreen, logout," echo " as the separator. Available choices are lockscreen, logout,"
echo " suspend, hibernate, reboot and shutdown. By default, all" echo " suspend, hibernate, reboot and shutdown. By default, all"
echo " available choices are shown." echo " available choices are shown."
echo " --confirm CHOICES Require confirmation for the gives choices only. Use / as" echo " --confirm CHOICES Require confirmation for the gives choices only. Use / as"
echo " the separator. Available choices are lockscreen, logout," echo " the separator. Available choices are lockscreen, logout,"
echo " suspend, hibernate, reboot and shutdown. By default, only" echo " suspend, hibernate, reboot and shutdown. By default, only"
echo " irreversible actions logout, reboot and shutdown require" echo " irreversible actions logout, reboot and shutdown require"
echo " confirmation." echo " confirmation."
echo " --choose CHOICE Preselect the given choice and only ask for a confirmation" echo " --choose CHOICE Preselect the given choice and only ask for a confirmation"
echo " (if confirmation is set to be requested). It is strongly" echo " (if confirmation is set to be requested). It is strongly"
echo " recommended to combine this option with --confirm=CHOICE" echo " recommended to combine this option with --confirm=CHOICE"
echo " if the choice wouldn't require confirmation by default." echo " if the choice wouldn't require confirmation by default."
echo " Available choices are lockscreen, logout, suspend," echo " Available choices are lockscreen, logout, suspend,"
echo " hibernate, reboot and shutdown." echo " hibernate, reboot and shutdown."
echo " --[no-]symbols Show Unicode symbols or not. Requires a font with support" echo " --[no-]symbols Show Unicode symbols or not. Requires a font with support"
echo " for the symbols. Use, for instance, fonts from the" echo " for the symbols. Use, for instance, fonts from the"
echo " Nerdfonts collection. By default, they are shown" echo " Nerdfonts collection. By default, they are shown"
echo " -h,--help Show this help text." echo " -h,--help Show this help text."
exit 0 exit 0
;; ;;
"--dry-run") "--dry-run")
dryrun=true dryrun=true
shift 1 shift 1
;; ;;
"--confirm") "--confirm")
IFS='/' read -ra confirmations <<< "$2" IFS='/' read -ra confirmations <<<"$2"
check_valid "$1" "${confirmations[@]}" check_valid "$1" "${confirmations[@]}"
shift 2 shift 2
;; ;;
"--choices") "--choices")
IFS='/' read -ra show <<< "$2" IFS='/' read -ra show <<<"$2"
check_valid "$1" "${show[@]}" check_valid "$1" "${show[@]}"
shift 2 shift 2
;; ;;
"--choose") "--choose")
# Check that the choice is valid # Check that the choice is valid
check_valid "$1" "$2" check_valid "$1" "$2"
selectionID="$2" selectionID="$2"
shift 2 shift 2
;; ;;
"--symbols") "--symbols")
showsymbols=true showsymbols=true
shift 1 shift 1
;; ;;
"--no-symbols") "--no-symbols")
showsymbols=false showsymbols=false
shift 1 shift 1
;; ;;
"--") "--")
shift shift
break break
;; ;;
*) *)
echo "Internal error" >&2 echo "Internal error" >&2
exit 1 exit 1
;; ;;
esac esac
done done
# Define the messages after parsing the CLI options so that it is possible to # Define the messages after parsing the CLI options so that it is possible to
# configure them in the future. # configure them in the future.
function write_message { function write_message {
icon="<span font_size=\"medium\">$1</span>" icon="<span font_size=\"medium\">$1</span>"
text="<span font_size=\"medium\">$2</span>" text="<span font_size=\"medium\">$2</span>"
if [ "$showsymbols" = "true" ] if [ "$showsymbols" = "true" ]; then
then echo -n "\u200e$icon \u2068$text\u2069"
echo -n "\u200e$icon \u2068$text\u2069" else
else echo -n "$text"
echo -n "$text" fi
fi
} }
function print_selection { function print_selection {
echo -e "$1" | $(read -r -d '' entry; echo "echo $entry") echo -e "$1" | $(
read -r -d '' entry
echo "echo $entry"
)
} }
declare -A messages declare -A messages
declare -A confirmationMessages declare -A confirmationMessages
for entry in "${all[@]}" for entry in "${all[@]}"; do
do messages[$entry]=$(write_message "${icons[$entry]}" "${texts[$entry]^}")
messages[$entry]=$(write_message "${icons[$entry]}" "${texts[$entry]^}")
done done
for entry in "${all[@]}" for entry in "${all[@]}"; do
do confirmationMessages[$entry]=$(write_message "${icons[$entry]}" "Yes, ${texts[$entry]}")
confirmationMessages[$entry]=$(write_message "${icons[$entry]}" "Yes, ${texts[$entry]}")
done done
confirmationMessages[cancel]=$(write_message "${icons[cancel]}" "No, cancel") confirmationMessages[cancel]=$(write_message "${icons[cancel]}" "No, cancel")
if [ $# -gt 0 ] if [ $# -gt 0 ]; then
then # If arguments given, use those as the selection
# If arguments given, use those as the selection selection="${*}"
selection="${*}"
else else
# Otherwise, use the CLI passed choice if given # Otherwise, use the CLI passed choice if given
if [ -n "${selectionID+x}" ] if [ -n "${selectionID+x}" ]; then
then selection="${messages[$selectionID]}"
selection="${messages[$selectionID]}" fi
fi
fi fi
# Don't allow custom entries # Don't allow custom entries
@ -237,54 +219,45 @@ echo -e "\0no-custom\x1ftrue"
echo -e "\0markup-rows\x1ftrue" echo -e "\0markup-rows\x1ftrue"
if [ -z "${selection+x}" ] if [ -z "${selection+x}" ]; then
then getuptime
getuptime echo -e "\0prompt\x1fPower Menu"
echo -e "\0prompt\x1fPower Menu" echo -en "\0message\x1fUptime :: ${state^}\n"
echo -en "\0message\x1fUptime :: ${state^}\n" for entry in "${show[@]}"; do
for entry in "${show[@]}" echo -e "${messages[$entry]}\0icon\x1f${icons[$entry]}"
do done
echo -e "${messages[$entry]}\0icon\x1f${icons[$entry]}"
done
else else
for entry in "${show[@]}" for entry in "${show[@]}"; do
do if [ "$selection" = "$(print_selection "${messages[$entry]}")" ]; then
if [ "$selection" = "$(print_selection "${messages[$entry]}")" ] # Check if the selected entry is listed in confirmation requirements
then for confirmation in "${confirmations[@]}"; do
# Check if the selected entry is listed in confirmation requirements if [ "$entry" = "$confirmation" ]; then
for confirmation in "${confirmations[@]}" # Ask for confirmation
do echo -e "\0prompt\x1fAre you sure"
if [ "$entry" = "$confirmation" ] echo -e "${confirmationMessages[$entry]}\0icon\x1f${icons[$entry]}"
then echo -e "${confirmationMessages[cancel]}\0icon\x1f${icons[cancel]}"
# Ask for confirmation exit 0
echo -e "\0prompt\x1fAre you sure"
echo -e "${confirmationMessages[$entry]}\0icon\x1f${icons[$entry]}"
echo -e "${confirmationMessages[cancel]}\0icon\x1f${icons[cancel]}"
exit 0
fi
done
# If not, then no confirmation is required, so mark confirmed
selection=$(print_selection "${confirmationMessages[$entry]}")
fi fi
if [ "$selection" = "$(print_selection "${confirmationMessages[$entry]}")" ] done
then # If not, then no confirmation is required, so mark confirmed
if [ $dryrun = true ] selection=$(print_selection "${confirmationMessages[$entry]}")
then fi
# Tell what would have been done if [ "$selection" = "$(print_selection "${confirmationMessages[$entry]}")" ]; then
echo "Selected: $entry" >&2 if [ $dryrun = true ]; then
else # Tell what would have been done
# Perform the action echo "Selected: $entry" >&2
${actions[$entry]} else
fi # Perform the action
exit 0 ${actions[$entry]}
fi fi
if [ "$selection" = "$(print_selection "${confirmationMessages[cancel]}")" ] exit 0
then fi
# Do nothing if [ "$selection" = "$(print_selection "${confirmationMessages[cancel]}")" ]; then
exit 0 # Do nothing
fi exit 0
done fi
# The selection didn't match anything, so raise an error done
echo "Invalid selection: $selection" >&2 # The selection didn't match anything, so raise an error
exit 1 echo "Invalid selection: $selection" >&2
exit 1
fi fi

View File

@ -9,19 +9,6 @@ if [ -f "$xprofile" ]; then
source "$xprofile" source "$xprofile"
fi fi
if [ -d "/usr/bin/husky" ]; then
export HUSKY_PATH=""
HUSKY_PATH="$HOME/.local/share/husky-repo/core"
HUSKY_PATH="$HOME/.local/share/husky-repo/extra:$HUSKY_PATH"
HUSKY_PATH="$HOME/.local/share/husky-repo/xorg:$HUSKY_PATH"
HUSKY_PATH="$HOME/.local/share/husky-repo/wayland:$HUSKY_PATH"
HUSKY_PATH="$HOME/.local/share/husky-repo/community:$HUSKY_PATH"
HUSKY_PATH="$HOME/.local/share/husky-repo/games:$HUSKY_PATH"
HUSKY_PATH="$HOME/.local/share/husky-repo/bin:$HUSKY_PATH"
HUSKY_PATH="$HOME/.local/share/husky-repo/git:$HUSKY_PATH"
HUSKY_PATH="$HOME/.local/share/husky-repo/testing:$HUSKY_PATH"
fi
# enable XDG_CONFIG_HOME # enable XDG_CONFIG_HOME
if [[ -z "$XDG_CONFIG_HOME" ]]; then if [[ -z "$XDG_CONFIG_HOME" ]]; then
export XDG_CONFIG_HOME="$HOME/.config/" export XDG_CONFIG_HOME="$HOME/.config/"