-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrofi-session
More file actions
executable file
·329 lines (275 loc) · 7.36 KB
/
rofi-session
File metadata and controls
executable file
·329 lines (275 loc) · 7.36 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env bash
# SPDX-License-Identifer: MIT
#
# rofi-session - a Session menu for Rofi
#
# homepage: https://github.com/nullxception/dotfiles
# based on: https://github.com/jluttine/rofi-power-menu
#
rf_item() {
local text="$2"
local item="$text"
item="\u200e"
if [ "$label" = "true" ]; then
item+="$text"
fi
printf "$item"
}
rf_opt() {
local prop=''
for i in "$@"; do
if [ -z "$prop" ]; then
prop+="\0$i"
else
prop+="\x1f$i"
fi
done
printf "$prop\n"
}
get_icon() {
case "$icon" in
"font")
color="${icon_color:-white}"
printf "<span color=\"$color\">${icon[$1]#*, }</span>"
;;
"true")
printf "${icon[$1]%,*}"
;;
*)
printf ""
;;
esac
}
default_choices() {
rf_opt prompt "Session Menu"
for i in "${displayed_choices[@]}"; do
i="${i%+*}"
rf_item "$(get_icon $i)" "${label[$i]}"
rf_opt icon "$(get_icon $i)" info "action:$i"
done
exit 0
}
prompt_choice() {
local choice="$1"
for i in "${need_prompt[@]}"; do
if [[ "$choice" == "$i" ]]; then
rf_opt prompt "Are you sure ?"
rf_item "$(get_icon $choice)" "Yes, ${label[$choice]}"
rf_opt icon "$(get_icon $choice)" info "prompt:$choice"
rf_item "$(get_icon back)" "Go back"
rf_opt icon "$(get_icon back)" info "prompt:back"
exit 0
else
act="prompt:$choice"
fi
done
}
dispatch_action() {
local key="${1#*:}"
if [[ "$key" == "back" ]]; then
default_choices
else
eval "${action[$key]}" &
disown
exit 0
fi
}
proceed_choices() {
for i in "${displayed_choices[@]}"; do
i="${i%+*}"
if [[ $act == "action:$i" ]]; then
prompt_choice "$i"
fi
if [[ "$act" == "prompt:"* ]]; then
dispatch_action "$act"
fi
done
}
gen_config() {
local conf="$1"
mkdir -p "$(dirname "$(realpath "$conf")")"
printf -v list_choices '%s:' "${displayed_choices[@]}"
cat <<EOF >"$conf"
#
# rofi-session configuration
#
# icon=$icon
# icon_color="${icon_color:-#ffffff}" # will only be used if icon=font
# choices=${list_choices%:}
# label=$label
# theme=/path/to/theme.rasi
EOF
}
usage() {
local name="$(basename "$0")"
cat <<EOF
$name - a Session menu for Rofi
Usage:
$name [options] -- [rofi args]
rofi -show foo -modi "foo:$name [options]"
Example:
$name -l false -c logout+o:shutdown+s -- -theme ~/.config/rofi/theme/cherry.rasi
or use as rofi modi:
rofi -show foo -modi "foo:$name -l false -c logout+o:shutdown+s" -theme ~/.config/rofi/theme/cherry.rasi
Available options:
-c, --choices CHOICES Filter choices to display, use ":" as the separator, and +<key> to assign
a key to a choice.
Available choices:
lockscreen+l:logout+o:suspend+x:hibernate+z:reboot+r:shutdown+s
By default, all available choices will be shown.
-i, --icon true|font|false Enable or disable icon for the choices.
Use --icon=font if you want to use a symbols from
your font instead of desktop icon pack (requires
nerd-patched font).
--icon-color "<color>" Tint icon symbol with pango-compatible color format.
note that this option only support tinting icon=font
-l, --label true|false Enable or disable label for the choices.
-h,--help Show this help text.
Configuration
$name will source variables from $sessionconf if exists.
Valid variables are :
icon=true|font|false
icon_color="#ffffff" # will only be used when icon=font
choices=CHOICES
label=true|false
theme=/path/to/theme.rasi
For "theme" variable, it'll only be used when -m or --as-modi is not used.
EOF
}
build_session_args() {
local args=""
if [ -n "$choices" ]; then
args+=" --choices=$choices"
fi
if [ -n "$icon" ]; then
args+=" --icon=$icon"
fi
if [ -n "$label" ]; then
args+=" --label=$label"
fi
echo "$args"
}
build_rofi_args() {
local args=""
local skip=0
for arg in $@; do
if [ $skip -gt 0 ]; then
skip=$((skip - 1))
continue
fi
case $arg in
-show | -modi)
skip=1
;;
*)
args+=" $arg"
;;
esac
done
if [ -f "$theme" ]; then
args+=" -theme $theme"
fi
if [ -n "$choices" ]; then
IFS=':' read -ra kbc <<<"$choices"
cindex=1
for choice in "${kbc[@]}"; do
kbd=${choice#*+}
if [ -n "$kbd" ]; then
args+=" -kb-select-$cindex $kbd"
fi
cindex=$((cindex + 1))
done
fi
echo $args
}
declare -A label
label[lockscreen]="Lock screen"
label[logout]="Logout"
label[suspend]="Suspend"
label[hibernate]="Hibernate"
label[reboot]="Reboot"
label[shutdown]="Shut down"
declare -A icon
icon[lockscreen]="system-lock-screen-symbolic, "
icon[logout]="system-log-out-symbolic, "
icon[suspend]="system-suspend-symbolic, "
icon[hibernate]="system-hibernate-symbolic, "
icon[reboot]="system-reboot-symbolic, "
icon[shutdown]="system-shutdown-symbolic, "
icon[back]="go-previous-symbolic, "
declare -A action
action[lockscreen]="loginctl lock-session ${XDG_SESSION_ID-}"
action[logout]="loginctl kill-session ${XDG_SESSION_ID-}"
action[suspend]="systemctl suspend"
action[hibernate]="systemctl hibernate"
action[reboot]="systemctl reboot"
action[shutdown]="systemctl poweroff"
# Supported choices, use --choices instead if you want to filters it
displayed_choices=(lockscreen+l logout+o suspend+x hibernate+z reboot+r shutdown+s)
need_prompt=(logout reboot shutdown)
icon=true
label=true
parsed=$(getopt --options=hsc:i:l: --longoptions=help,show,choices:,icon:,label:,config:,icon-color: --name "$0" -- "$@")
if [ $? -ne 0 ]; then
echo 'Invalid argument, exiting.' >&2
exit 1
fi
eval set -- "$parsed"
unset parsed
while true; do
case "$1" in
-h | --help)
usage
exit
;;
-c | --choices)
arg_choices="$2"
shift 2
;;
-i | --icon)
arg_icon="$2"
shift 2
;;
--icon-color)
arg_icon_color="$2"
shift 2
;;
-l | --label)
arg_label="$2"
shift 2
;;
--config)
arg_conf="$2"
shift 2
;;
--)
shift
break
;;
esac
done
confdir="${XDG_CONFIG_HOME:-$HOME/.config}/rofi"
sessionconf="${arg_conf:-${confdir}/rofi-session.conf}"
if [ ! -f "${sessionconf}" ]; then
gen_config "$sessionconf"
fi
. $sessionconf
choices="${arg_choices:-$choices}"
label="${arg_label:-$label}"
icon="${arg_icon:-$icon}"
icon_color="${arg_icon_color:-$icon_color}"
if [ -z "$ROFI_OUTSIDE" ]; then
rofi -show session -modi "session:$0 $(build_session_args)" $(build_rofi_args $@)
exit
fi
act="$ROFI_INFO"
rf_opt no-custom true
rf_opt markup-rows true
if [ -n "$choices" ]; then
IFS=':' read -ra displayed_choices <<<"$choices"
fi
if [ -z "$act" ]; then
default_choices
else
proceed_choices
fi