-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsway-launcher.sh
executable file
·59 lines (49 loc) · 1.73 KB
/
sway-launcher.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
#!/bin/sh
# terminal application launcher for sway, using fzf
# original command:
# Based on: https://github.com/swaywm/sway/issues/1367
# bindsym $altkey+space exec termite --name=launcher -e \
# "bash -c 'compgen -c | sort -u | fzf --no-extended --print-query | \
# tail -n1 | xargs -r swaymsg -t command exec'"
HIST_FILE="$HOME/.cache/sway-launcher-history.txt"
# Get shell command list
# This may include the occasional non-executable file
command_list=$({ IFS=:; ls -H $PATH; } | grep -v '/.*' | sort -u)
# read existing command history
if [ -f "$HIST_FILE" ]; then
command_history=$(cat "$HIST_FILE")
else
command_history=""
fi
# search command list
command_str=$(printf "%s\n" "${command_history}" "${command_list}" | \
sed -E 's/^[0-9]+ (.+)$/\1/' | \
fzf --no-extended --print-query --no-sort | \
tail -n1) || exit 1
if [ "$command_str" = "" ]; then
exit 1
fi
# echo "Command: $command_str"
# get full line from history (with count number)
hist_line=$(echo "$command_history" | grep --fixed-strings "$command_str")
# echo "Hist Line: $hist_line"
if [ "$hist_line" = "" ]; then
hist_count=1
else
# Increment usage count
hist_count=$(echo "$hist_line" | sed -E 's/^([0-9]+) .+$/\1/')
hist_count=$((hist_count + 1))
# delete line, to add updated later
# echo "Hist Before: $command_history"
command_history=$(echo "$command_history" | \
grep --fixed-strings --invert-match "$command_str")
# echo "Hist After: $command_history"
fi
# update history
update_line="${hist_count} ${command_str}"
printf "%s\n" "${update_line}" "${command_history}" | \
sort --numeric-sort --reverse > "$HIST_FILE"
# echo "$update_line"
# execute command
echo "$command_str"
swaymsg -t command exec "$command_str"