diff options
| -rwxr-xr-x | dmenu_app | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/dmenu_app b/dmenu_app new file mode 100755 index 0000000..ded6b6d --- /dev/null +++ b/dmenu_app | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | #!/usr/bin/env bash | ||
| 2 | |||
| 3 | add_to_cache(){ | ||
| 4 | application="$1" | ||
| 5 | |||
| 6 | names=$(sed -n "s/^Name=\(.*\)$/\1/p" "$application") | ||
| 7 | executables=$(sed -n "s/^Exec=\(.*\)$/\1/p" "$application") | ||
| 8 | |||
| 9 | if [ -n "$names" ] && [ -n "$executables" ]; then | ||
| 10 | name=$(echo "$names" | sed "1!d") | ||
| 11 | executable=$(echo "$executables" | sed "1!d") | ||
| 12 | |||
| 13 | if [ -n "$name" ] && [ -n "$executable" ]; then | ||
| 14 | echo -e "$name:$executable" >> "$cache_file" | ||
| 15 | fi | ||
| 16 | fi | ||
| 17 | } | ||
| 18 | |||
| 19 | usrlocations=$(echo {/usr,/usr/local,$HOME/.local,$HOME/.local/usr,$HOME/.local/usr/local}/share/applications) | ||
| 20 | desktop_files=$(find $usrlocations -name "*.desktop" 2> /dev/null) | ||
| 21 | checksum=$(echo $desktop_files | sha1sum) | ||
| 22 | cache_file="$HOME/.cache/dmenu_app" | ||
| 23 | if [ ! -f "$cache_file" ] || [ "$(head -n1 "$cache_file")" != "$checksum" ]; then | ||
| 24 | echo "Updating cache file" | ||
| 25 | echo "$checksum" > "$cache_file" | ||
| 26 | while read application; do | ||
| 27 | add_to_cache "$application" & | ||
| 28 | done < <(echo "$desktop_files") | ||
| 29 | wait | ||
| 30 | fi | ||
| 31 | |||
| 32 | choice=$(cat $cache_file | tail -n +2 | cut -d: -f1 | sort -h | dmenu $@) | ||
| 33 | if [ -n "$choice" ]; then | ||
| 34 | command=$(sed -n "s/^$choice:\(.*\)$/\1/p" "$cache_file") | ||
| 35 | if [ -n "$command" ]; then | ||
| 36 | echo "$command" | sh | ||
| 37 | fi | ||
| 38 | fi | ||
