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
|
#!/usr/bin/env bash
# terminal application launcher for sway, using fzf
# Based on: https://gitlab.com/FlyingWombat/my-scripts/blob/master/sway-launcher
# https://gist.github.com/Biont/40ef59652acf3673520c7a03c9f22d2a
shopt -s nullglob
if [[ "$1" == 'describe' ]]; then
shift
if [[ $2 == 'command' ]]; then
title=$1
readarray arr < <(whatis -l "$1" 2>/dev/null)
description="${arr[0]}"
description="${description%*-}"
else
title=$(sed -ne '/^Name=/{s/^Name=//;p;q}' "$1")
description=$(sed -ne '/^Comment=/{s/^Comment=//;p;q}' "$1")
fi
echo -e "\033[33m$title\033[0m"
echo "${description:-No description}"
exit
fi
# Defaulting terminal to termite, but feel free to either change
# this or override with an environment variable in your sway config
# It would be good to move this to a config file eventually
set "${TERMINAL_COMMAND:="termite -e"}"
HIST_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/${0##*/}-history.txt"
DIRS=(
/usr/share/applications
"$HOME/.local/share/applications"
/usr/local/share/applications
)
GLYPH_COMMAND=" "
GLYPH_DESKTOP=" "
touch "$HIST_FILE"
readarray HIST_LINES <"$HIST_FILE"
FZFPIPE=$(mktemp)
PIDFILE=$(mktemp)
trap 'rm "$FZFPIPE" "$PIDFILE"' EXIT INT
# Append Launcher History, removing usage count
(printf '%s' "${HIST_LINES[@]#* }" >>"$FZFPIPE") &
# Load and append Desktop entries
(
for dir in "${DIRS[@]}"; do
[[ -d "$dir" ]] || continue
awk -v pre="$GLYPH_DESKTOP" -F= '
BEGINFILE{application=0;block="";a=0}
/^\[Desktop Entry\]/{block="entry"}
/^Type=Application/{application=1}
/^\[Desktop Action/{
sub("^\\[Desktop Action ", "");
sub("\\]$", "");
block="action";
a++;
actions[a,"key"]=$0
}
/^Name=/{
if(block=="action") {
actions[a,"name"]=$2;
} else {
name=$2
}
}
ENDFILE{
if (application){
print FILENAME "\034desktop\034\033[33m" pre name "\033[0m";
if (a>0)
for (i=1; i<=a; i++)
print FILENAME "\034desktop\034\033[33m" pre name "\033[0m (" actions[i, "name"] ")\034" actions[i, "key"]
}
}' \
"$dir/"*.desktop </dev/null >>"$FZFPIPE"
# the empty stdin is needed in case no *.desktop files
done
) &
# Load and append command list
(
IFS=:
read -ra path <<<"$PATH"
for dir in "${path[@]}"; do
printf '%s\n' "$dir/"* |
awk -F / -v pre="$GLYPH_COMMAND" '{print $NF "\034command\034\033[31m" pre "\033[0m" $NF;}'
done | sort -u >>"$FZFPIPE"
) &
COMMAND_STR=$(
(
tail -n +0 -f "$FZFPIPE" &
echo $! >"$PIDFILE"
) |
fzf +s -x -d '\034' --nth ..3 --with-nth 3 \
--preview "$0 describe {1} {2}" \
--preview-window=up:3:wrap --ansi
kill -9 "$(<"$PIDFILE")" | tail -n1
) || exit 1
[ -z "$COMMAND_STR" ] && exit 1
# update history
for i in "${!HIST_LINES[@]}"; do
if [[ "${HIST_LINES[i]}" == *" $COMMAND_STR"$'\n' ]]; then
HIST_COUNT=${HIST_LINES[i]%% *}
HIST_LINES[$i]="$((HIST_COUNT + 1)) $COMMAND_STR"$'\n'
match=1
break
fi
done
if ! ((match)); then
HIST_LINES+=("1 $COMMAND_STR"$'\n')
fi
printf '%s' "${HIST_LINES[@]}" | sort -nr >"$HIST_FILE"
command='echo "nope"'
# shellcheck disable=SC2086
readarray -d $'\034' -t PARAMS <<<${COMMAND_STR}
# COMMAND_STR is "<string>\034<type>"
case ${PARAMS[1]} in
desktop)
# Define the search pattern that specifies the block to search for within the .desktop file
PATTERN="^\\\\[Desktop Entry\\\\]"
if [[ -n ${PARAMS[3]} ]]; then
PATTERN="^\\\\[Desktop Action ${PARAMS[3]%?}\\\\]"
fi
# 1. We see a line starting [Desktop, but we're already searching: deactivate search again
# 2. We see the specified pattern: start search
# 3. We see an Exec= line during search: remove field codes and set variable
# 3. We see a Path= line during search: set variable
# 4. Finally, build command line
command=$(awk -v pattern="${PATTERN}" -v terminal_command="${TERMINAL_COMMAND}" -F= '
BEGIN{a=0;exec=0;path=0}
/^\[Desktop/{
if(a){
a=0
}
}
$0 ~ pattern{
a=1
}
/^Terminal=/{
sub("^Terminal=", "");
if ($0 == "true") {
terminal=1
}
}
/^Exec=/{
if(a && !exec){
sub("^Exec=", "");
gsub(" ?%[cDdFfikmNnUuv]", "");
exec=$0;
}
}
/^Path=/{
if(a && !path){
path=$2
}
}
END{
if(path){
printf "cd " path " &&"
}
if (terminal){
printf terminal_command " "
}
print exec
}' "${PARAMS[0]}")
;;
command)
command="${PARAMS[0]}"
;;
esac
swaymsg -t command exec "$command"
|