spmenu/scripts/spmenu_desktop

164 lines
6.6 KiB
Plaintext
Raw Normal View History

2023-03-25 22:34:21 +01:00
#!/bin/sh
# spmenu_desktop - list .desktop entries with the icon.
# NOTE: Please pull request if you have any possible improvements.
2023-04-07 18:18:56 +02:00
[ -z "$CONFDIR" ] && CONFDIR="${XDG_CONFIG_HOME:-$HOME/.config}"
[ -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="$CONFDIR/spmenu/desktop/cache"
[ -z "$USEIMAGE" ] && USEIMAGE=true
[ -z "$config" ] && config="$CONFDIR/spmenu/desktop/config"
[ -z "$cache" ] && cache=true
2023-03-25 22:34:21 +01:00
[ -z "$RUNLAUNCHER" ] && RUNLAUNCHER=spmenu
2023-04-07 18:18:56 +02:00
[ -z "$RUNLAUNCHER_ARGS" ] && RUNLAUNCHER_ARGS="--lines 20 --columns 1 --image-size 100 --image-gaps 20 \$*"
[ -z "$LOGFILE" ] && LOGFILE="/tmp/spmenu_desktop.log"
[ -z "$PREFIX" ] && PREFIX="/usr"
2023-03-25 22:34:21 +01:00
# functions
2023-04-07 18:18:56 +02:00
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)"; }
print_help() {
cat << EOF | $RUNLAUNCHER $RUNLAUNCHER_ARGS --lines 20 --columns 1 --normal --sgr1 "#FFFF00" --hide-cursor --no-allow-typing --no-color-items --hide-prompt --hide-powerline --hide-input --hide-right-arrow --hide-left-arrow --hide-mode --hide-match-count > /dev/null
Start typing in keywords to list out entries. Press Enter to select an entry. The selected entry will be run through a shell.
To set spmenu options, you modify \$RUNLAUNCHER_ARGS in $config. See 'spmenu --help' for a list of valid arguments to add to the variable.
To configure spmenu itself, you may copy ${DESTDIR}${PREFIX}/share/spmenu.conf to ~/.config/spmenu/spmenu.conf.
By default, spmenu_desktop will cache entries for speed reasons. You can find these entries in ~/.config/spmenu/desktop/cache.
If you make changes to .desktop files (not new entries, modified old entries), you need to clear the cache for the changes to appear. Simply delete the directory to do this.
- Type in '?' to show this help screen at any time.
$(printf '\033[0;31m')Note: This may also be displayed if you deleted your spmenu configuration directory.
EOF
}
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"
2023-04-07 18:18:56 +02:00
# spmenu_desktop default configuration file
#
# This is the configuration file for the desktop launcher spmenu comes with.
# It is not the configuration file for spmenu, see ~/.config/spmenu/spmenu.conf for that.
#
# spmenu_desktop runs function 'pre_func' before listing items, the output will be passed to spmenu. It also runs function 'post_func' after spawning spmenu.
# You may create those functions below. For 'post_func', the selected item is passed as an argument (\$1).
#
# For example, to implement a basic history file:
#
# post_func() {
# rm -f /tmp/spmenu_entryhist; printf "\$1\n" >> /tmp/spmenu_entryhist
# }
#
# You can use anything POSIX compliant shells support, as well as programs available on the system.
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="\$CONFDIR/spmenu/desktop/cache" # Directory used to store cache
USEIMAGE=true # Enable image support
2023-03-28 19:49:05 +02:00
RUNLAUNCHER="\${RUNLAUNCHER:-spmenu}"
RUNLAUNCHER_ARGS="\${RUNLAUNCHER_ARGS:- --lines 20 --columns 1 --image-size 100 --image-gaps 20}"
2023-04-07 18:18:56 +02:00
LOGFILE="/tmp/spmenu_desktop.log"
2023-03-28 19:49:05 +02:00
EOF
fi
[ -f "$config" ] && . "$config"
}
# cache it, this means some speed improvements
2023-03-25 22:34:21 +01:00
cache() {
2023-04-07 18:18:56 +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-04-07 18:18:56 +02:00
entry="$(find $DESKTOP_DIR -type f)"
icons="$(find $ICON_DIR -type f)"
2023-03-26 16:12:27 +02:00
# 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")"
2023-04-06 02:26:56 +02:00
exec="$(grep -v "TryExec" "$cur_file" | grep -m1 "Exec=" | sed "s/Exec=//g; s/%U//g; s/%F//g; s/%u//g")"
2023-03-25 22:34:21 +01:00
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")"
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-04-07 18:18:56 +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
# run scan() again
scan
2023-03-25 22:34:21 +01:00
}
print_list() {
2023-04-07 18:18:56 +02:00
command -v pre_func > /dev/null && pre_func
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
# current file
2023-04-07 18:18:56 +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
# 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
2023-03-28 02:54:47 +02:00
# print it all
2023-04-07 18:18:56 +02:00
[ "$USEIMAGE" = "true" ] && [ ! -e "$icon" ] && USEIMAGE=false && reenable=1
[ "$USEIMAGE" = "true" ] && printf "%s\t%s\n" "IMG:${icon}" "$title" || \
2023-03-25 22:34:21 +01:00
printf "%s\n" "$title"
2023-04-07 18:18:56 +02:00
[ "$reenable" = "1" ] && USEIMAGE=true
2023-03-25 22:34:21 +01:00
done
}
execute_program() {
2023-04-07 18:18:56 +02:00
[ "$res" = "?" ] && print_help && main "$@"
command -v post_func > /dev/null && post_func "$res"
[ -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-04-07 18:18:56 +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
# 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")"
2023-04-07 18:18:56 +02:00
printf "Current file: '%s'\n" "$cur_file" >> "$LOGFILE"
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
if [ -n "$exec" ]; then
/bin/sh -c "$exec"
else
2023-04-07 18:18:56 +02:00
[ -n "$res" ] && printf "No executable found. Try clearing cache." >> "$LOGFILE"
fi
2023-03-25 22:34:21 +01:00
return 0
2023-03-25 22:34:21 +01:00
}
2023-04-07 18:18:56 +02:00
check() {
[ ! -d "$CONFDIR/spmenu/desktop" ] && mkdir -p "$CONFDIR/spmenu/desktop"
if [ ! -f "$CONFDIR/spmenu/desktop/.first_run" ]; then
print_help
touch "$CONFDIR/spmenu/desktop/.first_run"
fi
}
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
2023-04-07 18:18:56 +02:00
check
2023-03-25 22:34:21 +01:00
scan
cache
print_menu "$@"
execute_program "$@"
}
main "$@"