desktop-playbook/files/bin/launcher.sh

36 lines
1.1 KiB
Bash
Executable file

#!/bin/bash
# terminal application launcher for sway, using fzf
# inspired by https://github.com/Biont/sway-launcher-desktop
set -o pipefail
shopt -s nullglob globstar
HIST_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/sway-launcher-history.txt"
export FZF_DEFAULT_OPTS="--layout=reverse --no-extended --print-query --no-sort"
[ ! -f "$HIST_FILE" ] && touch {"$HIST_FILE"
# Create fifo for populate fzf in async mode
FZFPIPE=$(mktemp -u)
mkfifo "$FZFPIPE"
trap 'rm "$FZFPIPE"' EXIT INT
#retrieve history and pipe it into our fifo
readarray HIST_LINES <"$HIST_FILE"
(printf '%s' "${HIST_LINES[@]#* }" >>"$FZFPIPE") &
# Get command, if empty exit
command=$(fzf <"$FZFPIPE" | tail -n1 || exit 1)
[[ "$command" == "" ]] && exit 0
# Update history
index=$(printf '%s' "${HIST_LINES[@]}" | grep -n -Pe "^[0-9]+ ${command}$" | awk -F ':' '{print $1-1}')
if [ -n "$index" ]; then
occurences=${HIST_LINES[$index]%% *}
occurences=$((occurences + 1))
HIST_LINES[$index]="${occurences} ${command}"$'\n'
else
HIST_LINES+=("1 $command"$'\n')
fi
printf '%s' "${HIST_LINES[@]}" | sort --numeric-sort --reverse > "$HIST_FILE"
swaymsg -t command exec "$command"
exit 0