-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmenu-run
executable file
·87 lines (77 loc) · 2.38 KB
/
dmenu-run
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
#!/bin/sh
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2021 Jeremy Brubaker <jbru362@gmail.com>
#
# abstract: use dmenu as a run dialog
#
# Requires 'my terimnal' to print terminal command
#
set -u
LIBPATH="/usr/lib/dmenu:/usr/local/lib/dmenu:$HOME/lib/dmenu:$(dirname $0)"
oldifs="$IFS"
IFS=:
for d in $LIBPATH; do
if [ -r "$d/routines.sh" ]; then
dmenulib="$d/routines.sh"
break
fi
done
IFS="$oldifs"
if [ -n "${dmenulib-}" ]; then
source "$dmenulib"
else
printf "dmenu shell library not found\n" >&2
exit 1
fi
term_cmd=$(my -v terminal)
aliasfile="$HOME/.alias"
functionfile="$HOME/.functions"
cachedir="${XDG_CACHE_HOME:-"$HOME/.cache"}/dmenu"
if ! [ -d "$cachedir" ]; then
mkdir -p "$cachedir"
fi
cachefile="$cachedir/run"
# Instead of making this an interactive script, just
# source the files we need
test -r "$aliasfile" && source "$aliasfile"
test -r "$functionfile" && source "$functionfile"
(compgen -a; compgen -c | grep -vxF -- "$(compgen -a)") \
| sort | grep -v "^[[:punct:]]" | uniq > "$cachefile"
# Cannot quote dmenu args
# shellcheck disable=SC2046
cmdline=$(dmenu $(make_dmenu_args) "$@" < "$cachefile")
[ -z "$cmdline" ] && exit
cmd=$(echo "$cmdline" | cut -d' ' -f1)
[ -z "$cmd" ] && exit
if grep "${cmd/;/}" "$cachefile" >/dev/null 2>&1; then
# Trailing ';' means run it in a terminal
case "$cmdline" in
*\;)
# BUG: does not work with gnome-terminal unless -e is replaced by --
$term_cmd -e bash -ic "
__t=\$(date +%s)
$cmdline
test \$((\$(date +%s) - __t)) -lt 1 &&
read -srn1 -p 'Press any key...'
"
;;
*)
eval $cmdline &
;;
esac
else
exec "$0" "$@" -p "${cmd/;/} not found:"
fi