aboutsummaryrefslogtreecommitdiffstats
path: root/bin/udt-powermenu.sh
blob: a8524b47a9b6b5861cdc3e690b279dd5b71d1da1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env bash

## Author : Aditya Shakya (adi1090x)
## Github : @adi1090x
## Derived from adi1090x/rofi, GPL-3.0 like upstream. Modifications
## Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
## Rofi   : Power Menu
#
## Available Styles
#
## style-1   style-2   style-3   style-4   style-5

# Current Theme
dir="$HOME/.config/rofi/udt"
theme='powermenu'

# CMDs
uptime="`uptime -p | sed -e 's/up //g'`"
host=`hostname`

# Options
shutdown=''
reboot=''
logout=''
sleep='󰤄'
yes='󰗠'
no='󰅙'

# Rofi CMD
rofi_cmd() {
	rofi -dmenu \
		-p "'ndi vidimu ${USER}" \
		-mesg "Uptime: $uptime" \
		-theme ${dir}/${theme}.rasi
}

# Confirmation glyph size follows the focused monitor, as in
# qar-scrotmenu.sh: a quarter of the smaller of half-width and height, with
# 90/270 degree rotations (odd transforms) swapping width and height.
confirm_size() {
	hyprctl monitors -j | jq -r '.[] | select(.focused) | .scale as $s
	    | (if .transform % 2 == 1 then [.height, .width] else [.width, .height] end)
	    | [.[0] / 2, .[1]] | min / $s / 4 | floor' 2>/dev/null
}

# Confirmation CMD
confirm_cmd() {
	local size
	size=$(confirm_size)
	rofi -dmenu \
		-p 'Confirmation' \
		-selected-row 1 \
		-mesg 'Simu Sicuri?' \
		-theme "$HOME/.config/rofi/udt/confirm.rasi" \
		-theme-str "element-text { font: \"Inconsolata Nerd Font Mono ${size:-135}\"; }"
}

# Ask for confirmation
confirm_exit() {
	echo -e "$yes\n$no" | confirm_cmd
}

# Pass variables to rofi dmenu
run_rofi() {
	echo -e "$shutdown\n$reboot\n$sleep\n$logout" | rofi_cmd
}

# Execute Command
run_cmd() {
	selected="$(confirm_exit)"
	if [[ "$selected" == "$yes" ]]; then
		if [[ $1 == '--shutdown' ]]; then
	        notify-send 'system shutdown' 'the system is going to shutdown now'
	        sleep 1
	        /usr/bin/loginctl poweroff
		elif [[ $1 == '--reboot' ]]; then
	        notify-send 'system reboot' 'the system is going to reboot now'
	        sleep 1
	        /usr/bin/loginctl reboot
		elif [[ $1 == '--sleep' ]]; then
	        notify-send 'suspend to ram' 'the system is going to sleep now'
	        sleep 1
	        /usr/bin/loginctl suspend
		elif [[ $1 == '--logout' ]]; then
	        hyprctl dispatch 'hl.dsp.exit()'
		fi
	else
		exit 0
	fi
}

# Actions
chosen="$(run_rofi)"
case ${chosen} in
    $shutdown)
		run_cmd --shutdown
        ;;
    $reboot)
		run_cmd --reboot
        ;;
    $sleep)
        run_cmd --sleep
        ;;
    $logout)
		run_cmd --logout
        ;;
esac