#!/bin/sh # spmenu_desktop - list .desktop entries with the icon. # NOTE: Please pull request if you have any possible improvements. [ -z "$desktop_dir" ] && desktop_dir="/usr/share/applications" [ -z "$icon_dir" ] && icon_dir="/usr/share/icons/hicolor /usr/share/pixmaps" [ -z "$temporary_dir" ] && temporary_dir="$HOME/.config/spmenu/desktop/cache" [ -z "$useimage" ] && useimage=true [ -z "$config" ] && config="$HOME/.config/spmenu/desktop/config" [ -z "$cache" ] && cache=true [ -z "$RUNLAUNCHER" ] && RUNLAUNCHER=spmenu [ -z "$RUNLAUNCHER_ARGS" ] && RUNLAUNCHER_ARGS="--lines 20 --columns 1 --image-size 100 --image-gaps 20" [ -z "$logfile" ] && logfile="/tmp/spmenu_desktop.log" # functions print_menu() { res="$(print_list | $RUNLAUNCHER $@ $RUNLAUNCHER_ARGS)"; } prep() { mkdir -p "$temporary_dir"; rm -f "$logfile"; touch "$logfile"; } scan() { entry_c="$(find "$desktop_dir" -type f | wc -l)"; cached_c="$(find "$temporary_dir" -type f | wc -l)"; cached="$(find "$temporary_dir" -type f)"; } w_conf() { if [ ! -f "$config" ]; then mkdir -p "$(dirname $config)" cat << EOF > "$config" # spmenu_desktop configuration file desktop_dir="/usr/share/applications" # Directories to look for .desktop files in icon_dir="/usr/share/icons/hicolor /usr/share/pixmaps" # Directories to look for icons in temporary_dir="\$HOME/.config/spmenu/desktop/cache" # Directory used to store cache useimage=true # Enable image support RUNLAUNCHER="\${RUNLAUNCHER:-spmenu}" RUNLAUNCHER_ARGS="\${RUNLAUNCHER_ARGS:- --lines 20 --columns 1 --image-size 100 --image-gaps 20}" logfile="/tmp/spmenu_desktop.log" EOF fi [ -f "$config" ] && . "$config" } # cache it, this means some speed improvements cache() { printf "Cached: %s\n" "$cached_c" >> "$logfile" printf "Entries: %s\n" "$entry_c" >> "$logfile" [ "$cached_c" = "$entry_c" ] && return # we don't need to cache anything, it's already done # find entry="$(find $desktop_dir -type f)" icons="$(find $icon_dir -type f)" # write new entries for i in $(seq "$entry_c"); do cur_file="$(printf "%s" "$entry" | sed "${i}q;d")" exec="$(grep -v "TryExec" "$cur_file" | grep -m1 "Exec=" | sed "s/Exec=//g; s/%U//g; s/%F//g; s/%u//g")" name="$(grep "Name=" "$cur_file" | grep -v Generic | head -n 1 | sed "s/Name=//g")" # icon name icon_name="$(grep "Icon=" "$cur_file" | head -n 1 | sed "s/Icon=//g")" icon="$(printf "%s" "$icons" | grep "/$icon_name[.]" | head -n 1)" && [ ! -f "$icon" ] && icon="" # write the file printf "%s\n%s\n%s\n" "Name:$name" "Executable:$exec" "Icon:$icon" > "$temporary_dir/$(basename "$cur_file").entry" done # run scan() again scan } print_list() { # print data from entries for i in $(seq "$cached_c"); do # current file cur_file="$(printf "%s" "$cached" | sed "${i}q;d")" && [ ! -e "$cur_file" ] && printf "File '%s' does not exist. Skipping...\n" "$cur_file" >> "$logfile" && continue # get details to display title="$(head -n 1 "$cur_file" | sed "s/Name://g")" icon="$(tail -n 1 "$cur_file" | sed "s/Icon://g")" [ -z "$title" ] && continue # no title isn't worth displaying, obviously # print it all [ "$useimage" = "true" ] && [ ! -e "$icon" ] && useimage=false && reenable=1 [ "$useimage" = "true" ] && printf "%s\t%s\n" "IMG:${icon}" "$title" || \ printf "%s\n" "$title" [ "$reenable" = "1" ] && useimage=true done } execute_program() { [ -z "$res" ] && cached_c=0 || printf "User input: %s\n" "$res" >> "$logfile" for i in $(seq "$cached_c"); do cur_file="$(printf "%s" "$cached" | sed "${i}q;d")" && [ ! -f "$cur_file" ] && printf "File '%s' does not exist. Skipping...\n" "$cur_file" >> "$logfile" && continue # find the executable matching the selected name if grep -q "Name:$res" "$cur_file"; then exec="$(head -n 2 "$cur_file" | tail -n -1 | sed "s/Executable://g")" printf "Current file: '%s'\n" "$cur_file" >> "$logfile" break; else exec="" continue; fi done # finally run the program if [ -n "$exec" ]; then /bin/sh -c "$exec" else [ ! -z "$res" ] && printf "No executable found. Try clearing cache." >> "$logfile" fi return 0 } main() { w_conf "$@" prep scan cache print_menu "$@" execute_program "$@" } main "$@"