From c5328b227e2b8ba81254acf7ee820f098a287c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20Mei=C3=9Felbach?= Date: Thu, 24 Oct 2019 14:54:50 +0200 Subject: Add new tests and test data. Make parsing of desktop entries testable --- sway-launcher-desktop.sh | 79 ++++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 33 deletions(-) (limited to 'sway-launcher-desktop.sh') diff --git a/sway-launcher-desktop.sh b/sway-launcher-desktop.sh index 5d06a4b..ff41305 100755 --- a/sway-launcher-desktop.sh +++ b/sway-launcher-desktop.sh @@ -2,9 +2,15 @@ # 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 +set -uo pipefail +# shellcheck disable=SC2154 +trap 's=$?; echo "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR +IFS=$'\n\t' + +SUBCOMMAND=${1:-} + +if [[ -n $SUBCOMMAND ]] && [[ "$SUBCOMMAND" == 'describe' ]]; then shift if [[ $2 == 'command' ]]; then title=$1 @@ -20,6 +26,43 @@ if [[ "$1" == 'describe' ]]; then exit fi +GLYPH_COMMAND=" " +GLYPH_DESKTOP=" " + +if [[ -n $SUBCOMMAND ]] && [[ "$SUBCOMMAND" == 'entries' ]]; then + shift + 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"] + } + }' \ + "$@" >"$FZFPIPE" - # the empty stdin is needed in case no *.desktop files + "$0 entries $dir/*.desktop" >>"$FZFPIPE" done ) & -- cgit v1.2.3