diff options
| author | 2015-09-07 22:18:08 +0200 | |
|---|---|---|
| committer | 2015-09-07 22:18:08 +0200 | |
| commit | ec6413b8106768b25afa46f4da63ff88d0322cce (patch) | |
| tree | 1962ac4e4f25b2b8e17d9c21e02487ddfe7e890c | |
| parent | 3b4ffb8c9a5c0ed272032920172147d01cc415bb (diff) | |
| download | dmenu_app-ec6413b8106768b25afa46f4da63ff88d0322cce.tar.gz dmenu_app-ec6413b8106768b25afa46f4da63ff88d0322cce.tar.bz2 dmenu_app-ec6413b8106768b25afa46f4da63ff88d0322cce.zip | |
Added comments
| -rwxr-xr-x | dmenu_app | 45 |
1 files changed, 42 insertions, 3 deletions
| @@ -1,38 +1,77 @@ | |||
| 1 | #!/usr/bin/env bash | 1 | #!/usr/bin/env bash |
| 2 | 2 | ||
| 3 | # Add a menu entry to the cache file | ||
| 3 | add_to_cache(){ | 4 | add_to_cache(){ |
| 5 | |||
| 6 | # First parameter is the .desktop file | ||
| 4 | application="$1" | 7 | application="$1" |
| 5 | 8 | ||
| 9 | # Get the names and commands from the .desktop file | ||
| 6 | names=$(sed -n "s/^Name=\(.*\)$/\1/p" "$application") | 10 | names=$(sed -n "s/^Name=\(.*\)$/\1/p" "$application") |
| 7 | executables=$(sed -n "s/^Exec=\(.*\)$/\1/p" "$application") | 11 | executables=$(sed -n "s/^Exec=\(.*\)$/\1/p" "$application") |
| 8 | 12 | ||
| 13 | # If there are no names or executables, return | ||
| 9 | if [ -n "$names" ] && [ -n "$executables" ]; then | 14 | if [ -n "$names" ] && [ -n "$executables" ]; then |
| 15 | |||
| 16 | # Some .desktop files have multiple names and commands because they | ||
| 17 | # have multiple actions in xdg menus | ||
| 18 | # We assume the first name and command are the right ones | ||
| 10 | name=$(echo "$names" | sed "1!d") | 19 | name=$(echo "$names" | sed "1!d") |
| 11 | executable=$(echo "$executables" | sed "1!d") | 20 | executable=$(echo "$executables" | sed "1!d") |
| 12 | 21 | ||
| 13 | if [ -n "$name" ] && [ -n "$executable" ]; then | 22 | # Add them to the cache file with a colon as seperator |
| 14 | echo -e "$name:$executable" >> "$cache_file" | 23 | echo -e "$name:$executable" >> "$cache_file" |
| 15 | fi | ||
| 16 | fi | 24 | fi |
| 17 | } | 25 | } |
| 18 | 26 | ||
| 27 | # These are the locaions to search for .desktop files | ||
| 19 | usrlocations=$(echo {/usr,/usr/local,$HOME/.local,$HOME/.local/usr,$HOME/.local/usr/local}/share/applications) | 28 | usrlocations=$(echo {/usr,/usr/local,$HOME/.local,$HOME/.local/usr,$HOME/.local/usr/local}/share/applications) |
| 29 | |||
| 30 | # All the desktop files the find-command found | ||
| 20 | desktop_files=$(find $usrlocations -name "*.desktop" 2> /dev/null) | 31 | desktop_files=$(find $usrlocations -name "*.desktop" 2> /dev/null) |
| 32 | |||
| 33 | # Checksum the filenames | ||
| 21 | checksum=$(echo $desktop_files | sha1sum) | 34 | checksum=$(echo $desktop_files | sha1sum) |
| 35 | |||
| 36 | # The cache file location | ||
| 22 | cache_file="$HOME/.cache/dmenu_app" | 37 | cache_file="$HOME/.cache/dmenu_app" |
| 38 | |||
| 39 | # Create a new cachefile if there is no cachefile or the checksum on the top line | ||
| 40 | # does not match the checksum of the filenames | ||
| 23 | if [ ! -f "$cache_file" ] || [ "$(head -n1 "$cache_file")" != "$checksum" ]; then | 41 | if [ ! -f "$cache_file" ] || [ "$(head -n1 "$cache_file")" != "$checksum" ]; then |
| 24 | echo "Updating cache file" | 42 | echo "Updating cache file" |
| 43 | |||
| 44 | # Add the checksup to the top line of the cache file. This we use to check | ||
| 45 | # if the .desktopfiles are updated since the cache file was last created. | ||
| 25 | echo "$checksum" > "$cache_file" | 46 | echo "$checksum" > "$cache_file" |
| 47 | |||
| 48 | # Add all desktop entries to the cache file | ||
| 26 | while read application; do | 49 | while read application; do |
| 50 | |||
| 51 | # Forking the add_to_cache process, halves the time to add all entries | ||
| 52 | # to the cache file in my case | ||
| 27 | add_to_cache "$application" & | 53 | add_to_cache "$application" & |
| 28 | done < <(echo "$desktop_files") | 54 | done < <(echo "$desktop_files") |
| 55 | |||
| 56 | # Wait until all entries are in the cache file | ||
| 29 | wait | 57 | wait |
| 30 | fi | 58 | fi |
| 31 | 59 | ||
| 60 | # Pipe all the destop entry names to dmenu in alphabetical order | ||
| 32 | choice=$(cat $cache_file | tail -n +2 | cut -d: -f1 | sort -h | dmenu "$@") | 61 | choice=$(cat $cache_file | tail -n +2 | cut -d: -f1 | sort -h | dmenu "$@") |
| 62 | |||
| 63 | # If nothing is returned from dmenu, do nothing | ||
| 33 | if [ -n "$choice" ]; then | 64 | if [ -n "$choice" ]; then |
| 65 | |||
| 66 | # Get the respective command from the cache file | ||
| 34 | command=$(sed -n "s/^$choice:\(.*\)$/\1/p" "$cache_file") | 67 | command=$(sed -n "s/^$choice:\(.*\)$/\1/p" "$cache_file") |
| 68 | |||
| 69 | # If the command does not exists, do nothing | ||
| 35 | if [ -n "$command" ]; then | 70 | if [ -n "$command" ]; then |
| 71 | |||
| 72 | # Execute the command | ||
| 36 | echo "$command" | sh | 73 | echo "$command" | sh |
| 37 | fi | 74 | fi |
| 38 | fi | 75 | fi |
| 76 | |||
| 77 | # vim: set ts=8 sw=8 tw=0 noet : | ||
