spmenu/scripts/spmenu_run
2023-04-12 23:37:12 +02:00

327 lines
11 KiB
Bash
Executable file

#!/bin/sh
# spmenu_run
# Run launcher for spmenu
# Set basic variables, in case the config isn't valid, env variables and config file can override these
CONFDIR="${CONFDIR:-${XDG_CONFIG_HOME:-$HOME/.config}}"
TERMINAL="${TERMINAL:-st -e}"
BROWSER="${BROWSER:-xdg-open}"
TORRENT="${TORRENT:-qbittorrent}"
PDF_READER="${PDF_READER:-zathura}"
EDITOR="${EDITOR:-nvim}"
PLAYER="${PLAYER:-mpv}"
GENERIC="${GENERIC:-$EDITOR}"
WEB_GREP="${WEB_GREP:-http:|https:|www[.]}"
MAGNET_GREP="${MAGNET_GREP:-magnet:?}"
HISTORY="${HISTORY:-${XDG_CACHE_HOME:-$HOME/.cache/}/spmenu_run.hist}"
RUNLAUNCHER="${RUNLAUNCHER:-spmenu}"
PREFIX="${PREFIX:-/usr}"
DESTDIR="${DESTDIR:-}"
STDOUT="false"
SORT_BY_NUMBER="${SORT_BY_NUMBER:-true}"
SORT_IN_REVERSE="${SORT_IN_REVERSE:-true}"
SORT_BY_RECENT="${SORT_BY_RECENT:-false}"
SORT_ARGS="${SORT_ARGS:-}"
UNIQ_ARGS="${UNIQ_ARGS:-}"
HIDDEN_KEYWORDS="${HIDDEN_KEYWORDS:-spmenu}"
KEYWORDS="${KEYWORDS:-}"
DISPLAY_DUPLICATES="${DISPLAY_DUPLICATES:-false}"
LS_ARGS="${LS_ARGS:- --color=always}"
check() {
[ ! -d "$CONFDIR/spmenu/run" ] && mkdir -p "$CONFDIR/spmenu/run"
if [ ! -f "$CONFDIR/spmenu/run/.first_run" ]; then
print_help
touch "$CONFDIR/spmenu/run/.first_run"
fi
}
path() {
[ "$SORT_BY_NUMBER" != "false" ] && NUMBERSORTARG="-n"
[ "$SORT_IN_REVERSE" != "false" ] && REVERSESORTARG="-r"
SORTARGS="$NUMBERSORTARG $REVERSESORTARG $SORT_ARGS"
[ -z "$HIDDEN_KEYWORDS" ] && HIDDEN_KEYWORDS="NULL_ENTRY"
print_menu() {
print() {
printf "%s\n" "$PATH" | \
tr ':' '\n' | \
sed 's#$#/#' | \
xargs ls -lu --time-style=+%s 2>&1 | \
grep -vE "$HIDDEN_KEYWORDS" | \
grep -E "$KEYWORDS"
}
if [ "$SORT_BY_RECENT" != "false" ]; then
print | awk '/^(-|l)/ { print $6, $7 }' | sort $SORTARGS | cut -d' ' -f 2 2>&1
else
print | awk '/^(-|l)/ { print $7 }' | sort $SORTARGS | cut -d' ' -f 2 2>&1
fi
}
# uniq
if [ "$DISPLAY_DUPLICATES" != "false" ]; then
print_menu
else
print_menu | uniq $UNIQ_ARGS
fi
command -v run_pre_func && run_pre_func
}
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
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 configure spmenu, you may also copy ${DESTDIR}${PREFIX}/share/spmenu/example.Xresources to $CONFDIR/spmenu/spmenurc and edit that.
- Type in '?' to show this help screen at any time.
- If the entry selected starts with 'www', it will instead be treated as a link and spawned in a web browser (\$BROWSER)
- If the entry selected starts with 'magnet', it will instead be treated as a magnet link and spawned in a torrent client (\$TORRENT)
- If the entry selected starts with '?' followed by a valid command, it will be opened as a man page in spmenu.
- If the entry starts with '#' followed by a valid command, it will be opened in the defined terminal emulator.
$(printf '\033[0;31m')Note: This may also be displayed if you deleted your spmenu configuration directory.
EOF
}
print_cli_help() {
cat << EOF
spmenu_run - Run launcher for spmenu
spmenu -x, --run List entries in \$PATH.
spmenu -f, --fm <dir> List files and directories in <dir>.
spmenu -h, --help Print this help.
spmenu -o, --stdout Print to standard input and do not execute the selected item.
spmenu -no, --no-stdout Don't print to standard input, execute the selected item.
EOF
}
print_config() {
[ -f "$CONFDIR/spmenu/run/config" ] && . "$CONFDIR/spmenu/run/config" && return
mkdir -p "$CONFDIR/spmenu/run"
cat << EOF > "$CONFDIR/spmenu/run/config"
# spmenu_run default configuration file
#
# This is the configuration file for the run launcher spmenu comes with.
# It is not the configuration file for spmenu, see ~/.config/spmenu/spmenu.conf for that.
#
# spmenu_run also runs these functions:
#
# 'run_pre_func' before spawning spmenu.
# 'run_post_func' after spawning spmenu, selected item passed as an argument.
# 'fm_pre_func' before spawning spmenu.
# 'fm_post_func' after spawning spmenu, selected item passed as an argument.
# 'fm_pre_list_func' right before listing out files.
# 'fm_post_list_func' right after listing out files.
# 'read_man' when reading a man page, selected item passed as an argument.
#
# You may create those functions below.
#
# For example, to implement a basic history file:
#
# run_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.
# misc software
TERMINAL="\${TERMINAL:-st -e}" # Terminal commands are spawned in
BROWSER="\${BROWSER:-xdg-open}" # Web browser, for URLs
TORRENT="\${TORRENT:-qbittorrent}" # Torrent client, for magnet links
PDF_READER="\${PDF_READER:-zathura}" # PDF reader, for file management
EDITOR="\${EDITOR:-nvim}" # Editor, used to open documents
PLAYER="\${PLAYER:-mpv}" # Player, used to play audio/video
GENERIC="\${GENERIC:-\$EDITOR}" # Generic, used to open unknown files
WEB_GREP="http:|https:|www[.]" # Needs to be in grep -E syntax
MAGNET_GREP="magnet:?" # Needs to be in grep -E syntax
HISTORY="\${XDG_CACHE_HOME:-\$HOME/.cache/}/spmenu_run.hist" # History file, spmenu (meaning your user) must have permission to read and write to it.
# run launcher options
RUNLAUNCHER="\${RUNLAUNCHER:-spmenu}" # Run launcher to use
RUNLAUNCHER_RUN_ARGS="--insert --hist-file \$HISTORY \$args" # Arguments passed to \$RUNLAUNCHER when using the run launcher
RUNLAUNCHER_FM_ARGS="--insert --hist-file \$HISTORY \$args" # Arguments passed to \$RUNLAUNCHER when using the file manager
RUNLAUNCHER_HELP_ARGS="--insert --hist-file \$HISTORY \$args" # Arguments passed to \$RUNLAUNCHER when using the help
# sorting
SORT_BY_NUMBER="true" # Sort by numbers
SORT_IN_REVERSE="true" # Sort in reverse
SORT_BY_RECENT="false" # Sort by recent
SORT_ARGS="" # Extra arguments passed to the sort command.
# keywords
HIDDEN_KEYWORDS="spmenu" # Keywords that will be ignored, needs to be in grep -vE syntax.
KEYWORDS="" # Keywords that will be matched, needs to be in grep -E syntax.
# misc
STDOUT="false" # Print to stdout and exit (true/false)
DISPLAY_DUPLICATES="false" # Display duplicates or not
DEFAULT_FEATURE="run" # spmenu_run default feature (run/fm/help)
# file management
DEFAULT_DIRECTORY="\$(pwd)" # Directory to start -fm if none is specified.
LS_ARGS="\${LS_ARGS:- --color=always}" # Arguments passed to /bin/ls
# function to read the man page in spmenu
read_man() { man "\$1" | col -b | \${RUNLAUNCHER:-spmenu} --lines 40 --columns 1 -p "man \$1"; }
EOF
[ -f "$CONFDIR/spmenu/run/config" ] && . "$CONFDIR/spmenu/run/config" && return
}
parse() {
dout="$(path | sed "s/\&/\&amp;/g" | $RUNLAUNCHER $RUNLAUNCHER_RUN_ARGS)"
# parse
case "$(printf '%c' "$dout")" in
"#") EXEC="term" ;;
"?") EXEC="man" ;;
esac
case "$(printf "%s" "$dout" | awk '{ print $1 }')" in
"magnet") EXEC=torrent ;;
"www") EXEC=web ;;
"?") print_help && main && return ;;
esac
# check for keywords
printf "%s" "$dout" | grep -qE "$WEB_GREP" && EXEC=web
printf "%s" "$dout" | grep -qE "$MAGNET_GREP" && EXEC=torrent
}
exec_cmd() {
[ -z "$EXEC" ] && EXEC=shell
[ "$STDOUT" != "false" ] && printf "%s\n" "$dout" && exit 1
command -v run_post_func > /dev/null && run_post_func "$dout"
read_nman() {
$TERMINAL -e man "$1"
}
case "$EXEC" in
"shell") printf "%s" "$dout" | sed "s/#//g" | ${SHELL:-"/bin/sh"} & ;;
"term") $TERMINAL -e "$(printf "%s" "$dout" | sed "s/#//g")" & ;;
"web") $BROWSER "$(printf "%s" "$dout" | sed "s/www //g")" & ;;
"torrent") $TORRENT "$(printf "%s" "$dout" | sed "s/magnet //g")" & ;;
"man") exec="$(printf "%s" "$dout" | sed "s/?//g")"
[ -x "$(command -v "$exec")" ] || return
if [ "$(command -v read_man)" ]; then
read_man "$exec"
return
else
read_nman "$exec"
return
fi
;;
esac
}
remove_arg() { args="$(printf "%s\n" "$args" | sed "s|$1||g")"; }
read_args() {
function="${DEFAULT_FEATURE:-run}" # default functionality
dir="${DEFAULT_DIRECTORY:-$(pwd)}" # default directory
args="$(printf "%s\n" "$@")"
argc="$(printf "%s\n" "$@" | wc -l)"
while true; do
i=$(($i+1))
arg="$(printf "%s\n" "$args" | sed "${i}q;d")"
narg="$(printf "%s\n" "$args" | sed "$((i+1))q;d")"
case "$arg" in
-x|-run|--run) remove_arg "$arg" && function=run ;;
-o|-stdout|--stdout) remove_arg "$arg" && STDOUT=true ;;
-no|--no-stdout|--no-stdout) remove_arg "$arg" && STDOUT=false ;;
-f|-fm|--fm) remove_arg "$arg"
[ -d "$narg" ] && dir="$narg" && remove_arg "$narg"
function=fm ;;
-h|--help) remove_arg "$arg" && function=help ;;
esac
[ "$argc" = "$i" ] && break
done
}
exec_file() {
[ "$STDOUT" != "false" ] && printf "%s\n" "$1" && exit 0
command -v fm_post_func > /dev/null && fm_post_func "$1"
# some default basic parsing
case "$1" in
*.html|*.htm) $BROWSER "$1" ;;
*.pdf) $PDF_READER "$1" ;;
*.flac|*.mp3|*.wav|*.ogg) $PLAYER "$1" ;;
*.mp4|*.mov|*.mkv) $PLAYER "$1" ;;
*)
if [ -x "$1" ]; then
$TERMINAL -e "$1"
else
$GENERIC "$1"
fi
;;
esac
}
prepare_dirnav() {
[ ! -d "$dir" ] && return 1
listing() {
command -v fm_pre_list_func > /dev/null && fm_pre_list_func
ls $LS_ARGS # this allows us SGR colors
printf "..\n"
command -v fm_post_list_func > /dev/null && fm_post_list_func
}
command -v fm_pre_func > /dev/null && fm_pre_func
dir="$(listing | $RUNLAUNCHER $RUNLAUNCHER_FM_ARGS | sed -e 's/\x1b\[[0-9;]*m//g')"
case "$dir" in
*)
if [ -d "$dir" ]; then
cd "$dir" || printf "Invalid directory.. somehow\n"
dir="$(pwd)"
prepare_dirnav
elif [ -f "$dir" ]; then
exec_file "$dir" && return 0
return 1
else
return 1
fi
;;
esac
}
main() {
print_config "$args"
read_args "$@"
check "$args"
# some run launcher args
RUNLAUNCHER_FM_ARGS="${RUNLAUNCHER_FM_ARGS:- --insert --hist-file $HISTORY $args}"
RUNLAUNCHER_RUN_ARGS="${RUNLAUNCHER_RUN_ARGS:- --insert --hist-file $HISTORY $args}"
RUNLAUNCHER_HELP_ARGS="${RUNLAUNCHER_HELP_ARGS:- --insert --hist-file $HISTORY $args}"
# $PATH listing
case "$function" in
"run")
parse "$args"
exec_cmd "$args"
;;
"fm") prepare_dirnav "$args"
;;
"help")
print_cli_help
exit 0
;;
*)
printf "Undefined function: '%s'\n" "$function"
exit 1
;;
esac
}
main "$@"