-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmenu-menu
More file actions
executable file
·179 lines (155 loc) · 4.28 KB
/
dmenu-menu
File metadata and controls
executable file
·179 lines (155 loc) · 4.28 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
#!/bin/bash
#
# 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: present a menu based on commands in a config file
#
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
# Documentation {{{1
#
VERSION='1.0'
print_help() (
[ -n "${1-}" ] && printf "%s\n" "$1"
cat <<END
Usage: $0 [OPTION] [INTERFACE]
Present a menu based on commands in a config file
-h display menu horizontally instead of vertically
-s sort menu options
-h display this help and exit
-v output version information and exit
Menu file format:
[Commands]
cmd1 = <commands>
cmd2 = <commands>
[Config]
prompt = "prompt string"
END
) >&2
print_version() (
cat <<END
$0 $VERSION
Copyright (C) 2021 Orion Arts
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jeremy Brubaker.
END
) >&2
# Process options {{{1
#
MENUDIR="${XDG_CONFIG_DIR:-$HOME/.config}/dmenu-menu"
layout=v
sort=
case ${1-} in
-h | --help) print_help; exit 0 ;;
--version) print_version; exit 0 ;;
-d | --menu-dir)
if [ $# -gt 2 ]; then
shift
MENUDIR=$1
shift
else
print_help
exit 1
fi
;;
-H | --horizontal) layout=h; shift ;;
-s | --sort) sort=y; shift ;;
-*) print_help "Invalid option: $1"; exit 1 ;;
'') printf "%s: No menu file given.\n" "$0"; exit 1 ;;
esac
if [ -r "$1" ]; then
MENUFILE="$1"
elif [ -r "$MENUDIR/$1" ]; then
MENUFILE="$MENUDIR/$1"
elif [ -r "$MENUDIR/$1.ini" ]; then
MENUFILE="$MENUDIR/$1.ini"
else
printf "%s: %s not found.\n" "$0" "$1"
exit 1
fi
shift
# Get menu list and configure options {{{1
#
section=
choices=
lineno=0
declare -A COMMANDS
while read -r line; do
lineno=$((lineno + 1))
# Which section are we in?
case ${line^^} in
\#* | '') continue ;;
\[CONFIG\]) section='cfg'; continue ;;
\[COMMANDS\]) section='cmd'; continue ;;
esac
# Split fields and strip leading/trailing whitespace
key=$(echo "$line" | cut -d= -f1 | sed 's/^\s*\(.*\)\s*$/\1/')
val=$(echo "$line" | cut -d= -f2- | sed 's/^\s*\(.*\)\s*$/\1/')
# Strip surrounding quotes
# BUG: Does not handle having words at start and end separately quoted
# BUG: such as 'foo' blah "bar" or "foo" blah "bar"
case "$val" in
\'*\' | \"*\")
val=$(echo "$val" | sed -e 's/^"\(.*\)"$/\1/' -e "s/^'\(.*\)'$/\1/")
;;
\'*\" | \"*\')
printf "Ignoring quoting error on line %d (%s)\n" "$lineno" "$line" >&2
continue
;;
esac
case $section in
cmd)
choices="$choices$key\n"
COMMANDS[$key]="$val"
;;
cfg)
case "${key^^}" in
PROMPT) prompt=$val ;;
esac
;;
esac
done < "$MENUFILE"
# Main {{{1
#
case $layout in
v) layout="-c -l ${#COMMANDS[*]}" ;;
h) layout= ;;
*) printf "%s: layout '%s' is invalid.\n" "$0" "$layout"; exit 1 ;;
esac
case $sort in
y) choices=$(echo -e "$choices" | sort) ;;
esac
choice=$(echo -e "$choices" | dmenu $(make_dmenu_args) -i -p "${prompt-}" $layout "$@")
if [ -n "$choice" ]; then
${COMMANDS[$choice]}
fi