massively speed up listing, need to fix running executable

This commit is contained in:
speedie 2023-04-18 16:19:39 +02:00
parent e724d54eb5
commit 7869c4d085

View file

@ -28,11 +28,11 @@ KEYWORDS="${KEYWORDS:-}"
DISPLAY_DUPLICATES="${DISPLAY_DUPLICATES:-false}" DISPLAY_DUPLICATES="${DISPLAY_DUPLICATES:-false}"
LS_ARGS="${LS_ARGS:- --color=always}" LS_ARGS="${LS_ARGS:- --color=always}"
USE_FULL_PATH="${USE_FULL_PATH:-false}" USE_FULL_PATH="${USE_FULL_PATH:-false}"
HELP_COLOR="${HELP_COLOR:-#FFFF00}"
DESKTOP_DIR="${DESKTOP_DIR:-${DESTDIR}${PREFIX}/share/applications}" DESKTOP_DIR="${DESKTOP_DIR:-${DESTDIR}${PREFIX}/share/applications}"
ICON_DIR="${ICON_DIR:-${DESTDIR}${PREFIX}/share/icons/hicolor ${DESTDIR}${PREFIX}/share/pixmaps}" ICON_DIR="${ICON_DIR:-${DESTDIR}${PREFIX}/share/icons/hicolor ${DESTDIR}${PREFIX}/share/pixmaps}"
TEMPORARY_DIR="${TEMPORARY_DIR:-$CONFDIR/spmenu/run/cache}" TEMPORARY_DIR="${TEMPORARY_DIR:-$CONFDIR/spmenu/run/cache}"
USEIMAGE="${USEIMAGE:-true}"
CACHE="${CACHE:-true}" CACHE="${CACHE:-true}"
LOGFILE="${LOGFILE:-/tmp/spmenu_run.log}" LOGFILE="${LOGFILE:-/tmp/spmenu_run.log}"
@ -79,7 +79,7 @@ path() {
} }
print_help() { print_help() {
cat << EOF | $RUNLAUNCHER $RUNLAUNCHER_HELP_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 cat << EOF | $RUNLAUNCHER $RUNLAUNCHER_HELP_ARGS --lines 20 --columns 1 --normal --sgr1 "$HELP_COLOR" --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. 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 pass arguments to 'spmenu_run' directly. See 'spmenu --help' for a list of valid arguments. To set spmenu options, you pass arguments to 'spmenu_run' directly. See 'spmenu --help' for a list of valid arguments.
To configure spmenu, you may also copy ${DESTDIR}${PREFIX}/share/spmenu/example.Xresources to $CONFDIR/spmenu/spmenurc and edit that. To configure spmenu, you may also copy ${DESTDIR}${PREFIX}/share/spmenu/example.Xresources to $CONFDIR/spmenu/spmenurc and edit that.
@ -113,8 +113,8 @@ EOF
} }
print_config() { print_config() {
[ -f "$CONFDIR/spmenu/run/config" ] && . "$CONFDIR/spmenu/run/config" && return [ -f "$CONFDIR/spmenu/run/config" ] && . "$CONFDIR/spmenu/run/config" && return
mkdir -p "$CONFDIR/spmenu/run" mkdir -p "$CONFDIR/spmenu/run"
cat << EOF > "$CONFDIR/spmenu/run/config" cat << EOF > "$CONFDIR/spmenu/run/config"
# spmenu_run default configuration file # spmenu_run default configuration file
# #
@ -176,12 +176,12 @@ KEYWORDS="" # Keywords that will be matched, needs to be in grep -E syntax.
STDOUT="false" # Print to stdout and exit (true/false) STDOUT="false" # Print to stdout and exit (true/false)
DISPLAY_DUPLICATES="false" # Display duplicates or not DISPLAY_DUPLICATES="false" # Display duplicates or not
DEFAULT_FEATURE="run" # spmenu_run default feature (run/fm/desktop/help) DEFAULT_FEATURE="run" # spmenu_run default feature (run/fm/desktop/help)
HELP_COLOR="#FFFF00"
# .desktop options # .desktop options
DESKTOP_DIR="\${DESTDIR}\${PREFIX}/share/applications" # Directories for .desktop entries DESKTOP_DIR="\${DESTDIR}\${PREFIX}/share/applications" # Directories for .desktop entries
ICON_DIR="\${DESTDIR}\${PREFIX}/share/icons/hicolor \${DESTDIR}\${PREFIX}/share/pixmaps" # Directories for icons defined in the entries ICON_DIR="\${DESTDIR}\${PREFIX}/share/icons/hicolor \${DESTDIR}\${PREFIX}/share/pixmaps" # Directories for icons defined in the entries
TEMPORARY_DIR="\$CONFDIR/spmenu/run/cache" # Directory used to store cached entries TEMPORARY_DIR="\$CONFDIR/spmenu/run/cache" # Directory used to store cached entries
USEIMAGE="true" # Enable images (true/false)
CACHE="true" # Cache entries (true/false) CACHE="true" # Cache entries (true/false)
LOGFILE="/tmp/spmenu_run.log" # Log file LOGFILE="/tmp/spmenu_run.log" # Log file
@ -247,7 +247,9 @@ exec_cmd() {
esac esac
} }
remove_arg() { args="$(printf "%s\n" "$args" | sed "s|$1||g")"; } remove_arg() {
args="$(printf "%s\n" "$args" | sed "s|$1||g")"
}
read_args() { read_args() {
function="${DEFAULT_FEATURE:-run}" # default functionality function="${DEFAULT_FEATURE:-run}" # default functionality
@ -256,7 +258,7 @@ read_args() {
argc="$(printf "%s\n" "$@" | wc -l)" argc="$(printf "%s\n" "$@" | wc -l)"
while true; do while true; do
i=$(($i+1)) i=$((i+1))
arg="$(printf "%s\n" "$args" | sed "${i}q;d")" arg="$(printf "%s\n" "$args" | sed "${i}q;d")"
narg="$(printf "%s\n" "$args" | sed "$((i+1))q;d")" narg="$(printf "%s\n" "$args" | sed "$((i+1))q;d")"
@ -348,7 +350,7 @@ prepare_dirnav() {
} }
print_desktop_help() { print_desktop_help() {
cat << EOF | $RUNLAUNCHER $RUNLAUNCHER_HELP_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 cat << EOF | $RUNLAUNCHER $RUNLAUNCHER_HELP_ARGS --lines 20 --columns 1 --normal --sgr1 "$HELP_COLOR" --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. 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 the config. See 'spmenu --help' for a list of valid arguments to add to the variable. To set spmenu options, you modify \$RUNLAUNCHER_ARGS in the 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. To configure spmenu itself, you may copy ${DESTDIR}${PREFIX}/share/spmenu.conf to ~/.config/spmenu/spmenu.conf.
@ -365,9 +367,9 @@ EOF
main_desktop() { main_desktop() {
print_menu() { print_menu() {
if [ "$DISPLAY_DUPLICATES" != "false" ]; then if [ "$DISPLAY_DUPLICATES" != "false" ]; then
res="$(print_list | $RUNLAUNCHER $RUNLAUNCHER_DESKTOP_ARGS)" menusel="$(print_list | $RUNLAUNCHER $RUNLAUNCHER_DESKTOP_ARGS)"
else else
res="$(print_list | uniq $UNIQ_ARGS | $RUNLAUNCHER $RUNLAUNCHER_DESKTOP_ARGS)" menusel="$(print_list | uniq $UNIQ_ARGS | $RUNLAUNCHER $RUNLAUNCHER_DESKTOP_ARGS)"
fi fi
} }
@ -376,10 +378,18 @@ main_desktop() {
# cache it, this means some speed improvements # cache it, this means some speed improvements
cache() { cache() {
scan # run initial scan
printf "Cached: %s\n" "$cached_c" >> "$LOGFILE" printf "Cached: %s\n" "$cached_c" >> "$LOGFILE"
printf "Entries: %s\n" "$entry_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 [ "$cached_c" = "$entry_c" ] && return # we don't need to cache anything, it's already done
[ -f "$TEMPORARY_DIR/../.title" ] && rm -f "$TEMPORARY_DIR/../.title"
[ -f "$TEMPORARY_DIR/../.icon" ] && rm -f "$TEMPORARY_DIR/../.icon"
[ -f "$TEMPORARY_DIR/../.exec" ] && rm -f "$TEMPORARY_DIR/../.exec"
[ -f "$TEMPORARY_DIR/../.file" ] && rm -f "$TEMPORARY_DIR/../.file"
# find # find
entry="$(find $DESKTOP_DIR -type f)" entry="$(find $DESKTOP_DIR -type f)"
icons="$(find $ICON_DIR -type f)" icons="$(find $ICON_DIR -type f)"
@ -405,13 +415,14 @@ main_desktop() {
print_list() { print_list() {
command -v desktop_pre_func > /dev/null && desktop_pre_func command -v desktop_pre_func > /dev/null && desktop_pre_func
# arrays containing entries # should we use cached files?
declare -a it_title [ -f "$TEMPORARY_DIR/../.title" ] && cfiles="true" || cfiles="false"
declare -a it_icon [ -f "$TEMPORARY_DIR/../.icon" ] && cfiles="true" || cfiles="false"
declare -a it_exec [ -f "$TEMPORARY_DIR/../.exec" ] && cfiles="true" || cfiles="false"
declare -a it_file [ -f "$TEMPORARY_DIR/../.file" ] && cfiles="true" || cfiles="false"
# print data from entries # print data from entries
if [ "$cfiles" = "false" ]; then
for i in $(seq "$cached_c"); do for i in $(seq "$cached_c"); do
# current file # 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 cur_file="$(printf "%s" "$cached" | sed "${i}q;d")" && [ ! -e "$cur_file" ] && printf "File '%s' does not exist. Skipping...\n" "$cur_file" >> "$LOGFILE" && continue
@ -422,39 +433,77 @@ main_desktop() {
it_exec[i]="$(head -n 2 "$cur_file" | tail -n 1 | sed "s/Executable://g")" it_exec[i]="$(head -n 2 "$cur_file" | tail -n 1 | sed "s/Executable://g")"
it_file[i]="$cur_file" it_file[i]="$cur_file"
[ -z "${it_title[i]}" ] && continue # no title isn't worth displaying, obviously printf "%s\n" "${it_title[i]}" >> "$TEMPORARY_DIR/../.title"
command -v "${it_exec[i]}" > /dev/null || continue # why bother if we can't execute it? printf "%s\n" "${it_icon[i]}" >> "$TEMPORARY_DIR/../.icon"
printf "%s\n" "${it_exec[i]}" >> "$TEMPORARY_DIR/../.exec"
# print it all printf "%s\n" "${it_file[i]}" >> "$TEMPORARY_DIR/../.file"
[ "$USEIMAGE" = "true" ] && [ ! -e "${it_icon[i]}" ] && USEIMAGE=false && reenable=1
[ "$USEIMAGE" = "true" ] && printf "%s\t%s\n" "IMG:${it_icon[i]}" "${it_title[i]}" || \
printf "%s\n" "${it_title[i]}"
[ "$reenable" = "1" ] && USEIMAGE=true
done done
print_list "$@"
else # we have entries, let's populate the arrays
i=1
# read title
while read -r p; do
it_title[i]="$p"
i=$((i+1))
done < "$TEMPORARY_DIR/../.title"; i=1
# read icon
while read -r p; do
it_icon[i]="$p"
i=$((i+1))
done < "$TEMPORARY_DIR/../.icon"; i=1
# read executable
while read -r p; do
it_exec[i]="$p"
i=$((i+1))
done < "$TEMPORARY_DIR/../.exec"; i=1
# read file
while read -r p; do
it_file[i]="$p"
i=$((i+1))
done < "$TEMPORARY_DIR/../.file"; i=1
# finally print all of it
for i in "${!it_title[@]}"; do
if [ -f "${it_icon[i]}" ] && [ -n "${it_title[i]}" ] && [ -n "${it_exec[i]}" ]; then
printf "%s\t%s\n" "IMG:${it_icon[i]}" "${it_title[i]}"
elif [ -n "${it_title[i]}" ] && [ -n "${it_exec[i]}" ]; then
printf "%s\n" "${it_title[i]}"
else
continue
fi
done
fi
} }
# execute the program
execute_program() { execute_program() {
[ "$res" = "?" ] && print_desktop_help && main "$@" [ "$menusel" = "?" ] && print_desktop_help && main "$@"
command -v desktop_post_func > /dev/null && desktop_post_func "$res" command -v desktop_post_func > /dev/null && desktop_post_func "$menusel"
[ -z "$res" ] && cached_c=0 || printf "User input: %s\n" "$res" >> "$LOGFILE" [ -z "$menusel" ] && return 1 \
for i in $(seq "$cached_c"); do || printf "User input: %s\n" "$menusel" >> "$LOGFILE"
# find the executable matching the selected name # find the executable matching the selected name
if [ "${it_title[i]}" = "${res}" ]; then for i in "${!it_title[@]}"; do
i=$((i+1))
if [ "${it_title[i]}" = "${menusel}" ]; then
exec="${it_exec[i]}" exec="${it_exec[i]}"
printf "Current file: '%s'\n" "${it_file[i]}" >> "$LOGFILE" printf "Current file: '%s'\n" "${it_file[i]}" >> "$LOGFILE"
break; break;
else
exec=""
continue;
fi fi
[ -n "$exec" ] && break;
done done
# finally run the program # finally run the program
if [ -n "$exec" ]; then if [ -n "$exec" ]; then
/bin/sh -c "$exec" /bin/sh -c "$exec"
else else
[ -n "$res" ] && printf "No executable found. Try clearing cache." >> "$LOGFILE" printf "No executable found. Try clearing cache." >> "$LOGFILE"
fi fi
return 0 return 0
@ -462,7 +511,6 @@ main_desktop() {
prep prep
check check
scan
cache cache
print_menu "$@" print_menu "$@"
execute_program "$@" execute_program "$@"
@ -487,7 +535,14 @@ main() {
;; ;;
"fm") prepare_dirnav "$args" "fm") prepare_dirnav "$args"
;; ;;
"desktop") main_desktop "$args" "desktop")
# arrays containing entries
declare -a it_title
declare -a it_icon
declare -a it_exec
declare -a it_file
main_desktop "$args"
;; ;;
"help") "help")
print_cli_help print_cli_help