diff options
| author | Moritz Meißelbach <arbelzapf@gmail.com> | 2019-10-24 21:21:27 +0200 |
|---|---|---|
| committer | Moritz Meißelbach <arbelzapf@gmail.com> | 2019-10-24 21:21:27 +0200 |
| commit | 71d526589f8e7ad8bf92a8e78c34c7af5e1497cb (patch) | |
| tree | 1316e5fecd55ed62f3d1f50a41638f775cb91be5 | |
| parent | b57166d0687b2ba9e7981dd4a2e4078abe09acc7 (diff) | |
| parent | 2a4c9971ad00ede63e9f964d21745b108889b0b8 (diff) | |
| download | sway-launcher-desktop-71d526589f8e7ad8bf92a8e78c34c7af5e1497cb.tar.gz sway-launcher-desktop-71d526589f8e7ad8bf92a8e78c34c7af5e1497cb.zip | |
Merge remote-tracking branch 'origin/master'
| -rw-r--r-- | README.md | 1 | ||||
| -rwxr-xr-x | sway-launcher-desktop.sh | 178 | ||||
| -rw-r--r-- | tests/data/htop.desktop | 65 | ||||
| -rw-r--r-- | tests/data/minecraft-launcher.desktop | 10 | ||||
| -rw-r--r-- | tests/describe.bats | 2 | ||||
| -rw-r--r-- | tests/entries.bats | 14 | ||||
| -rw-r--r-- | tests/generate-command.bats | 5 |
7 files changed, 193 insertions, 82 deletions
@@ -1,3 +1,4 @@ +[](https://travis-ci.org/Biont/sway-launcher-desktop) # sway-launcher-desktop This is a launcher menu made for the Sway window manager made with bash and the amazing [fzf](https://github.com/junegunn/fzf). diff --git a/sway-launcher-desktop.sh b/sway-launcher-desktop.sh index 5d06a4b..cf6b359 100755 --- a/sway-launcher-desktop.sh +++ b/sway-launcher-desktop.sh @@ -2,8 +2,12 @@ # 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 +set -o pipefail +# shellcheck disable=SC2154 +trap 's=$?; echo "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR +IFS=$'\n\t' + if [[ "$1" == 'describe' ]]; then shift if [[ $2 == 'command' ]]; then @@ -23,7 +27,96 @@ fi # Defaulting terminal to termite, but feel free to either change # this or override with an environment variable in your sway config # It would be good to move this to a config file eventually -set "${TERMINAL_COMMAND:="termite -e"}" +TERMINAL_COMMAND="${TERMINAL_COMMAND:="termite -e"}" +GLYPH_COMMAND=" " +GLYPH_DESKTOP=" " + +if [[ "$1" == '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"] + } + }' \ + "$@" </dev/null + # the empty stdin is needed in case no *.desktop files + + exit 0 +fi + +if [[ "$1" == 'generate-command' ]]; then + shift + # Define the search pattern that specifies the block to search for within the .desktop file + PATTERN="^\\\\[Desktop Entry\\\\]" + if [[ -n $2 ]]; then + PATTERN="^\\\\[Desktop Action ${2%?}\\\\]" + fi + # 1. We see a line starting [Desktop, but we're already searching: deactivate search again + # 2. We see the specified pattern: start search + # 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= ' + BEGIN{a=0;exec=0;path=0} + /^\[Desktop/{ + if(a){ + a=0 + } + } + $0 ~ pattern{ + a=1 + } + /^Terminal=/{ + sub("^Terminal=", ""); + if ($0 == "true") { + terminal=1 + } + } + /^Exec=/{ + if(a && !exec){ + sub("^Exec=", ""); + gsub(" ?%[cDdFfikmNnUuv]", ""); + exec=$0; + } + } + /^Path=/{ + if(a && !path){ + path=$2 + } + } + + END{ + if(path){ + printf "cd " path " &&" + } + if (terminal){ + printf terminal_command " " + } + print exec + }' "$1" + exit 0 +fi HIST_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/${0##*/}-history.txt" @@ -33,9 +126,6 @@ DIRS=( /usr/local/share/applications ) -GLYPH_COMMAND=" " -GLYPH_DESKTOP=" " - touch "$HIST_FILE" readarray HIST_LINES <"$HIST_FILE" FZFPIPE=$(mktemp) @@ -49,34 +139,7 @@ trap 'rm "$FZFPIPE" "$PIDFILE"' EXIT INT ( for dir in "${DIRS[@]}"; do [[ -d "$dir" ]] || continue - 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"] - } - }' \ - "$dir/"*.desktop </dev/null >>"$FZFPIPE" - # the empty stdin is needed in case no *.desktop files + "$0 entries $dir/*.desktop" >>"$FZFPIPE" done ) & @@ -124,54 +187,7 @@ readarray -d $'\034' -t PARAMS <<<${COMMAND_STR} # COMMAND_STR is "<string>\034<type>" case ${PARAMS[1]} in desktop) - # Define the search pattern that specifies the block to search for within the .desktop file - PATTERN="^\\\\[Desktop Entry\\\\]" - if [[ -n ${PARAMS[3]} ]]; then - PATTERN="^\\\\[Desktop Action ${PARAMS[3]%?}\\\\]" - fi - # 1. We see a line starting [Desktop, but we're already searching: deactivate search again - # 2. We see the specified pattern: start search - # 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 - command=$(awk -v pattern="${PATTERN}" -v terminal_command="${TERMINAL_COMMAND}" -F= ' - BEGIN{a=0;exec=0;path=0} - /^\[Desktop/{ - if(a){ - a=0 - } - } - $0 ~ pattern{ - a=1 - } - /^Terminal=/{ - sub("^Terminal=", ""); - if ($0 == "true") { - terminal=1 - } - } - /^Exec=/{ - if(a && !exec){ - sub("^Exec=", ""); - gsub(" ?%[cDdFfikmNnUuv]", ""); - exec=$0; - } - } - /^Path=/{ - if(a && !path){ - path=$2 - } - } - - END{ - if(path){ - printf "cd " path " &&" - } - if (terminal){ - printf terminal_command " " - } - print exec - }' "${PARAMS[0]}") + command=$($0 generate-command "${PARAMS[0]}" "${PARAMS[3]}") ;; command) command="${PARAMS[0]}" diff --git a/tests/data/htop.desktop b/tests/data/htop.desktop new file mode 100644 index 0000000..13b71e7 --- /dev/null +++ b/tests/data/htop.desktop @@ -0,0 +1,65 @@ +[Desktop Entry] +Version=1.0 +Name=Htop +Type=Application +Comment=Show System Processes +Comment[ca]=Mostra els processos del sistema +Comment[de]=Systemprozesse anzeigen +Comment[en_GB]=Show System Processes +Comment[es]=Mostrar procesos del sistema +Comment[fi]=Katsele järjestelmän prosesseja +Comment[fr]=Affiche les processus système +Comment[gl]=Mostrar os procesos do sistema. +Comment[it]=Mostra processi di sistema +Comment[ko]=시스템 프로세스 보기 +Comment[nb]=Vis systemprosesser +Comment[nl]=Systeemprocessen tonen +Comment[nn]=Vis systemprosessar +Comment[pl]=Pokaż procesy systemowe +Comment[pt]=Mostrar os Processos do Sistema +Comment[pt_BR]=Mostra os processos do sistema +Comment[ru]=Просмотр списка процессов в системе +Comment[sk]=Zobraziť systémové procesy +Comment[sl]=Prikaz sistemskih opravil +Comment[sr]=Приказ системских процеса +Comment[sr@ijekavian]=Приказ системских процеса +Comment[sr@ijekavianlatin]=Prikaz sistemskih procesa +Comment[sr@latin]=Prikaz sistemskih procesa +Comment[sv]=Visa systemprocesser +Comment[tr]=Sistem Süreçlerini Göster +Comment[uk]=Перегляд системних процесів +Comment[zh_CN]=显示系统进程 +Comment[zh_TW]=顯示系統行程 +Terminal=true +Exec=htop +Icon=htop +Categories=ConsoleOnly;System; +GenericName=Process Viewer +GenericName[ca]=Visor de processos +GenericName[de]=Prozessanzeige +GenericName[en_GB]=Process Viewer +GenericName[es]=Visor de procesos +GenericName[fi]=Prosessikatselin +GenericName[fr]=Visualiseur de processus +GenericName[gl]=Visor de procesos +GenericName[it]=Visore dei processi +GenericName[ko]=프로세스 뷰어 +GenericName[nb]=Prosessviser +GenericName[nl]=Viewer van processen +GenericName[nn]=Prosessvisar +GenericName[pl]=Przeglądarka procesów +GenericName[pt]=Visualizador de Processos +GenericName[pt_BR]=Visualizador de processos +GenericName[ru]=Монитор процессов +GenericName[sk]=Prehliadač procesov +GenericName[sl]=Pregledovalnik opravil +GenericName[sr]=Приказивач процеса +GenericName[sr@ijekavian]=Приказивач процеса +GenericName[sr@ijekavianlatin]=Prikazivač procesa +GenericName[sr@latin]=Prikazivač procesa +GenericName[sv]=Processvisning +GenericName[tr]=Süreç Görüntüleyici +GenericName[uk]=Перегляд процесів +GenericName[zh_CN]=进程查看器 +GenericName[zh_TW]=行程檢視器 +Keywords=system;process;task diff --git a/tests/data/minecraft-launcher.desktop b/tests/data/minecraft-launcher.desktop new file mode 100644 index 0000000..e2c464c --- /dev/null +++ b/tests/data/minecraft-launcher.desktop @@ -0,0 +1,10 @@ +[Desktop Entry] +Type=Application +Version=1.0 +Name=Minecraft Launcher +Comment=Official Minecraft Launcher +Exec=env GDK_BACKEND=x11 /opt/minecraft-launcher/minecraft-launcher +Path=/opt/minecraft-launcher/ +Icon=minecraft-launcher +Terminal=false +Categories=Game;Application; diff --git a/tests/describe.bats b/tests/describe.bats index 7d2f866..7f26184 100644 --- a/tests/describe.bats +++ b/tests/describe.bats @@ -1,5 +1,5 @@ @test "Name and description of firefox desktop file are properly extracted" { - run ../sway-launcher-desktop.sh describe data/firefox.desktop + run ../sway-launcher-desktop.sh describe data/firefox.desktop desktop [ "$status" -eq 0 ] [[ ${lines[0]} =~ "Firefox" ]] [[ ${lines[1]} =~ "Browse the World Wide Web" ]] diff --git a/tests/entries.bats b/tests/entries.bats new file mode 100644 index 0000000..304dc6a --- /dev/null +++ b/tests/entries.bats @@ -0,0 +1,14 @@ +@test "Firefox desktop entry and all its actions are extracted" { + run ../sway-launcher-desktop.sh entries data/firefox.desktop + [ "$status" -eq 0 ] + [[ ${lines[0]} =~ data/firefox.desktop ]] + [[ ${lines[0]} =~ ^data/firefox.desktop.*Firefox ]] + [[ ${lines[1]} =~ ^data/firefox.desktop.*Firefox.*(New Window).*new-window ]] + [[ ${lines[2]} =~ ^data/firefox.desktop.*Firefox.*(New Private Window).*new-private-window ]] +} + +@test "Wildcard expansion works for extraction of desktop files" { + run ../sway-launcher-desktop.sh entries data/*.desktop + [ "$status" -eq 0 ] + [[ ${#lines[@]} == 5 ]] +}
\ No newline at end of file diff --git a/tests/generate-command.bats b/tests/generate-command.bats new file mode 100644 index 0000000..780c963 --- /dev/null +++ b/tests/generate-command.bats @@ -0,0 +1,5 @@ +@test "Exec command is properly extracted from Firefox desktop file" { + run ../sway-launcher-desktop.sh generate-command data/firefox.desktop + [ "$status" -eq 0 ] + [[ "$output" == '/usr/lib/firefox/firefox' ]] +}
\ No newline at end of file |
