-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinstantmenu_smartrun
More file actions
71 lines (62 loc) · 2.09 KB
/
instantmenu_smartrun
File metadata and controls
71 lines (62 loc) · 2.09 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
#!/bin/bash
# smart application launcher that remembers often used apps
# define $1 to run in terminal
if [ -n "$2" ] || [ -z "$1" ]; then
LIST="$HOME/.cache/instantmenuhist"
RARGS="terminal"
LARGS="desktop"
else
if [ "$1" = "desktop" ]; then
shift 1
i3-dmenu-desktop --dmenu='instantmenu -p desktop -lc "instantmenu_smartrun terminal" -rc "instantmenu_smartrun" -q "search apps" -l 10 -c -i -w -1 -h -1 -bw 4'
exit
fi
LIST="$HOME/.cache/instanttermmenuhist"
RARGS="desktop"
LARGS=""
fi
if [ -n "$2" ]; then
# read history and path
# put in initial text
read -r SELECTION < <({
tac "$LIST"
instantmenu_path | sort -u
} | perl -nE '$seen{$_}++ or print' |
instantmenu -rc "instantmenu_smartrun $RARGS" -lc "instantmenu_smartrun $LARGS" \
-p "${1}" -i -f -q "search apps" -l 10 -c -w -1 -h -1 -bw 4 -it "$2")
else
# read history and path
read -r SELECTION < <({
tac "$LIST"
instantmenu_path | sort -u
} | perl -nE '$seen{$_}++ or print' |
instantmenu -rc "instantmenu_smartrun $RARGS" -lc "instantmenu_smartrun $LARGS" \
-p "${1}" -i -f -q "search apps" -l 10 -c -w -1 -h -1 -bw 4)
fi
# check if the application doesn't exist (indicated by exit code 1 and short runtime)
if [ -z "$SELECTION" ]; then
echo "selection empty, exiting"
exit
fi
a="$(TZ=UTC0 printf '%(%s)T\n' '-1')" ### `-1` is the current time
if [ -z "$1" ]; then
EXITSTATUS=$({
/bin/sh -c "$SELECTION" &>/dev/null && echo "$?"
})
else
EXITSTATUS=$({
~/.config/instantos/default/terminal -e "/bin/sh" -c "$SELECTION" &>/dev/null && echo "$?"
})
fi
elapsedseconds=$(($(TZ=UTC0 printf '%(%s)T\n' '-1') - a))
if [ "$elapsedseconds" -gt 5 ] || [ "$EXITSTATUS" = 0 ]; then
[ -e "$HOME/.cache" ] || mkdir "$HOME/.cache"
echo "$SELECTION" | tee -a "$LIST"
else
echo "error in running $SELECTION: skipping history"
fi
# max history size
if [ "$(wc -l "$LIST" | grep -o '^[^ ]*')" -gt 1300 ]; then
head -1000 <"$LIST" | tee "${LIST}2"
cat "${LIST}2" >"$LIST"
fi