-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi3launcher.sh
executable file
·398 lines (322 loc) · 14.7 KB
/
i3launcher.sh
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
#!/bin/bash
#
# Small launcher based on Rofi. Have to document this but basically it
# creates Rofi menus based on directory structure like this:
#
# ls ~/.i3launcher
# 00- VirtualBox/
# 05-* CPU scaling
# 05-* CPU scaling.dynentry
# 25- Media/
# handler.sh
# ls ~/.i3launcher/00-\ \ VirtualBox
# 00-Win 7
# 05-Win 7 (new)
# 10-Alarm UD
# 15-iTunes
# 20-Linux dummy
# handler.sh
#
# When you choose directory, it will enter it & display rofi based on that
# dirs content. If you choose simple file, it will look up the tree to look
# for handler.sh which will be executed & passed current path as params.
#
# Author: Xoores <whyxoores.cz>
# If we don't disable this, shellcheck will bitch aboud sourced .sh files...
# shellcheck disable=1091
source /scripts/_common.sh
LAUNCHER_ROOT="${HOME}/.i3launcher"
if [ ! -d "${LAUNCHER_ROOT}" ]; then
fLog -p 2 "No launcher root directory found: '${LAUNCHER_ROOT}'"
exit 1
fi
function run_handler()
{
# If selected menu item is a file, it means that we have reached
# the end of our menu journey and it is time to act!
# First thing we need to do is prepare "normalized path", which
# will be passed as parameters to handler of our menu item.
#
# "Normalized path" is basically for giving the handlers means to
# easily distinguish different sub-menus by separating full menu
# path to separate parameters. It strips numerics used to sort
# menu items and every non-ASCII character. Parameter $1 is
# always full and unchanged path! But wait, there's more: the
# parameters passed in ${2} onwards are *relative* to the handler
# so the handler does not have to worry if it is in level 4 or 5
# in the menu itself! It will allways get items in its directory
# as ${2} and first relative submenu items in ${3} etc!
#
# For example: item 00-aa/10-bb/14-cd will be passed to handler as
# $1=00-aa/10-bb/14-cd $2=a $3=bb $4=cd when handler is in 00-aa
# If the handler is in 00-aa/10-bb, it will get the following:
# $1=00-aa/10-bb/14-cd $2=cd
declare -a PARAMS_RAW HANDLER_PARAMS
#while read -r PATH_PART; do
# #[[ "${#PATH_PART}" -eq 0 ]] && continue
# PARAMS_RAW+=( "${PATH_PART}" )
# #echo "NORM> ${PATH_PART}"
#done< <(echo -e "${CHOICE//\/[0-9][0-9]\-/\\n}" | grep -Po "[\x20-\x7F]*")
readarray -s1 -td '' PARAMS_RAW < <(echo "${CHOICE//[![:ascii:]]/}/99-" | \
awk '{ gsub(/\/[0-9]{2}\-[ ]*/, "\0"); print; }')
unset "PARAMS_RAW[-1]";
# Now there are 2 possibilities:
# 1) The menu item itself is a runnable file
# 2) The menu item is a plain file
#
# In (1) the item file is exeuted with normalized parameters (see
# above). In the (2) situation we try to look up for executable
# named handler in menu items directory. If there is none, we try
# again in parent directory and again until we either find a real
# handler (there should be atleast one global!) or we hit our root
# path.
#
# In any case, if we run the file or handler, it will get full path
# as an array of parameters. First parameter is always full and
# unaltered path to the menu item, the rest is full path chopped
# to individual bits (directories) and stripped of unnecessary
# symbols (non-ASCII) and information (e.g. sorting numbers)
if [ -x "${LAUNCHER_ROOT}/${CHOICE}" ]; then
# shellcheck disable=2164,1090
# The menu item can be a symlink as well pointing to some app
# so if that is the case, we just run it and *DONT GIVE IT
# ANY PARAMETERS* as we assume that it is a direct app that
# might get confused by our args!
#
# Also some apps are quite picky and badly written and assume
# that they are run from "their" directory - so we just cd into
# their workdir (link target) and then run them from that point...
if [ -L "${LAUNCHER_ROOT}/${CHOICE}" ]; then
LINK_TARGET="$(readlink "${LAUNCHER_ROOT}/${CHOICE}")"
cd "$(dirname "${LINK_TARGET}")"
./"$(basename "${LINK_TARGET}")" &
else
. "${LAUNCHER_ROOT}/${CHOICE}" "${PARAMS_RAW[@]}" &
fi
return
fi
HANDLER_DIR="${LAUNCHER_ROOT}/$(dirname "${CHOICE}")"
HANDLER_LEVEL=${#PARAMS_RAW[@]}
while [ "${HANDLER_LEVEL}" -ge 0 ] ; do
let HANDLER_LEVEL--;
HANDLER="${HANDLER_DIR}/handler.sh"
#fLog -p 7 ">> Searching for handler: ${HANDLER_DIR}"
HANDLER_PARAMS=( "${PARAMS_RAW[${HANDLER_LEVEL}]}" \
"${HANDLER_PARAMS[@]}" )
# Found executable handler? Then run it!
if [ -x "${HANDLER}" ]; then
echo "Got handler: ${HANDLER}"
# shellcheck disable=1090
. "${HANDLER}" "${CHOICE}" "${HANDLER_PARAMS[@]}"
return
fi
HANDLER_DIR="$(dirname "${HANDLER_DIR}")"
done
fLog -p 3 "No handler found for choice '${CHOICE}'"
return
}
function construct_menu()
{
# Some global menu-related variables...
MENU_COUNT=0 # Menu item counter
MENU_INACTIVES="" # List of "titles" (inactive menu elements)
MENU_ENTRIES="" # List of selectable (active) menu entries
MENU_DYNENTRIES=0 # How many .dynentries we have
# We have to prepare our menu, so we crawl selected directory for necessary
# files (or LAUNCHER_ROOT if nothing else is specified)...
while read -r ENTRY; do
ENTRY_FULLPATH="${LAUNCHER_ROOT}/${1}/${ENTRY}"
# Everything we find is by default a menu entry, which is stripped
# of its sorting number (XX-) and prepended with 2 digit menu counter.
MENU_NUM="$(printf "%02d" "${MENU_COUNT}")"
MENU_ENTRY=" [${MENU_NUM}] ${ENTRY:3}"
# Next we check for dirs (submenus)
if [ -d "${ENTRY_FULLPATH}" ]; then
# If a menu item is a directory, it means it is a submenu so
# we append a nice icon to it... And if we are not in a root
# menu, we make submenus BOLD so they stand out more
[[ ${#1} -ne 0 ]] && MENU_ENTRY="<b>${MENU_ENTRY}</b> "
# TODO: Add color: MENU_ENTRY+=" <span color='#66AbFF'></span>"
MENU_ENTRY+=" "
# If the name of an entry begins with '#' (e.g. XX-#something) it
# means it is a title. Titles are inactive menu items used as
# separators and cannot be used by the user...
elif [[ "${ENTRY}" =~ ^[0-9]{2}-# ]]; then
MENU_ENTRY="--- ${ENTRY:4}"
MENU_INACTIVES+="${MENU_COUNT},"
elif [[ "${ENTRY}" =~ ^[0-9]{2}-\* ]]; then
MENU_ENTRY=" [${MENU_NUM}] ${ENTRY:4}"
fi
# Check if there is a runnable "xxx.dynentry" file, which can be
# used to creating dynamically named entries - basically we append
# the result (50 chars of first line only) to the entry name...
if [ -x "${ENTRY_FULLPATH}.dynentry" ]; then
DYNENTRY=$(bash "${ENTRY_FULLPATH}.dynentry" | head -n1 | head -c50)
MENU_ENTRY+="${DYNENTRY}"
let MENU_DYNENTRIES++
fi
# Now we construct our list of menu entries
[[ ${#MENU_ENTRIES} -ne 0 ]] && MENU_ENTRIES+="\n"
MENU_ENTRIES+="${MENU_ENTRY}"
let MENU_COUNT++;
done< <(find "${LAUNCHER_ROOT}/${1}/" -maxdepth 1 -mindepth 1 -name "[0-9]*" ! -name "*.dynentry" -printf "%f\n" | sort -n)
# If there is a ".prompt" file, we load the first line and trim it to 20
# characters if necessary.
if [ -f "${LAUNCHER_ROOT}/${1}/.prompt" ]; then
# TODO: Maybe we should crawl up the tree if there is no .prompt in
# the current directory and we could use one .prompt file
# for a whole subtree!
MENU_PROMPT=$(head -n1 "${LAUNCHER_ROOT}/${1}/.prompt" | head -c20 )
fi
}
construct_menu "${@}"
MENU_SELECTED="${2}"
# By this point we already have a full menu built, so we need to display it!
while [ "${CHOICE}" == "" ] ; do
#RSP=$(echo -e "${MENU_ENTRIES}" | \
# rofi -dmenu -f -p "${MENU_PROMPT:-"Your choice?"}" \
# -format "d s" -select "${MENU_SELECTED}" -markup-rows -no-click-to-exit \
# -color-normal "#393939, #ffffff, #393939, #268bd2, #ffffff" \
# -color-window "#393939, #268bd2, #268bd2" \
# -u "${MENU_INACTIVES}" -no-custom)
RSP=$(echo -e "${MENU_ENTRIES}" | \
rofi -dmenu -i -f -p "${MENU_PROMPT:-"Your choice?"}" \
-format "d s" -select "${MENU_SELECTED}" -markup-rows -no-click-to-exit \
-theme xoores \
-u "${MENU_INACTIVES}" -no-custom)
RET=$?
SEQ="$(echo "${RSP}" | awk '{print $1}')"
echo "ROFI: RET=${RET} SEQ=${SEQ} RSP='${RSP}'"
# If ROFI returns 1, it means user pressed [ESC] or something similar
if [ ${RET} -eq 1 ]; then
# In that case we check whether we are at the top of the menu. If
# we are on the top, we quit. Otherwise we just go 'up' one step.
if [ ${#1} -eq 0 ] || [ "${1}" == "/" ]; then
echo "Last level, quittin' time!"
else
LAUNCHER_UP="$(dirname "${1}")"
[[ "${LAUNCHER_UP}" == "/" ]] && LAUNCHER_UP=""
UP_MENUITEM="$(basename "${1}")"
UP_MENUITEM="${UP_MENUITEM##[0-9][0-9]\-}"
echo "Shall go one level up: '${LAUNCHER_UP}' to '${UP_MENUITEM}'"
$0 "${LAUNCHER_UP}" "${UP_MENUITEM}"
fi
exit
fi
# Check whether the user has tried to choose the separator (title) or if
# the user just mashed the keyboard and pressed enter... If this is the
# case, we simply display the menu again to enable the user to make a
# valid choice
[[ "${RSP}" == *"---"* ]] || [ "${SEQ}" -eq 0 ] && continue
# If we get to this point, the user made a valid choice. That means that
# we need to get full name of the selected item (remember, we stripped the
# sorting numbers etc). We use index given to us by ROFI which we stored
# in variable ${SEQ}
CHOICE="${1}/$(find "${LAUNCHER_ROOT}/${1}" -maxdepth 1 -mindepth 1 -name "[0-9]*" ! -name "*.dynentry" -printf "%f\n" | sort -n | sed "${SEQ}q;d")"
# Check, whether our choice has "don't close menu" flag
if [[ "${CHOICE}" =~ [0-9]{2,}-\* ]]; then
run_handler
# Reset choice = display the same menu again
CHOICE=""
# Since the entries might have changed, we use just substring.
# TODO: Use actual line number instead of text...
MENU_SELECTED="${RSP:3:8}"
# Check if there are any dynentries and reconstruct the whole menu
# if that's the case
[[ ${MENU_DYNENTRIES} -gt 0 ]] && construct_menu
fi
done
# If we get here, it means that user has made a valid choice and we have
# all the information we need for making things happen!
if [ -d "${LAUNCHER_ROOT}/${CHOICE}" ]; then
# If selected menu item is a directory (== submenu), we re-run
# this again with selected dir as a menu root...
echo "Choice '${CHOICE}' is a SUBMENU!"
$0 "${CHOICE}"
else
run_handler
fi
if false; then
# If selected menu item is a file, it means that we have reached
# the end of our menu journey and it is time to act!
# First thing we need to do is prepare "normalized path", which
# will be passed as parameters to handler of our menu item.
#
# "Normalized path" is basically for giving the handlers means to
# easily distinguish different sub-menus by separating full menu
# path to separate parameters. It strips numerics used to sort
# menu items and every non-ASCII character. Parameter $1 is
# always full and unchanged path! But wait, there's more: the
# parameters passed in ${2} onwards are *relative* to the handler
# so the handler does not have to worry if it is in level 4 or 5
# in the menu itself! It will allways get items in its directory
# as ${2} and first relative submenu items in ${3} etc!
#
# For example: item 00-aa/10-bb/14-cd will be passed to handler as
# $1=00-aa/10-bb/14-cd $2=a $3=bb $4=cd when handler is in 00-aa
# If the handler is in 00-aa/10-bb, it will get the following:
# $1=00-aa/10-bb/14-cd $2=cd
declare -a PARAMS_RAW HANDLER_PARAMS
#while read -r PATH_PART; do
# #[[ "${#PATH_PART}" -eq 0 ]] && continue
# PARAMS_RAW+=( "${PATH_PART}" )
# #echo "NORM> ${PATH_PART}"
#done< <(echo -e "${CHOICE//\/[0-9][0-9]\-/\\n}" | grep -Po "[\x20-\x7F]*")
readarray -s1 -td '' PARAMS_RAW < <(echo "${CHOICE//[![:ascii:]]/}/99-" | \
awk '{ gsub(/\/[0-9]{2}\-[ ]*/, "\0"); print; }')
unset "PARAMS_RAW[-1]";
# Now there are 2 possibilities:
# 1) The menu item itself is a runnable file
# 2) The menu item is a plain file
#
# In (1) the item file is exeuted with normalized parameters (see
# above). In the (2) situation we try to look up for executable
# named handler in menu items directory. If there is none, we try
# again in parent directory and again until we either find a real
# handler (there should be atleast one global!) or we hit our root
# path.
#
# In any case, if we run the file or handler, it will get full path
# as an array of parameters. First parameter is always full and
# unaltered path to the menu item, the rest is full path chopped
# to individual bits (directories) and stripped of unnecessary
# symbols (non-ASCII) and information (e.g. sorting numbers)
if [ -x "${LAUNCHER_ROOT}/${CHOICE}" ]; then
# shellcheck disable=2164,1090
# The menu item can be a symlink as well pointing to some app
# so if that is the case, we just run it and *DONT GIVE IT
# ANY PARAMETERS* as we assume that it is a direct app that
# might get confused by our args!
#
# Also some apps are quite picky and badly written and assume
# that they are run from "their" directory - so we just cd into
# their workdir (link target) and then run them from that point...
if [ -L "${LAUNCHER_ROOT}/${CHOICE}" ]; then
LINK_TARGET="$(readlink "${LAUNCHER_ROOT}/${CHOICE}")"
cd "$(dirname "${LINK_TARGET}")"
./"$(basename "${LINK_TARGET}")" &
else
. "${LAUNCHER_ROOT}/${CHOICE}" "${PARAMS_RAW[@]}" &
fi
exit
fi
HANDLER_DIR="${LAUNCHER_ROOT}/$(dirname "${CHOICE}")"
HANDLER_LEVEL=${#PARAMS_RAW[@]}
while [ "${HANDLER_LEVEL}" -ge 0 ] ; do
let HANDLER_LEVEL--;
HANDLER="${HANDLER_DIR}/handler.sh"
#fLog -p 7 ">> Searching for handler: ${HANDLER_DIR}"
HANDLER_PARAMS=( "${PARAMS_RAW[${HANDLER_LEVEL}]}" \
"${HANDLER_PARAMS[@]}" )
# Found executable handler? Then run it!
if [ -x "${HANDLER}" ]; then
echo "Got handler: ${HANDLER}"
# shellcheck disable=1090
. "${HANDLER}" "${CHOICE}" "${HANDLER_PARAMS[@]}"
exit
fi
HANDLER_DIR="$(dirname "${HANDLER_DIR}")"
done
fLog -p 3 "No handler found for choice '${CHOICE}'"
exit 1;
fi