all repos

dotfiles @ f8ffdbda2e0dbb8e3cd065e9dfca7a904eaa62d3

i use rach linux btw

dotfiles/config/rofi/rofi-power-menu (view raw)

1
#!/usr/bin/env bash
2
3
# This script defines just a mode for rofi instead of being a self-contained
4
# executable that launches rofi by itself. This makes it more flexible than
5
# running rofi inside this script as now the user can call rofi as one pleases.
6
# For instance:
7
#
8
#   rofi -show powermenu -modi powermenu:./rofi-power-menu
9
#
10
# See README.md for more information.
11
12
set -e
13
set -u
14
15
# All supported choices
16
all=(shutdown reboot suspend hibernate logout lockscreen)
17
18
# By default, show all (i.e., just copy the array)
19
show=("${all[@]}")
20
21
declare -A texts
22
texts[lockscreen]="lock screen"
23
texts[switchuser]="switch user"
24
texts[logout]="log out"
25
texts[suspend]="suspend"
26
texts[hibernate]="hibernate"
27
texts[reboot]="reboot"
28
texts[shutdown]="shut down"
29
30
declare -A icons
31
icons[lockscreen]="\uf023"
32
icons[switchuser]="\uf518"
33
icons[logout]="\uf842"
34
icons[suspend]="\uf9b1"
35
icons[hibernate]="\uf7c9"
36
icons[reboot]="\ufc07"
37
icons[shutdown]="\uf011"
38
icons[cancel]="\u00d7"
39
40
declare -A actions
41
actions[lockscreen]="loginctl lock-session $XDG_SESSION_ID"
42
#actions[switchuser]="???"
43
actions[logout]="loginctl terminate-session $XDG_SESSION_ID"
44
actions[suspend]="systemctl suspend"
45
actions[hibernate]="systemctl hibernate"
46
actions[reboot]="systemctl reboot"
47
actions[shutdown]="systemctl poweroff"
48
49
# By default, ask for confirmation for actions that are irreversible
50
confirmations=(reboot shutdown logout)
51
52
# By default, no dry run
53
dryrun=false
54
showsymbols=true
55
56
function check_valid {
57
    option="$1"
58
    shift 1
59
    for entry in "${@}"
60
    do
61
        if [ -z "${actions[$entry]+x}" ]
62
        then
63
            echo "Invalid choice in $1: $entry" >&2
64
            exit 1
65
        fi
66
    done
67
}
68
69
# Parse command-line options
70
parsed=$(getopt --options=h --longoptions=help,dry-run,confirm:,choices:,choose:,symbols,no-symbols --name "$0" -- "$@")
71
if [ $? -ne 0 ]; then
72
    echo 'Terminating...' >&2
73
    exit 1
74
fi
75
eval set -- "$parsed"
76
unset parsed
77
while true; do
78
    case "$1" in
79
        "-h"|"--help")
80
            echo "rofi-power-menu - a power menu mode for Rofi"
81
            echo
82
            echo "Usage: rofi-power-menu [--choices CHOICES] [--confirm CHOICES]"
83
            echo "                       [--choose CHOICE] [--dry-run] [--symbols|--no-symbols]"
84
            echo
85
            echo "Use with Rofi in script mode. For instance, to ask for shutdown or reboot:"
86
            echo
87
            echo "  rofi -show menu -modi \"menu:rofi-power-menu --choices=shutdown/reboot\""
88
            echo
89
            echo "Available options:"
90
            echo "  --dry-run          Don't perform the selected action but print it to stderr."
91
            echo "  --choices CHOICES  Show only the selected choices in the given order. Use / "
92
            echo "                     as the separator. Available choices are lockscreen, logout,"
93
            echo "                     suspend, hibernate, reboot and shutdown. By default, all"
94
            echo "                     available choices are shown."
95
            echo "  --confirm CHOICES  Require confirmation for the gives choices only. Use / as"
96
            echo "                     the separator. Available choices are lockscreen, logout,"
97
            echo "                     suspend, hibernate, reboot and shutdown. By default, only"
98
            echo "                     irreversible actions logout, reboot and shutdown require"
99
            echo "                     confirmation."
100
            echo "  --choose CHOICE    Preselect the given choice and only ask for a confirmation"
101
            echo "                     (if confirmation is set to be requested). It is strongly"
102
            echo "                     recommended to combine this option with --confirm=CHOICE"
103
            echo "                     if the choice wouldn't require confirmation by default."
104
            echo "                     Available choices are lockscreen, logout, suspend,"
105
            echo "                     hibernate, reboot and shutdown."
106
            echo "  --[no-]symbols     Show Unicode symbols or not. Requires a font with support"
107
            echo "                     for the symbols. Use, for instance, fonts from the"
108
            echo "                     Nerdfonts collection. By default, they are shown"
109
            echo "  -h,--help          Show this help text."
110
            exit 0
111
            ;;
112
        "--dry-run")
113
            dryrun=true
114
            shift 1
115
            ;;
116
        "--confirm")
117
            IFS='/' read -ra confirmations <<< "$2"
118
            check_valid "$1" "${confirmations[@]}"
119
            shift 2
120
            ;;
121
        "--choices")
122
            IFS='/' read -ra show <<< "$2"
123
            check_valid "$1" "${show[@]}"
124
            shift 2
125
            ;;
126
        "--choose")
127
            # Check that the choice is valid
128
            check_valid "$1" "$2"
129
            selectionID="$2"
130
            shift 2
131
            ;;
132
        "--symbols")
133
            showsymbols=true
134
            shift 1
135
            ;;
136
        "--no-symbols")
137
            showsymbols=false
138
            shift 1
139
            ;;
140
        "--")
141
            shift
142
            break
143
            ;;
144
        *)
145
            echo "Internal error" >&2
146
            exit 1
147
            ;;
148
    esac
149
done
150
151
# Define the messages after parsing the CLI options so that it is possible to
152
# configure them in the future.
153
154
function write_message {
155
    icon="<span font_size=\"medium\">$1</span>"
156
    text="<span font_size=\"medium\">$2</span>"
157
    if [ "$showsymbols" = "true" ]
158
    then
159
        echo -n "\u200e$icon \u2068$text\u2069"
160
    else
161
        echo -n "$text"
162
    fi
163
}
164
165
function print_selection {
166
    echo -e "$1" | $(read -r -d '' entry; echo "echo $entry")
167
}
168
169
declare -A messages
170
declare -A confirmationMessages
171
for entry in "${all[@]}"
172
do
173
    messages[$entry]=$(write_message "${icons[$entry]}" "${texts[$entry]^}")
174
done
175
for entry in "${all[@]}"
176
do
177
    confirmationMessages[$entry]=$(write_message "${icons[$entry]}" "Yes, ${texts[$entry]}")
178
done
179
confirmationMessages[cancel]=$(write_message "${icons[cancel]}" "No, cancel")
180
181
if [ $# -gt 0 ]
182
then
183
    # If arguments given, use those as the selection
184
    selection="${@}"
185
else
186
    # Otherwise, use the CLI passed choice if given
187
    if [ -n "${selectionID+x}" ]
188
    then
189
        selection="${messages[$selectionID]}"
190
    fi
191
fi
192
193
# Don't allow custom entries
194
echo -e "\0no-custom\x1ftrue"
195
# Use markup
196
echo -e "\0markup-rows\x1ftrue"
197
198
if [ -z "${selection+x}" ]
199
then
200
    echo -e "\0prompt\x1fPower menu"
201
    for entry in "${show[@]}"
202
    do
203
        echo -e "${messages[$entry]}\0icon\x1f${icons[$entry]}"
204
    done
205
else
206
    for entry in "${show[@]}"
207
    do
208
        if [ "$selection" = "$(print_selection "${messages[$entry]}")" ]
209
        then
210
            # Check if the selected entry is listed in confirmation requirements
211
            for confirmation in "${confirmations[@]}"
212
            do
213
                if [ "$entry" = "$confirmation" ]
214
                then
215
                    # Ask for confirmation
216
                    echo -e "\0prompt\x1fAre you sure"
217
                    echo -e "${confirmationMessages[$entry]}\0icon\x1f${icons[$entry]}"
218
                    echo -e "${confirmationMessages[cancel]}\0icon\x1f${icons[cancel]}"
219
                    exit 0
220
                fi
221
            done
222
            # If not, then no confirmation is required, so mark confirmed
223
            selection=$(print_selection "${confirmationMessages[$entry]}")
224
        fi
225
        if [ "$selection" = "$(print_selection "${confirmationMessages[$entry]}")" ]
226
        then
227
            if [ $dryrun = true ]
228
            then
229
                # Tell what would have been done
230
                echo "Selected: $entry" >&2
231
            else
232
                # Perform the action
233
                ${actions[$entry]}
234
            fi
235
            exit 0
236
        fi
237
        if [ "$selection" = "$(print_selection "${confirmationMessages[cancel]}")" ]
238
        then
239
            # Do nothing
240
            exit 0
241
        fi
242
    done
243
    # The selection didn't match anything, so raise an error
244
    echo "Invalid selection: $selection" >&2
245
    exit 1
246
fi