aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMoritz Meißelbach <arbelzapf@gmail.com>2021-01-21 23:31:58 +0100
committerMoritz Meißelbach <arbelzapf@gmail.com>2021-01-21 23:31:58 +0100
commit1ac6d768619aab8e80c63d47e69aa65f27aa47ce (patch)
treef183a7ead12a5f985219e93436ad09f6271b4b59
parent0c6cc8103834f6ac3ec91cbec2e5eae69fd2a9e4 (diff)
downloadsway-launcher-desktop-1ac6d768619aab8e80c63d47e69aa65f27aa47ce.tar.gz
sway-launcher-desktop-1ac6d768619aab8e80c63d47e69aa65f27aa47ce.zip
#31 #27 #28 Allow arbitrary commands
-rw-r--r--README.md1
-rwxr-xr-xsway-launcher-desktop.sh15
2 files changed, 14 insertions, 2 deletions
diff --git a/README.md b/README.md
index 07ef6a5..4b6b655 100644
--- a/README.md
+++ b/README.md
@@ -14,6 +14,7 @@ Despite its name, it does not (read: no longer) depend on the Sway window manage
- Colored output and glyphs for the different entry types
- Entries are lazily piped into fzf eliminating any lag during startup
- Optional support for the XDG Autostart specification
+- Executes arbitrary custom commands (if there are no other matches)
## Installation
diff --git a/sway-launcher-desktop.sh b/sway-launcher-desktop.sh
index 351aaa5..df25e92 100755
--- a/sway-launcher-desktop.sh
+++ b/sway-launcher-desktop.sh
@@ -51,6 +51,7 @@ else
HIST_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/${0##*/}-history.txt"
fi
fi
+PROVIDERS['user']="exit${DEL}exit${DEL}{1}" # Fallback provider that simply executes the exact command if there were no matches
if [[ -n "${HIST_FILE}" ]]; then
mkdir -p "${HIST_FILE%/*}" && touch "$HIST_FILE"
@@ -255,8 +256,9 @@ for PROVIDER_NAME in "${!PROVIDERS[@]}"; do
(bash -c "${0} provide ${PROVIDER_NAME}" >>"$FZFPIPE") &
done
-COMMAND_STR=$(
+readarray -t COMMAND_STR <<<$(
fzf --ansi +s -x -d '\034' --nth ..3 --with-nth 3 \
+ --print-query \
--preview "$0 describe {2} {1}" \
--preview-window=up:2:noborder \
--no-multi --cycle \
@@ -265,10 +267,19 @@ COMMAND_STR=$(
--color='16,gutter:-1' \
<"$FZFPIPE"
) || exit 1
+# Get the last line of the fzf output. If there were no matches, it contains the query which we'll treat as a custom command
+# If there were matches, it contains the selected item
+COMMAND_STR=$(printf '%s\n' "${COMMAND_STR[@]: -1}")
+# We still need to format the query to conform to our fallback provider.
+# We check for the presence of field separator character to determine if we're dealing with a custom command
+if [[ $COMMAND_STR != *$'\034'* ]]; then
+ COMMAND_STR="${COMMAND_STR}"$'\034user\034'"${COMMAND_STR}"$'\034'
+ SKIP_HIST=1 # I chose not to include custom commands in the history. If this is a bad idea, open an issue please
+fi
[ -z "$COMMAND_STR" ] && exit 1
-if [[ -n "${HIST_FILE}" ]]; then
+if [[ -n "${HIST_FILE}" && ! "$SKIP_HIST" ]]; then
# update history
for i in "${!HIST_LINES[@]}"; do
if [[ "${HIST_LINES[i]}" == *" $COMMAND_STR"$'\n' ]]; then