2023-03-25 22:34:21 +01:00
|
|
|
#!/bin/sh
|
2023-03-28 00:30:47 +02:00
|
|
|
# spmenu_desktop - list .desktop entries with the icon.
|
|
|
|
# NOTE: Please pull request if you have any possible improvements.
|
2023-03-25 22:34:21 +01:00
|
|
|
[ -z "$desktop_dir" ] && desktop_dir="/usr/share/applications"
|
2023-03-26 16:03:13 +02:00
|
|
|
[ -z "$icon_dir" ] && icon_dir="/usr/share/icons/hicolor /usr/share/pixmaps"
|
2023-03-25 22:34:21 +01:00
|
|
|
[ -z "$temporary_dir" ] && temporary_dir="$HOME/.config/spmenu/desktop/cache"
|
|
|
|
[ -z "$useimage" ] && useimage=true
|
2023-03-28 19:49:05 +02:00
|
|
|
[ -z "$config" ] && config="$HOME/.config/spmenu/desktop/config"
|
2023-03-29 19:56:52 +02:00
|
|
|
[ -z "$cache" ] && cache=true
|
2023-03-25 22:34:21 +01:00
|
|
|
[ -z "$RUNLAUNCHER" ] && RUNLAUNCHER=spmenu
|
2023-03-28 02:53:28 +02:00
|
|
|
[ -z "$RUNLAUNCHER_ARGS" ] && RUNLAUNCHER_ARGS="--lines 20 --columns 1 --image-size 100 --image-gaps 20"
|
2023-03-28 19:49:05 +02:00
|
|
|
[ -z "$logfile" ] && logfile="/tmp/spmenu_desktop.log"
|
2023-03-25 22:34:21 +01:00
|
|
|
|
2023-03-28 00:30:47 +02:00
|
|
|
# functions
|
2023-03-28 23:55:06 +02:00
|
|
|
print_menu() { res="$(print_list | $RUNLAUNCHER $@ $RUNLAUNCHER_ARGS)"; }
|
2023-03-28 19:49:05 +02:00
|
|
|
prep() { mkdir -p "$temporary_dir"; rm -f "$logfile"; touch "$logfile"; }
|
2023-03-28 00:30:47 +02:00
|
|
|
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)"; }
|
2023-03-25 22:34:21 +01:00
|
|
|
|
2023-03-28 19:49:05 +02:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2023-03-28 00:30:47 +02:00
|
|
|
# cache it, this means some speed improvements
|
2023-03-25 22:34:21 +01:00
|
|
|
cache() {
|
2023-03-28 19:49:05 +02:00
|
|
|
printf "Cached: %s\n" "$cached_c" >> "$logfile"
|
|
|
|
printf "Entries: %s\n" "$entry_c" >> "$logfile"
|
2023-03-25 22:34:21 +01:00
|
|
|
[ "$cached_c" = "$entry_c" ] && return # we don't need to cache anything, it's already done
|
2023-03-26 16:12:27 +02:00
|
|
|
|
|
|
|
# find
|
2023-03-28 00:30:47 +02:00
|
|
|
entry="$(find $desktop_dir -type f)"
|
2023-03-26 16:12:27 +02:00
|
|
|
icons="$(find $icon_dir -type f)"
|
|
|
|
|
|
|
|
# write new entries
|
2023-03-25 22:34:21 +01:00
|
|
|
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")"
|
|
|
|
name="$(grep "Name=" "$cur_file" | grep -v Generic | head -n 1 | sed "s/Name=//g")"
|
2023-03-28 02:53:28 +02:00
|
|
|
|
|
|
|
# icon name
|
2023-03-25 22:34:21 +01:00
|
|
|
icon_name="$(grep "Icon=" "$cur_file" | head -n 1 | sed "s/Icon=//g")"
|
2023-03-29 19:56:52 +02:00
|
|
|
icon="$(printf "%s" "$icons" | grep "/$icon_name[.]" | head -n 1)" && [ ! -f "$icon" ] && icon=""
|
2023-03-25 22:34:21 +01:00
|
|
|
|
2023-03-26 16:12:27 +02:00
|
|
|
# write the file
|
2023-03-29 19:56:52 +02:00
|
|
|
printf "%s\n%s\n%s\n" "Name:$name" "Executable:$exec" "Icon:$icon" > "$temporary_dir/$(basename "$cur_file").entry"
|
2023-03-25 22:34:21 +01:00
|
|
|
done
|
2023-03-29 19:56:52 +02:00
|
|
|
|
|
|
|
# run scan() again
|
|
|
|
scan
|
2023-03-25 22:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
print_list() {
|
2023-03-26 16:12:27 +02:00
|
|
|
# print data from entries
|
2023-03-25 22:34:21 +01:00
|
|
|
for i in $(seq "$cached_c"); do
|
2023-03-28 00:30:47 +02:00
|
|
|
# current file
|
2023-03-28 19:49:05 +02:00
|
|
|
cur_file="$(printf "%s" "$cached" | sed "${i}q;d")" && [ ! -e "$cur_file" ] && printf "File '%s' does not exist. Skipping...\n" "$cur_file" >> "$logfile" && continue
|
2023-03-28 00:30:47 +02:00
|
|
|
|
|
|
|
# get details to display
|
|
|
|
title="$(head -n 1 "$cur_file" | sed "s/Name://g")"
|
2023-03-29 19:56:52 +02:00
|
|
|
icon="$(tail -n 1 "$cur_file" | sed "s/Icon://g")"
|
2023-03-28 00:30:47 +02:00
|
|
|
|
2023-03-29 19:56:52 +02:00
|
|
|
[ -z "$title" ] && continue # no title isn't worth displaying, obviously
|
2023-03-28 02:54:47 +02:00
|
|
|
|
2023-03-29 19:56:52 +02:00
|
|
|
# print it all
|
2023-03-25 22:34:21 +01:00
|
|
|
[ "$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() {
|
2023-03-28 19:49:05 +02:00
|
|
|
[ -z "$res" ] && cached_c=0 || printf "User input: %s\n" "$res" >> "$logfile"
|
2023-03-25 22:34:21 +01:00
|
|
|
for i in $(seq "$cached_c"); do
|
2023-03-28 19:49:05 +02:00
|
|
|
cur_file="$(printf "%s" "$cached" | sed "${i}q;d")" && [ ! -f "$cur_file" ] && printf "File '%s' does not exist. Skipping...\n" "$cur_file" >> "$logfile" && continue
|
2023-03-28 00:30:47 +02:00
|
|
|
|
2023-03-29 19:56:52 +02:00
|
|
|
# find the executable matching the selected name
|
2023-03-28 00:30:47 +02:00
|
|
|
if grep -q "Name:$res" "$cur_file"; then
|
|
|
|
exec="$(head -n 2 "$cur_file" | tail -n -1 | sed "s/Executable://g")"
|
2023-03-28 19:49:05 +02:00
|
|
|
printf "Current file: '%s'\n" "$cur_file" >> "$logfile"
|
2023-03-28 00:30:47 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
exec=""
|
|
|
|
continue;
|
|
|
|
fi
|
2023-03-25 22:34:21 +01:00
|
|
|
done
|
|
|
|
|
2023-03-26 16:12:27 +02:00
|
|
|
# finally run the program
|
2023-03-28 00:30:47 +02:00
|
|
|
if [ -n "$exec" ]; then
|
2023-03-29 19:56:52 +02:00
|
|
|
/bin/sh -c "$exec"
|
2023-03-28 00:30:47 +02:00
|
|
|
else
|
2023-03-29 19:56:52 +02:00
|
|
|
[ ! -z "$res" ] && printf "No executable found. Try clearing cache." >> "$logfile"
|
2023-03-28 00:30:47 +02:00
|
|
|
fi
|
2023-03-25 22:34:21 +01:00
|
|
|
|
2023-03-28 00:30:47 +02:00
|
|
|
return 0
|
2023-03-25 22:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
2023-03-28 19:49:05 +02:00
|
|
|
w_conf "$@"
|
2023-03-25 22:34:21 +01:00
|
|
|
prep
|
|
|
|
scan
|
|
|
|
cache
|
|
|
|
print_menu "$@"
|
|
|
|
execute_program "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|