spmenu/scripts/spmenu_run

148 lines
6.1 KiB
Plaintext
Raw Normal View History

2023-01-20 23:17:30 +01:00
#!/bin/sh
2023-04-07 18:18:56 +02:00
[ -z "$CONFDIR" ] && CONFDIR="${XDG_CONFIG_HOME:-$HOME/.config}"
[ -z "$TERMINAL" ] && TERMINAL="st -e"
[ -z "$BROWSER" ] && BROWSER="xdg-open"
[ -z "$TORRENT" ] && TORRENT="qbittorrent"
[ -z "$WEB_GREP" ] && WEB_GREP="http:|https:|www[.]"
[ -z "$MAGNET_GREP" ] && MAGNET_GREP="magnet:?"
[ -z "$HISTORY" ] && HISTORY="${XDG_CACHE_HOME:-$HOME/.cache/}/spmenu_run.hist"
[ -z "$RUNLAUNCHER" ] && RUNLAUNCHER=spmenu
[ -z "$RUNLAUNCHER_ARGS" ] && RUNLAUNCHER_ARGS="--insert --hist-file $HISTORY $*"
[ -z "$PREFIX" ] && PREFIX="/usr"
[ -z "$SORT_BY_NUMBER" ] && SORT_BY_NUMBER="true"
[ -z "$SORT_IN_REVERSE" ] && SORT_IN_REVERSE="true"
[ -z "$SORT_BY_RECENT" ] && SORT_BY_RECENT="false"
[ -z "$SORT_ARGS" ] && SORT_ARGS=""
[ -z "$HIDDEN_KEYWORDS" ] && HIDDEN_KEYWORDS="spmenu"
[ -z "$KEYWORDS" ] && KEYWORDS=""
[ -z "$DISPLAY_DUPLICATES" ] && DISPLAY_DUPLICATES="false"
2023-03-28 13:14:22 +02:00
check() {
2023-04-07 18:18:56 +02:00
[ ! -d "$CONFDIR/spmenu/run" ] && mkdir -p "$CONFDIR/spmenu/run"
if [ ! -f "$CONFDIR/spmenu/run/.first_run" ]; then
2023-03-28 13:14:22 +02:00
print_help
2023-04-07 18:18:56 +02:00
touch "$CONFDIR/spmenu/run/.first_run"
2023-03-28 13:14:22 +02:00
fi
}
path() {
2023-04-06 01:59:14 +02:00
[ "$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
2023-04-06 01:59:14 +02:00
}
# uniq
if [ "$DISPLAY_DUPLICATES" != "false" ]; then
print_menu
2023-04-06 01:59:14 +02:00
else
print_menu | uniq
2023-04-06 01:59:14 +02:00
fi
command -v pre_func && pre_func
2023-03-28 13:14:22 +02:00
}
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
2023-03-28 13:14:22 +02:00
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.
2023-04-07 18:18:56 +02:00
To configure spmenu, you may also copy ${DESTDIR}${PREFIX}/share/spmenu/example.Xresources to $CONFDIR/spmenu/spmenurc and edit that.
2023-03-28 13:14:22 +02:00
- 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)
$(printf '\033[0;31m')Note: This may also be displayed if you deleted your spmenu configuration directory.
EOF
}
2023-01-20 23:17:30 +01:00
2023-03-28 15:10:57 +02:00
print_config() {
2023-04-07 18:18:56 +02:00
[ -f "$CONFDIR/spmenu/run/config" ] && . "$CONFDIR/spmenu/run/config" && return
mkdir -p "$CONFDIR/spmenu/run"
cat << EOF > "$CONFDIR/spmenu/run/config"
2023-03-28 15:10:57 +02:00
# 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 runs function 'pre_func' before spawning spmenu, and '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.
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
2023-03-28 15:10:57 +02:00
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.
2023-03-28 15:10:57 +02:00
RUNLAUNCHER="\${RUNLAUNCHER:-spmenu}" # Run launcher to use
RUNLAUNCHER_ARGS="--insert --hist-file \$HISTORY \$*" # Arguments passed to \$RUNLAUNCHER
2023-04-06 01:59:14 +02:00
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.
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.
DISPLAY_DUPLICATES="false" # Display duplicates or not
2023-03-28 15:10:57 +02:00
EOF
2023-04-07 18:18:56 +02:00
[ -f "$CONFDIR/spmenu/run/config" ] && . "$CONFDIR/spmenu/run/config" && return
2023-03-28 15:10:57 +02:00
}
parse() {
2023-03-28 13:14:22 +02:00
dout="$(path | sed "s/\&/\&amp;/g" | $RUNLAUNCHER $RUNLAUNCHER_ARGS)"
2023-01-20 23:17:30 +01:00
# parse
2023-03-28 15:10:57 +02:00
[ "$(printf '%c' "$dout")" = "#" ] && EXEC=term
[ "$(printf "$dout" | awk '{ print $1 }')" = "magnet" ] && EXEC=torrent
[ "$(printf "$dout" | awk '{ print $1 }')" = "www" ] && EXEC=web
2023-03-28 13:14:22 +02:00
[ "$(printf "$dout" | awk '{ print $1 }')" = "?" ] && print_help && main && return
2023-03-28 15:10:57 +02:00
# check for keywords
printf "%s" "$dout" | grep -qE "$WEB_GREP" && EXEC=web
printf "%s" "$dout" | grep -qE "$MAGNET_GREP" && EXEC=torrent
}
2023-03-28 15:10:57 +02:00
exec_cmd() {
[ -z "$EXEC" ] && EXEC=shell
2023-04-07 01:06:25 +02:00
command -v post_func > /dev/null && post_func "$dout"
2023-03-28 15:10:57 +02:00
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")" & ;;
2023-03-28 15:10:57 +02:00
esac
2023-01-20 23:17:30 +01:00
}
main() {
2023-03-28 13:14:22 +02:00
check "$@"
2023-03-28 15:10:57 +02:00
print_config "$@"
parse "$@"
2023-03-28 15:10:57 +02:00
exec_cmd "$@"
}
2023-01-20 23:17:30 +01:00
main "$@"