diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-05-15 18:44:40 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-05-15 18:48:14 +0200 |
commit | 9244192e2822b4e764b5670d8e00ec1f1a3792a0 (patch) | |
tree | 17e82ffb412abec1b75c1d5810cd55ee4e86a8cb |
Initial import
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | README.md | 20 | ||||
-rw-r--r-- | notmuch-sel-mid | 162 |
2 files changed, 182 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..0d80753 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# notmuch-scripts + +Collection of scripts for working with notmuch. Feel free to use, contribute, +share under terms of GPL2.0 only. + + +## notmuch-select-message-i + +Fuzzy select a message id. Usecase: + +```bash +git format-patch --in-reply-to=$(notmuch-sel-mid) +``` + + +## License + +GPL 2.0 only +(c) Matthias Beyer + diff --git a/notmuch-sel-mid b/notmuch-sel-mid new file mode 100644 index 0000000..92b9d54 --- /dev/null +++ b/notmuch-sel-mid @@ -0,0 +1,162 @@ +#!/usr/bin/env bash + +#### +# +# BEGIN argbash.io generated +# +#### + +die() +{ + local _ret="${2:-1}" + test "${_PRINT_HELP:-no}" = yes && print_help >&2 + echo "$1" >&2 + exit "${_ret}" +} + + +begins_with_short_option() +{ + local first_option all_short_options='vdmh' + first_option="${1:0:1}" + test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0 +} + +# THE DEFAULTS INITIALIZATION - POSITIONALS +_positionals=() +_arg_term=("tag:inbox") +# THE DEFAULTS INITIALIZATION - OPTIONALS +_arg_debug="off" +_arg_matcher="fzf" + + +print_help() +{ + printf '%s\n' "Fuzzy-select message-id from a set of messages" + printf 'Usage: %s [-d|--(no-)debug] [-m|--matcher <arg>] [-h|--help] [<term-1>] ... [<term-n>] ...\n' "$0" + printf '\t%s\n' "<term>: Searchterm for initial notmuch-search (defaults for <term> to <term> respectively: 'tag:inbox' and '...')" + printf '\t%s\n' "-d, --debug, --no-debug: print debug information (off by default)" + printf '\t%s\n' "-m, --matcher: Select fuzzy matcher (supported: fzf, skim (default: 'fzf')" + printf '\t%s\n' "-h, --help: Prints help" +} + + +parse_commandline() +{ + _positionals_count=0 + while test $# -gt 0 + do + _key="$1" + case "$_key" in + -d|--no-debug|--debug) + _arg_debug="on" + test "${1:0:5}" = "--no-" && _arg_debug="off" + ;; + -d*) + _arg_debug="on" + _next="${_key##-d}" + if test -n "$_next" -a "$_next" != "$_key" + then + { begins_with_short_option "$_next" && shift && set -- "-d" "-${_next}" "$@"; } || die "The short option '$_key' can't be decomposed to ${_key:0:2} and -${_key:2}, because ${_key:0:2} doesn't accept value and '-${_key:2:1}' doesn't correspond to a short option." + fi + ;; + -m|--matcher) + test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 + _arg_matcher="$2" + shift + ;; + --matcher=*) + _arg_matcher="${_key##--matcher=}" + ;; + -m*) + _arg_matcher="${_key##-m}" + ;; + -h|--help) + print_help + exit 0 + ;; + -h*) + print_help + exit 0 + ;; + *) + _last_positional="$1" + _positionals+=("$_last_positional") + _positionals_count=$((_positionals_count + 1)) + ;; + esac + shift + done +} + + +assign_positional_args() +{ + local _positional_name _shift_for=$1 + _positional_names="" + _our_args=$((${#_positionals[@]} - 0)) + for ((ii = 0; ii < _our_args; ii++)) + do + _positional_names="$_positional_names _arg_term[$((ii + 0))]" + done + + shift "$_shift_for" + for _positional_name in ${_positional_names} + do + test $# -gt 0 || break + eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1 + shift + done +} + +parse_commandline "$@" +assign_positional_args 1 "${_positionals[@]}" + + +#### +# +# END argbash.io generated +# +#### + +COLOR_OFF='\e[0m' # Text Reset +RED='\e[0;31m' # Red +GREEN='\e[0;32m' # Green +YELLOW='\e[0;33m' # Yellow +CYAN='\e[0;36m' # Cyan + + +DEBUG="${_arg_debug}" +MATCHER="${_arg_matcher}" +TERMS="${_arg_term[*]}" + +abort() { + echo -e "[${RED}ERROR${COLOR_OFF}] $*" >&2 + exit 1 +} + +debug() { + [[ $DEBUG ]] && echo -e "[${YELLOW}DEBUG${COLOR_OFF}] ${CYAN}$*${COLOR_OFF}" >&2 +} + +debug "Starting..." +debug "DEBUG = ${DEBUG}" +debug "VERBOSE = ${VERBOSE}" +debug "MATCHER = ${MATCHER}" +debug "TERMS = ${TERMS}" + +[[ "${MATCHER}" == "sk" ]] || [[ "${MATCHER}" == "fzf" ]] || { + abort "${MATCHER} not supported" +} + +[[ -z "${TERMS}" ]] && abort "TERMS ('${TERMS}') cannot be empty" + +matcher_select() { + "$MATCHER" --preview "notmuch search --output=files {} | xargs -n 1 cat" +} + +notmuch search --output=messages ${TERMS} | \ + matcher_select | \ + sed 's,^id:,,' + +# vim: set ft=sh ts=4 sw=4 et |