-
Notifications
You must be signed in to change notification settings - Fork 1
/
launcher
executable file
·71 lines (61 loc) · 1.78 KB
/
launcher
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
#!/bin/sh -e
#
# launcher script for use with a menu program (e.g., dmenu, bemenu,
# fzf, etc.)
#
# launcher [utility [args...]]
#
# The command selected will be passed as an argument to the utility
# provided on this script's command line, e.g., "launcher i3-msg exec"
# to run under i3. If no utility is provided then the selected command
# will be ran directly by this script.
#
# The TERMCMD environment variable must be set in order to spawn a
# terminal for commands, e.g., "xterm -e" for xterm.
#
# The MENU environment variable determines the menu command to be used,
# defaults to "bemenu -i -l 10"
#
: "${TERMCMD:?'$TERMCMD must be set (i.e. xterm -e)'}"
# ensure $LAUNCHER_HISTORY is set and the file exists
: >> "${LAUNCHER_HISTORY:="${HOME:?}/.launcher"}"
cmd=$(sed '1!G;h;$!d' "$LAUNCHER_HISTORY" | ${MENU:-bemenu -i -l 10})
# XXX selecting an entry, Shift+Tab to copy it into the filter, and then
# C-A + Space to prepend it with a space should make it anonymous, but
# bemenu seems to strip the space in this case?
case "$cmd" in
'') exit 0 ;;
' '*) anonymous=1; cmd=${cmd#' '};;
*) unset -v anonymous ;;
esac
case "$cmd" in
[12]' '*) cmdctx=${cmd%%' '*}; cmd=${cmd#[12]' '} ;;
*) unset -v cmdctx ;;
esac
if [ ! "$cmdctx" ]; then
cmdctx=$(
printf '%s\n' 'background' 'terminal' |
${MENU:-bemenu -l 2 -p '' -P "Run '$cmd' in the"}
)
case "$cmdctx" in
background) cmdctx=2 ;;
terminal) cmdctx=1 ;;
*) exit 1 ;;
esac
fi
case "$cmdctx" in
1) terminal=1 ;;
*) unset -v terminal; cmdctx=2 ;;
esac
if [ ! "$anonymous" ]; then
deduped=$(grep -Fxve "$cmdctx $cmd" -e "$cmd" < "$LAUNCHER_HISTORY") || :
cat > "$LAUNCHER_HISTORY" <<-EOF
$deduped
$cmdctx $cmd
EOF
fi
if [ "$*" ]; then
exec "$@" "${terminal:+$TERMCMD }$cmd"
else
eval exec "${terminal:+$TERMCMD }$cmd"
fi