spmenu/scripts/spmenu_desktop

85 lines
2.7 KiB
Plaintext
Raw Normal View History

2023-03-25 22:34:21 +01:00
#!/bin/sh
# spmenu_desktop
#
# List desktop entries
[ -z "$desktop_dir" ] && desktop_dir="/usr/share/applications"
[ -z "$icon_dir" ] && icon_dir="/usr/share/icons/hicolor"
[ -z "$temporary_dir" ] && temporary_dir="$HOME/.config/spmenu/desktop/cache"
[ -z "$useimage" ] && useimage=true
[ -z "$RUNLAUNCHER" ] && RUNLAUNCHER=spmenu
[ -z "$RUNLAUNCHER_ARGS" ] && RUNLAUNCHER_ARGS="-l 20 -g 1 -is 200"
print_menu() { res="$(print_list | $RUNLAUNCHER $RUNLAUNCHER_ARGS "$@")"; cat /tmp/ls_desktop.log; }
prep() {
mkdir -p "$temporary_dir"
[ -f "$temporary_dir/../config" ] && . "$temporary_dir/../config"
}
scan() {
entry_c="$(find "$desktop_dir" -type f | wc -l)"
entry="$(find "$desktop_dir" -type f)"
cached_c="$(find "$temporary_dir" -type f | wc -l)"
cached="$(find "$temporary_dir" -type f)"
}
cache() {
[ "$cached_c" = "$entry_c" ] && return # we don't need to cache anything, it's already done
icons="$(find $icon_dir -type f)"
rm -rf "$temporary_dir/*.entry"
2023-03-25 23:49:52 +01:00
oname="$name"
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")"
icon_name="$(grep "Icon=" "$cur_file" | head -n 1 | sed "s/Icon=//g")"
2023-03-25 23:49:52 +01:00
echo "$icon_name" > /tmp/iconaasd
icon="$(printf "%s" "$icons" | grep "$icon_name[.]" | head -n 1)"
2023-03-25 22:34:21 +01:00
2023-03-25 23:49:52 +01:00
[ "$oname" = "$name" ] && continue
2023-03-25 22:34:21 +01:00
printf "%s\n%s\n%s\n" "$name" "$exec" "$icon" > "$temporary_dir/$(basename "$cur_file").entry"
done
}
print_list() {
rm -f /tmp/ls_desktop.log; touch /tmp/ls_desktop.log
for i in $(seq "$cached_c"); do
cur_file="$(printf "%s" "$cached" | sed "${i}q;d")"
[ ! -e "$cur_file" ] && printf "File '%s' does not exist. Skipping...\n" "$cur_file" >> /tmp/ls_desktop.log && continue
title="$(head -n 1 "$cur_file")"
exec="$(head -n 2 "$cur_file" | tail -n -1)"
icon="$(tail -n 1 "$cur_file")"
[ "$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() {
# TODO: clean up this
for i in $(seq "$cached_c"); do
cur_file="$(printf "%s" "$cached" | sed "${i}q;d")"
grep -q "$res" "$cur_file" || continue
exec="$(head -n 2 "$cur_file" | tail -n -1)"
break
done
$exec
return
}
main() {
prep
scan
cache
print_menu "$@"
execute_program "$@"
}
main "$@"