-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmenu-launch
executable file
·424 lines (359 loc) · 12.1 KB
/
dmenu-launch
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/bin/bash
### Dmenu Launcher ################
# Version 0.5.11 by Scott Garrett #
# Wintervenom [(at)] gmail.com #
###################################
# Depends on: dmenu #
# Optional: slmenu, lsx #
###################################
###############
### Globals ###
###############
# Path to XDG shortcut (*.desktop) directories.
xdg_paths=(
"$HOME/.local/share/applications"
/usr/local/share/applications
/usr/share/applications
)
# Path to Dmenu Launcher configuration file.
config="$HOME/.config/dmenu-launch/config"
# Path to Dmenu Launcher cache will be stored.
cache="$HOME/.cache/dmenu-launch.cache"
# Path to history file.
hist="$HOME/.config/dmenu-launch/history"
# Path to lock file.
lock="$HOME/.config/dmenu-launch/lock"
# Dmenu command.
dm="dmenu -i $DMENU_OPTIONS"
if tty &>/dev/null && [[ "$TERM" != 'dumb' ]]; then
if type -p slmenu &>/dev/null; then
dm='slmenu -i'
term_mode=1
fi
elif [[ -z "$DISPLAY" ]]; then
exit 1
fi
# Dmenu prompt strings.
declare -A dm_prompts=(
['execute']='Execute:'
['clearhist']='Clear History?'
['nobinary']='No such binary.'
['whichbin']='Which one?'
['setterm']='Set terminal:'
)
# Choose proper LSX binary.
_using_real_lsx=1
if type -p lsx-suckless >/dev/null; then
# New LSX
lsx=lsx-suckless
elif type -p lsx >/dev/null; then
# Old LSX
lsx=lsx
else
# Use compgen cleverness.
unset _using_real_lsx=1
lsx () { compgen -c -X '_*' | grep -Fv "$(compgen -bk)"; }
fi
# Run in Terminal flag.
flag_terminal='no'
# Show Binaries flag.
flag_binaries='yes'
# Show XDG Names flag.
flag_xdg_names='yes'
# Remember launch history.
flag_history='yes'
# Cache and re-use last generated menu.
flag_cache='yes'
# Location of configuration items.
flag_prefix_opts='no'
# Use first match in the case of multiple binary targets.
flag_first_match='no'
# Terminal to use.
terminal='urxvt'
# Configuration items.
config_menu=('Clear History' 'Run in Terminal' 'Terminal' 'Detach'
'Show Binaries' 'Show XDG Names' 'History' 'Cache'
'Run First Match' 'Options in Front')
_debug=0
#################
### Functions ###
#################
debug () {
[[ $_debug == 1 ]] && printf -- "${FUNCNAME[1]}: $1\n" "${@:2}" >&2
}
set_option () {
value="$2"
# If the caller wanted to flip a "yes" to a "no" or vise versa...
if [[ "$3" == 'flip' ]]; then
[[ "$3" == 'flip' && "$value" == 'yes' ]] && value='no' || value='yes'
fi
debug "Set option '%s' from '%s' to '%s'." "$1" "$2" "$3"
# Create configuration directory if it doesn't exist.
mkdir -p "${config%/*}"
touch "$config"
# Remove the old setting.
sed -i "s:^[[:blank:]]*$1=.*::g" "$config"
# Append the new setting.
echo "$1=\"$value\"" >> "$config"
# Remove blank lines.
sed -i '/^[[:blank:]]*$/d' "$config"
# Reload the configuraton file so it takes effect.
. "$config"
}
list_xdg_shortcuts () {
local path
echo > "${cache}-shortcuts.tmp"
for path in "${xdg_paths[@]}"; do
[[ ! -d "$path" ]] && continue
while read -r file; do
basename=$(basename "$file")
if ! grep -Fix "$basename" "${cache}-shortcuts.tmp"; then
echo "$file"
echo "${file##*/}" >> "${cache}-shortcuts.tmp"
fi
done < <(find -L "$path" -type f -name '*.desktop')
done
rm "${cache}-shortcuts.tmp"
}
build_opt_list () {
echo "[${config_menu[2]}: $terminal]"
echo "[${config_menu[3]}: $flag_detach]"
echo "[${config_menu[6]}: $flag_history]"
echo "[${config_menu[7]}: $flag_cache]"
echo "[${config_menu[5]}: $flag_xdg_names]"
echo "[${config_menu[4]}: $flag_binaries]"
echo "[${config_menu[8]}: $flag_first_match]"
echo "[${config_menu[9]}: $flag_prefix_opts]"
}
build_list () {
debug 'Building executable list.'
# Create cache directory if it doesn't exist.
mkdir -p "${cache%/*}"
# Cache names and executable paths of XDG shortcuts.
if [[ "$flag_xdg_names" == 'yes' ]]; then
while read -r file; do
# Grab the friendly names and paths of the executable.
awk -F'=' '
/^ *Name=/ { name=$2 }
/^ *Exec=/ { exec=$2 }
{
if (name != "" && exec != "") {
print name "\t" exec
name=""
exec=""
}
}
' "$file" 2>/dev/null
done > "$cache" < <(list_xdg_shortcuts)
# Start printing out the menu items, starting with XDG shortcut names...
sed 's/\t.*$//' "$cache" | sort -fu
fi
# ...then, the binary names...
if [[ "$flag_binaries" == 'yes' ]]; then
if [[ $_using_real_lsx ]]; then
(IFS=':'; $lsx $PATH | sort -u)
else
$lsx | sort -u
fi
fi
}
cache_menu () {
debug 'Caching menu to disk.'
touch "${cache}-menu.lock"
build_list > "${cache}-menu.new"
mv "${cache}-menu"{.new,}
rm "${cache}-menu.lock"
}
build_menu () {
debug 'Building menu.'
echo "[${config_menu[1]}: $flag_terminal]"
[[ "$term_mode" ]] &&
echo "[${config_menu[3]}: $flag_detach]"
[[ "$flag_prefix_opts" == 'yes' ]] &&
build_opt_list
if [[ "$flag_cache" == 'yes' ]]; then
[[ ! -f "${cache}-menu" ]] &&
cache_menu
cat "${cache}-menu"
else
build_list
fi
# ...then, the configuration options.
[[ "$flag_prefix_opts" != 'yes' ]] &&
build_opt_list
}
update_history () {
debug "Adding '%s' to history." "$1"
(echo "$1"; head -9 "$hist" | grep -Fvx "$1") > "$hist.new"
mv "$hist"{.new,}
}
build_hist_menu () {
debug 'Combining history with menu items.'
mkdir -p "${hist%/*}"
touch "$hist"
menu_items=$(build_menu)
hist_items=$(grep -Fx "$(echo "$menu_items")" "$hist")
echo "$hist_items" > "$hist" # Keep the history file free of invalids.
# There's probably a better way, but this works, for now.
if [[ ${#hist_items} > 1 ]]; then
[[ "$flag_history" == 'yes' ]] &&
echo "[${config_menu[0]}]"
echo "$hist_items"
echo "$menu_items" | grep -Fvx "$hist_items"
else
echo "$menu_items"
fi
}
program_exists () {
type -p "$1" &>/dev/null
}
launch () {
if [[ "$flag_detach" == 'yes' ]]; then
debug "Launching detached: %s" "$*"
# Shouldn't be set when there won't be a terminal attached anymore...
export TERM='dumb'
# ...and won't be interactive...
unset PS1
"$@" </dev/null &>/dev/null & disown
else
debug "Launching: %s" "$*"
"$@"
fi
}
############
### Main ###
############
# Load configuration
[[ -f "$config" ]] && . "$config"
# Don't run if an instance of dmenu-launch is already running.
if [[ -f "$lock" ]]; then
pid=$(<"$lock")
if [[ $pid ]] && kill -0 "$pid" &>/dev/null; then
debug "A dmenu-launch PID $pid is still running."
exit 1
fi
fi
echo $BASHPID > "$lock"
cache_menu &
# Launch processes in background, detached and disowned from shell.
[[ "$term_mode" ]] && flag_detach='no' || flag_detach='yes'
while :; do
while [[ -f "${cache}-menu.lock" ]]; do
debug "Waiting for menu caching to finish..."
sleep 1
done
# Ask the user to select a program to launch.
if [[ "$flag_history" == 'yes' ]]; then
app=$(build_hist_menu | $dm -p "${dm_prompts['execute']}")
else
app=$(build_menu | $dm -p "${dm_prompts['execute']}")
fi
debug "User selected: $app"
[[ -z "$term_mode" ]] && flag_detach='yes'
[[ "$flag_detach" == 'no' ]] && flag_terminal='no'
case "$app" in
*"[${config_menu[0]}"*) # Clear History
confirm=$(printf '%s\n' '[Yes]' '[No]' |
$dm -p "${dm_prompts['clearhist']}")
[[ -z "$confirm" || "$confirm" == '[No]' ]] && continue
rm -f "$hist"
touch "$hist"
;;
*"[${config_menu[1]}"*) # Run in Terminal
set_option flag_terminal $flag_terminal flip
echo yes
;;
*"[${config_menu[2]}"*) # Set Terminal
new_value=$(echo '[Cancel]' | $dm -p "${dm_prompts['setterm']}")
[[ -z "$new_value" || "$new_value" == '[Cancel]' ]] && continue
if ! type -p "$new_value"; then
echo '[OK]' | $dm -p "${dm_prompts['nobinary']}"
continue
fi
set_option terminal "$new_value"
;;
*"[${config_menu[3]}"*) # Detach
set_option flag_detach $flag_detach flip
;;
*"[${config_menu[4]}"*)
set_option flag_binaries $flag_binaries flip
[[ -f "${cache}-menu" ]] && rm -f "${cache}-menu"
;;
*"[${config_menu[5]}"*)
set_option flag_xdg_names $flag_xdg_names flip
[[ -f "${cache}-menu" ]] && rm -f "${cache}-menu"
;;
*"[${config_menu[6]}"*)
set_option flag_history $flag_history flip
;;
*"[${config_menu[7]}"*)
set_option flag_cache $flag_cache flip
[[ -f "${cache}-menu" ]] && rm -f "${cache}-menu"
;;
*"[${config_menu[8]}"*)
set_option flag_first_match $flag_first_match flip
;;
*"[${config_menu[9]}"*)
set_option flag_prefix_opts $flag_prefix_opts flip
;;
*)
# Quit if nothing was selected.
[[ -z "$app" ]] && exit
selection=$app
# If the selection doesn't exist, see if it's an XDG shortcut.
if ! program_exists $app; then
app=$(grep -F "$app"$'\t' "$cache" | sed 's/.*\t//;s/ %.//g')
debug "Exec line: $app"
if [[ "$flag_first_match" == 'no' ]]; then
# If there's more than one, ask which binary to use.
[[ "$(echo "$app" | wc -l)" != '1' ]] &&
app=$(echo "$app" | $dm -p "${dm_prompts['whichbin']}")
else
IFS=$'\n' read app trash <<< "$app"
fi
[[ -z "$app" ]] && exit
fi
# Check and see if the binary exists, and launch it, if so.
if program_exists $app; then
update_history "$selection"
if [[ $flag_terminal = 'no' ]]; then
launch $app
else
launch $terminal -e $app
fi
exit
else
echo '[OK]' | $dm -p "${dm_prompts['nobinary']}"
fi
;;
esac
done
### Changelog #################################################################
# 0.4.1 + Read DMENU_OPTIONS for colors
# 0.5.0 + Clean-up code
# + Cache XDG items to speed up look-up
# + Added Terminal option.
# 0.5.1 + Fix '=' parse bug
# 0.5.2 + Launch history added
# 0.5.3 + Clear History option added
# 0.5.4 + Don't try to source config file if it is not yet created
# 0.5.5 + Use `slmenu` (if it is installed) when running from a non-dumb
# terminal
# 0.5.6 + Added Detach option in terminal mode
# + Added Cache option: generate menu for the next run in the
# background
# + History optional
# 0.5.7 + Run in Terminal and Detach options move back to front for
# convineince
# + Bug fix: display rest of menu items at first launch
# 0.5.8 + Change $* to "$@"
# 0.5.9 + Bug fix: handle XDG entries with more than one friendly name
# + Bug fix: add binary and XDG options
# + `lsx` is no longer a hard dependency
# + Locking mechanism
# 0.5.10 + Request: configurable prompt strings
# + Toggleable location of options
# 0.5.11 + Request: prioritize XDG shortcuts by order in $xdg_paths.
# + Request: add option to launch first match if multiple targets
# + Recursively scan XDG shortcut directories
# + Bug fix: wait on cache before showing menu