aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMoritz Meißelbach <m.meisselbach@inpsyde.com>2019-11-29 21:28:24 +0100
committerMoritz Meißelbach <m.meisselbach@inpsyde.com>2019-11-29 21:28:24 +0100
commit3835562c5325c279809bb41c34c6afca6288a1cb (patch)
tree7bbf1910d43d9a512146bc5a4cb11842e66bf0d3
parent22182d980a2ebde89fe2970e11c39fbf8e4d6628 (diff)
downloadsway-launcher-desktop-3835562c5325c279809bb41c34c6afca6288a1cb.tar.gz
sway-launcher-desktop-3835562c5325c279809bb41c34c6afca6288a1cb.zip
[WIP] Read custom providers from a ~/.config/sway-launcher-desktop/providers.conf file.
-rwxr-xr-xsway-launcher-desktop.sh74
-rw-r--r--tests/data/config/0/sway-launcher-desktop/providers.conf7
-rw-r--r--tests/describe.bats6
-rw-r--r--tests/providers.bats36
4 files changed, 103 insertions, 20 deletions
diff --git a/sway-launcher-desktop.sh b/sway-launcher-desktop.sh
index 4b21c7d..3f00794 100755
--- a/sway-launcher-desktop.sh
+++ b/sway-launcher-desktop.sh
@@ -16,13 +16,44 @@ TERMINAL_COMMAND="${TERMINAL_COMMAND:="urxvt -e"}"
GLYPH_COMMAND=" "
GLYPH_DESKTOP=" "
HIST_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/${0##*/}-history.txt"
+CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/sway-launcher-desktop"
# Provider config entries are separated by the field separator \034 and have the following structure:
-# list_cmd,launch_cmd
+# list_cmd,preview_cmd,launch_cmd
declare -A PROVIDERS
-PROVIDERS['desktop']="${0} list-entries${DEL}${0} generate-command"
-PROVIDERS['command']="${0} list-commands${DEL}${0} command-line"
+if [ -f "${CONFIG_DIR}/providers.conf" ]; then
+ PARSED=$( awk -F= '
+ BEGINFILE{ provider=""; }
+ /^\[.*\]/{sub("^\\[", "");sub("\\]$", "");provider=$0}
+ /^(launch|list|preview)_cmd/{st = index($0,"=");providers[provider][$1] = substr($0,st+1)}
+ ENDFILE{
+ for (key in providers){
+ if(!("list_cmd" in providers[key])){continue;}
+ if(!("launch_cmd" in providers[key])){continue;}
+ if(!("preview_cmd" in providers[key])){continue;}
+ for (entry in providers[key]){
+ # gsub(/\$/,"\\$", providers[key][entry])
+ # gsub(/"/,"\\\"", providers[key][entry])
+ # gsub("\047",sq, providers[key][entry])
+ # gsub("\x27",sq, providers[key][entry])
+ # gsub(/\047/,sq, providers[key][entry])
+ gsub(/[\x27,\047]/,"\x27\"\x27\"\x27", providers[key][entry])
+ # gsub(/\047/,"DURR", providers[key][entry])
+ }
+ print "PROVIDERS[\x27" key "\x27]=\x27" providers[key]["list_cmd"] "\034" providers[key]["preview_cmd"] "\034" providers[key]["launch_cmd"] "\x27\n"
+ }
+ }' "${CONFIG_DIR}/providers.conf")
+ echo "$PARSED"
+ eval "$PARSED"
+else
+ PROVIDERS['desktop']="${0} list-entries${DEL}${0} describe-desktop${DEL}${0} generate-command"
+ PROVIDERS['command']="${0} list-commands${DEL}${0} describe-command${DEL}${0} command-line"
+fi
+#for i in "${!PROVIDERS[@]}"; do
+# echo "${i}=${PROVIDERS[$i]}"
+#done
+#exit
touch "$HIST_FILE"
readarray HIST_LINES <"$HIST_FILE"
@@ -30,20 +61,27 @@ function command-line() {
echo "${TERMINAL_COMMAND} ${1}"
}
function describe() {
- 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
+ # shellcheck disable=SC2086
+ readarray -d ${DEL} -t PROVIDER_ARGS <<<${PROVIDERS[${1}]}
+ # shellcheck disable=SC2086
+ [ -n "${PROVIDER_ARGS[1]}" ] && eval "${PROVIDER_ARGS[1]} ${2}"
+}
+function describe-desktop() {
+ description=$(sed -ne '/^Comment=/{s/^Comment=//;p;q}' "$1")
+ echo -e "\033[33m$(sed -ne '/^Name=/{s/^Name=//;p;q}' "$1")\033[0m"
+ echo "${description:-No description}"
+}
+function describe-command() {
+ title=$1
+ readarray arr < <(whatis -l "$1" 2>/dev/null)
+ description="${arr[0]}"
+ description="${description%*-}"
echo -e "\033[33m$title\033[0m"
echo "${description:-No description}"
}
function provide() {
+ # shellcheck disable=SC2086
readarray -d ${DEL} -t PROVIDER_ARGS <<<${PROVIDERS[$1]}
eval "${PROVIDER_ARGS[0]}"
}
@@ -128,7 +166,7 @@ function generate-command() {
# 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
- awk -v pattern="${PATTERN}" -v terminal_command="${TERMINAL_COMMAND}" -F= '
+ awk -v pattern="${PATTERN}" -v terminal_cmd="${TERMINAL_COMMAND}" -F= '
BEGIN{a=0;exec=0;path=0}
/^\[Desktop/{
if(a){
@@ -162,14 +200,14 @@ function generate-command() {
printf "cd " path " && "
}
if (terminal){
- printf terminal_command " "
+ printf terminal_cmd " "
}
print exec
}' "$1"
}
case "$1" in
-describe | entries | list-entries | list-commands | command-line | generate-command | provide)
+describe | describe-desktop | describe-command | entries | list-entries | list-commands | command-line | generate-command | provide)
"$@"
exit
;;
@@ -183,11 +221,13 @@ trap 'rm "$FZFPIPE"' EXIT INT
(printf '%s' "${HIST_LINES[@]#* }" >>"$FZFPIPE") &
# Iterate over providers and run their list-command
-for PROVIDER_NAME in "${!PROVIDERS[@]}"; do (bash -c "${0} provide ${PROVIDER_NAME}" >>"$FZFPIPE") & done
+for PROVIDER_NAME in "${!PROVIDERS[@]}"; do
+ (bash -c "${0} provide ${PROVIDER_NAME}" >>"$FZFPIPE") &
+done
COMMAND_STR=$(
fzf +s -x -d '\034' --nth ..3 --with-nth 3 \
- --preview "$0 describe {1} {2}" \
+ --preview "$0 describe {2} {1}" \
--preview-window=up:3:wrap --ansi \
<"$FZFPIPE"
) || exit 1
diff --git a/tests/data/config/0/sway-launcher-desktop/providers.conf b/tests/data/config/0/sway-launcher-desktop/providers.conf
new file mode 100644
index 0000000..4e82b2b
--- /dev/null
+++ b/tests/data/config/0/sway-launcher-desktop/providers.conf
@@ -0,0 +1,7 @@
+[foo]
+list_cmd=printf "foo\034foo"
+launch_cmd=printf 'printf "{1}"'
+preview_cmd=printf 'printf "{1}"'
+
+[incomplete]
+list_cmd=echo 'nope'
diff --git a/tests/describe.bats b/tests/describe.bats
index d02ee96..1541974 100644
--- a/tests/describe.bats
+++ b/tests/describe.bats
@@ -1,13 +1,13 @@
@test "Name and description of firefox desktop file are properly extracted" {
- run ../sway-launcher-desktop.sh describe data/desktop-files/0/applications/firefox.desktop desktop
+ run env XDG_CONFIG_HOME=./data/config ../sway-launcher-desktop.sh describe desktop ./data/desktop-files/0/applications/firefox.desktop
[ "$status" -eq 0 ]
[[ ${lines[0]} =~ "Firefox" ]]
[[ ${lines[1]} =~ "Browse the World Wide Web" ]]
}
@test "Name and description of ls command should be given" {
- run ../sway-launcher-desktop.sh describe ls command
+ run env XDG_CONFIG_HOME=./data/config ../sway-launcher-desktop.sh describe command ls
[ "$status" -eq 0 ]
[[ ${lines[0]} =~ "ls" ]]
[[ ${lines[1]} =~ "list directory contents" ]]
-} \ No newline at end of file
+}
diff --git a/tests/providers.bats b/tests/providers.bats
new file mode 100644
index 0000000..1c11f0d
--- /dev/null
+++ b/tests/providers.bats
@@ -0,0 +1,36 @@
+@test "Builtin desktop provider works" {
+ run env XDG_CONFIG_HOME=./data/config XDG_DATA_HOME=./data/desktop-files/1 XDG_DATA_DIRS=./data/desktop-files/0 ../sway-launcher-desktop.sh provide desktop
+ echo "OUTPUT:$output"
+ echo "LINES:${#lines[@]}"
+ [ "$status" -eq 0 ]
+ [[ ${#lines[@]} -gt 2 ]]
+}
+
+@test "Builtin command provider works" {
+ run env XDG_CONFIG_HOME=./data/config XDG_DATA_HOME=./data/desktop-files/1 XDG_DATA_DIRS=./data/desktop-files/0 ../sway-launcher-desktop.sh provide command
+ echo "OUTPUT:$output"
+ echo "LINES:${#lines[@]}"
+ [ "$status" -eq 0 ]
+ [[ ${#lines[@]} -gt 2 ]]
+}
+
+@test "Reads custom provider from providers.conf" {
+ run printf %q "$(env XDG_CONFIG_HOME=./data/config/0 ../sway-launcher-desktop.sh provide foo)"
+ echo "OUTPUT:$output"
+ [ "$status" -eq 0 ]
+ [[ ${output} == "$'foo\034foo'" ]]
+}
+
+@test "Skips incomplete custom provider from providers.conf" {
+ run printf %q "$(env XDG_CONFIG_HOME=./data/config/0 ../sway-launcher-desktop.sh provide incomplete)"
+ echo "OUTPUT:$output"
+ [ "$status" -eq 0 ]
+ [[ ${output} == "''" ]]
+}
+
+@test "Does not use builtin providers when reading from providers.conf" {
+ run printf %q "$(env XDG_CONFIG_HOME=./data/config/0 ../sway-launcher-desktop.sh provide desktop)"
+ echo "OUTPUT:$output"
+ [ "$status" -eq 0 ]
+ [[ ${output} == "''" ]]
+}