From cf1b79008bf945dc925cb4699373214c90d2d420 Mon Sep 17 00:00:00 2001 From: speedie Date: Tue, 28 Mar 2023 15:10:57 +0200 Subject: [PATCH] upgrade run launcher significantly --- docs/docs.md | 6 +++++ docs/example.Xresources | 1 + libs/argv.c | 6 +++++ libs/draw.c | 3 +++ libs/xresources.h | 1 + options.h | 3 ++- scripts/spmenu_run | 56 ++++++++++++++++++++++++++++++----------- spmenu.1 | 8 +++++- 8 files changed, 68 insertions(+), 16 deletions(-) diff --git a/docs/docs.md b/docs/docs.md index 67aac07..4d68db0 100644 --- a/docs/docs.md +++ b/docs/docs.md @@ -82,6 +82,12 @@ You may use long, descriptive arguments or the shorter arguments. `-nip, --no-indent ` : Don't indent items to prompt width +`-ci, --color-items ` +: Color items + +`-nci, --no-color-items ` +: Don't color items + `-a, --alpha ` : Enable alpha diff --git a/docs/example.Xresources b/docs/example.Xresources index 5808b51..87bfbf1 100644 --- a/docs/example.Xresources +++ b/docs/example.Xresources @@ -86,6 +86,7 @@ spmenu.class: spmenu spmenu.fast: 0 spmenu.managed: 0 spmenu.mon: -1 +spmenu.coloritems: 1 !! Image spmenu.imagewidth: 86 diff --git a/libs/argv.c b/libs/argv.c index d25a3a0..30772d1 100644 --- a/libs/argv.c +++ b/libs/argv.c @@ -134,6 +134,10 @@ readargs(int argc, char *argv[]) indentitems = 1; } else if (!strcmp(argv[i], "-nip") || (!strcmp(argv[i], "--no-indent"))) { // don't indent to prompt width indentitems = 0; + } else if (!strcmp(argv[i], "-ci") || (!strcmp(argv[i], "--color-items"))) { // color items + coloritems = 1; + } else if (!strcmp(argv[i], "-nci") || (!strcmp(argv[i], "--no-color-items"))) { // don't color items + coloritems = 0; } else if (i + 1 == argc) { // any of the arguments we checked first if ((!strcmp(argv[i], "-xrdb") @@ -358,6 +362,8 @@ usage(void) "spmenu -p, --prompt Set spmenu prompt text to \n" "spmenu -ip, --indent Indent items to prompt width\n" "spmenu -nip, --no-indent Don't indent items to prompt width\n" + "spmenu -ci, --color-items Color items\n" + "spmenu -nci, --no-color-items Don't color items\n" "spmenu -a, --alpha Enable alpha\n" "spmenu -na, --no-alpha Disable alpha\n" "spmenu -tp, --allow-typing Allow the user to type\n" diff --git a/libs/draw.c b/libs/draw.c index e103dae..3bd86c2 100644 --- a/libs/draw.c +++ b/libs/draw.c @@ -64,6 +64,9 @@ drawitemtext(struct item *item, int x, int y, int w) memcpy(scm, scheme[SchemeItemNormPri], sizeof(scm)); } + // don't color + if (!coloritems) memcpy(scm, scheme[SchemeItemNorm], sizeof(scm)); + drw_setscheme(drw, scm); // set scheme // parse item text diff --git a/libs/xresources.h b/libs/xresources.h index 2f2bb61..5b138f0 100644 --- a/libs/xresources.h +++ b/libs/xresources.h @@ -94,6 +94,7 @@ ResourcePref resources[] = { { "color14", STRING, &col_sgrcolor14 }, { "color15", STRING, &col_sgrcolor15 }, + { "coloritems", INTEGER, &coloritems }, { "menuposition", INTEGER, &menuposition }, { "menupaddingv", INTEGER, &menupaddingv }, { "menupaddingh", INTEGER, &menupaddingh }, diff --git a/options.h b/options.h index 3cb5e97..af22169 100644 --- a/options.h +++ b/options.h @@ -160,4 +160,5 @@ static int pango_password = 0; /* Enable support for pango markup f #define borderalpha opaque /* Border alpha */ /* Misc */ -static char worddelimiters[] = " "; /* Word delimiters, " " is default. */ +static int coloritems = 1; /* Color items or not */ +static char worddelimiters[] = " "; /* Word delimiters used for keybinds that change words, " " is default. */ diff --git a/scripts/spmenu_run b/scripts/spmenu_run index eb6004c..3e20464 100755 --- a/scripts/spmenu_run +++ b/scripts/spmenu_run @@ -1,9 +1,11 @@ #!/bin/sh -[ -z "$TERMINAL" ] && TERMINAL="st -e" -[ -z "$BROWSER" ] && BROWSER="xdg-open" -[ -z "$TORRENT" ] && TORRENT="qbittorrent" -[ -z "$HISTORY" ] && HISTORY="${XDG_CACHE_HOME:-$HOME/.cache/}/spmenu_run.hist" -[ -z "$RUNLAUNCHER" ] && RUNLAUNCHER=spmenu +[ -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 $*" check() { @@ -25,7 +27,7 @@ path() { } print_help() { -cat << EOF | $RUNLAUNCHER $RUNLAUNCHER_ARGS --lines 20 --columns 1 --normal --sgr1 "#FFFF00" --hide-cursor --no-allow-typing > /dev/null +cat << EOF | $RUNLAUNCHER $RUNLAUNCHER_ARGS --lines 20 --columns 1 --normal --sgr1 "#FFFF00" --hide-cursor --no-allow-typing --no-color-items --prompt "" > /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 /usr/share/spmenu/example.Xresources to $HOME/.config/spmenu/spmenurc and edit that. @@ -38,26 +40,52 @@ $(printf '\033[0;31m')Note: This may also be displayed if you deleted your spmen EOF } -# run spmenu and parse it +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() { dout="$(path | sed "s/\&/\&/g" | $RUNLAUNCHER $RUNLAUNCHER_ARGS)" # parse - [ "$(printf '%c' "$dout")" = "#" ] && RUN_ARG="$TERMINAL" - [ "$(printf "$dout" | awk '{ print $1 }')" = "magnet" ] && "$TORRENT" "$(printf "$dout" | sed "s/magnet //")" - [ "$(printf "$dout" | awk '{ print $1 }')" = "www" ] && "$BROWSER" "$(printf "$dout" | sed "s/www //")" + [ "$(printf '%c' "$dout")" = "#" ] && EXEC=term + [ "$(printf "$dout" | awk '{ print $1 }')" = "magnet" ] && EXEC=torrent + [ "$(printf "$dout" | awk '{ print $1 }')" = "www" ] && EXEC=web [ "$(printf "$dout" | awk '{ print $1 }')" = "?" ] && print_help && main && return - # terminal - [ -z "$RUN_ARG" ] && \ - printf "%s" "$dout" | sed "s/#//g" | ${SHELL:-"/bin/sh"} & + # check for keywords + printf "%s" "$dout" | grep -qE "$WEB_GREP" && EXEC=web + printf "%s" "$dout" | grep -qE "$MAGNET_GREP" && EXEC=torrent +} - [ "$RUN_ARG" = "$TERMINAL" ] && $TERMINAL -e "$(printf "%s" "$dout" | sed "s/#//g")"; return +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 } main() { check "$@" + print_config "$@" parse "$@" + exec_cmd "$@" rm -f "$HOME/.cache/spmenu_run" } diff --git a/spmenu.1 b/spmenu.1 index 4aa35c5..95be3e6 100644 --- a/spmenu.1 +++ b/spmenu.1 @@ -1,4 +1,4 @@ -.\" Automatically generated by Pandoc 3.1 +.\" Automatically generated by Pandoc 3.0.1 .\" .\" Define V font for inline verbatim, using C font in formats .\" that render this, and otherwise B font. @@ -98,6 +98,12 @@ Indent items to prompt width \f[V]-nip, --no-indent\f[R] Don\[cq]t indent items to prompt width .TP +\f[V]-ci, --color-items\f[R] +Color items +.TP +\f[V]-nci, --no-color-items\f[R] +Don\[cq]t color items +.TP \f[V]-a, --alpha\f[R] Enable alpha .TP