Files
dotfiles/rofi/powermenu
2025-02-17 23:48:17 +01:00

113 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
## Author : Aditya Shakya (adi1090x)
## Github : @adi1090x
#
## Rofi : Power Menu
#
## Available Styles
#
## style-1 style-2 style-3 style-4 style-5
# Current Theme
# CMDs
uptime="`uptime -p | sed -e 's/up //g'`"
host=`hostname`
# Options
shutdown=''
reboot=''
lock=''
suspend='󰤄'
logout='󰍃'
yes=''
no=''
# Rofi CMD
rofi_cmd() {
rofi -dmenu \
-p "$host" \
-mesg "Uptime: $uptime" \
-theme powermenu.rasi
}
# Confirmation CMD
confirm_cmd() {
rofi \
-theme-str 'window {location: center; anchor: center; fullscreen: false; width: 250px;}' \
-theme-str 'mainbox {children: [ "message", "listview" ];}' \
-theme-str 'listview {columns: 2; lines: 1;}' \
-theme-str 'element-text {horizontal-align: 0.5; }' \
-theme-str 'element {padding: 0px; }'\
-theme-str 'textbox {horizontal-align: 0.5;}' \
-dmenu \
-p 'Confirmation' \
-mesg 'Are you Sure?' \
-theme powermenu.rasi
}
# Ask for confirmation
confirm_exit() {
echo -e "$yes\n$no" | confirm_cmd
}
# Pass variables to rofi dmenu
run_rofi() {
echo -e "$lock\n$suspend\n$logout\n$reboot\n$shutdown" | rofi_cmd
}
# Execute Command
run_cmd() {
selected="$(confirm_exit)"
if [[ "$selected" == "$yes" ]]; then
if [[ $1 == '--shutdown' ]]; then
shutdown -p now
elif [[ $1 == '--reboot' ]]; then
shutdown -r now
elif [[ $1 == '--suspend' ]]; then
systemctl suspend
elif [[ $1 == '--logout' ]]; then
if [[ "$XDG_SESSION_DESKTOP" == 'openbox' ]]; then
openbox --exit
elif [[ "$XDG_SESSION_DESKTOP" == 'bspwm' ]]; then
bspc quit
elif [[ "$XDG_SESSION_DESKTOP" == 'sway' ]]; then
sway exit
elif [[ "$XDG_SESSION_DESKTOP" == 'i3' ]]; then
i3-msg exit
elif [[ "$XDG_SESSION_DESKTOP" == 'plasma' ]]; then
qdbus org.kde.ksmserver /KSMServer logout 0 0 0
fi
fi
else
exit 0
fi
}
# Actions
chosen="$(run_rofi)"
case ${chosen} in
$shutdown)
run_cmd --shutdown
;;
$reboot)
run_cmd --reboot
;;
$lock)
if command -v swaylock 2>&1 >/dev/null; then
swaylock -f -c 000000 --clock
elif command -v betterlockscreen 2>&1 >/dev/null; then
betterlockscreen -l
elif command -v i3lock 2>&1 >/dev/null; then
i3lock
fi
;;
$suspend)
run_cmd --suspend
;;
$logout)
run_cmd --logout
;;
esac