spmenu/scripts/spmenu_run

93 lines
3.7 KiB
Plaintext
Raw Normal View History

2023-01-20 23:17:30 +01:00
#!/bin/sh
2023-03-28 15:10:57 +02:00
[ -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
2023-03-28 13:14:22 +02:00
[ -z "$RUNLAUNCHER_ARGS" ] && RUNLAUNCHER_ARGS="--insert --hist-file $HISTORY $*"
2023-03-28 13:14:22 +02:00
check() {
[ ! -d "$HOME/.config/spmenu/run" ] && mkdir -p "$HOME/.config/spmenu/run"
if [ ! -f "$HOME/.config/spmenu/run/.first_run" ]; then
print_help
touch "$HOME/.config/spmenu/run/.first_run"
fi
}
path() {
printf "%s\n" "$PATH" | \
tr ':' '\n' | \
uniq | sed 's#$#/#' | \
xargs ls -lu --time-style=+%s 2>&1 | \
awk '/^(-|l)/ { print $6, $7 }' | \
sort -rn | \
cut -d' ' -f 2 2>&1
}
print_help() {
2023-03-28 15:10:57 +02:00
cat << EOF | $RUNLAUNCHER $RUNLAUNCHER_ARGS --lines 20 --columns 1 --normal --sgr1 "#FFFF00" --hide-cursor --no-allow-typing --no-color-items --prompt "" > /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.
To configure spmenu, you may also copy /usr/share/spmenu/example.Xresources to $HOME/.config/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)
$(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() {
[ -f "$HOME/.config/spmenu/run/config" ] && . "$HOME/.config/spmenu/run/config" && return
mkdir -p "$HOME/.config/spmenu/run"
cat << EOF > "$HOME/.config/spmenu/run/config"
# spmenu_run default configuration file
TERMINAL="\${TERMINAL:-st -e}" # Terminal
BROWSER="\${BROWSER:-xdg-open}" # Web browser
TORRENT="\${TORRENT:-qbittorrent}" # Torrent client
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
RUNLAUNCHER="\${RUNLAUNCHER:-spmenu}" # Run launcher to use
RUNLAUNCHER_ARGS="--insert --hist-file \$HISTORY \$*"
EOF
[ -f "$HOME/.config/spmenu/run/config" ] && . "$HOME/.config/spmenu/run/config" && return
}
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
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 "$dout" | sed "s/www //g")" & ;;
"torrent") $TORRENT "$(printf "$dout" | sed "s/magnet //g")" & ;;
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 "$@"
rm -f "$HOME/.cache/spmenu_run"
}
2023-01-20 23:17:30 +01:00
main "$@"