[PATCH] cli: handle bash completion of all shared options

classic Classic list List threaded Threaded
2 messages Options
Tobias Backer Dirks Tobias Backer Dirks
Reply | Threaded
Open this post in threaded view
|

[PATCH] cli: handle bash completion of all shared options

A slight rework of base bash completion to handle all shared options.
This simplifies the handling of base commands and adds creature comforts
such as directory completion for --config, and removing trailing spaces
from options ending in an `=`. Also fixes a bug reported in [1], wherein
completion after an `=` produced erroneous output.

[1]: id:[hidden email]

Signed-off-by: Tobias Backer Dirks <[hidden email]>
---
 completion/notmuch-completion.bash | 74 ++++++++++++++++--------------
 1 file changed, 40 insertions(+), 34 deletions(-)

diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash
index 15425697..6d349081 100644
--- a/completion/notmuch-completion.bash
+++ b/completion/notmuch-completion.bash
@@ -27,7 +27,7 @@
 # on completion.
 #
 
-_notmuch_shared_options="--help --uuid= --version"
+_notmuch_shared_options="--config= --help --uuid= --version"
 
 # $1: current input of the form prefix:partialinput, where prefix is
 # to or from.
@@ -580,43 +580,49 @@ _notmuch_tag()
 _notmuch()
 {
     local _notmuch_commands="compact config count dump help insert new reply restore reindex search address setup show tag emacs-mua"
-    local arg cur prev words cword split
+    local cur prev split
 
-    # require bash-completion with _init_completion
-    type -t _init_completion >/dev/null 2>&1 || return
-
-    _init_completion || return
+    _init_completion -s || return
 
-    COMPREPLY=()
+    # handle _notmuch_shared_options
+    case $prev in
+ --version | --uuid)
+    return
+            ;;
+ --help)
+    local help_topics="$_notmuch_commands hooks search-terms properties"
+    COMPREPLY=( $(compgen -W "${help_topics}" -- "$cur") )
+    return
+    ;;
+ --config)
+    _filedir
+            COMPREPLY=("${COMPREPLY[@]/#/}")
+    return
+    ;;
+    esac
 
-    # subcommand
-    _get_first_arg
+    $split && return
 
-    # complete --help option like the subcommand
-    if [ -z "${arg}" -a "${prev}" = "--help" ]; then
- arg="help"
-    fi
+    case $COMP_CWORD in
+ 1) # handle top level completions
+    COMPREPLY=($(compgen -W "$_notmuch_commands" -- "$cur"))
 
-    if [ -z "${arg}" ]; then
- # top level completion
- case "${cur}" in
-    -*)
- # XXX: handle ${_notmuch_shared_options} and --config=
- local options="--help --version"
- COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
- ;;
-    *)
- COMPREPLY=( $(compgen -W "${_notmuch_commands}" -- ${cur}) )
- ;;
- esac
-    elif [ "${arg}" = "help" ]; then
- # handle help command specially due to _notmuch_commands usage
- local help_topics="$_notmuch_commands hooks search-terms properties"
- COMPREPLY=( $(compgen -W "${help_topics}" -- ${cur}) )
-    else
- # complete using _notmuch_subcommand if one exist
- local completion_func="_notmuch_${arg//-/_}"
- declare -f $completion_func >/dev/null && $completion_func
-    fi
+    # complete _notmuch_shared_options
+    if [[ "$cur" == -* ]]; then
+ COMPREPLY=($(compgen -W "$_notmuch_shared_options" -- "$cur"))
+ # if completion ends in `=` do not add a space
+ [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
+ return
+    fi
+    ;;
+ 2) # handle second level completions
+    local completion_func prev
+    prev=${COMP_WORDS[COMP_CWORD-1]}
+    completion_func="_notmuch_${prev}"
+    # try use a completion function if available
+    declare -f "$completion_func" >/dev/null && "$completion_func"
+    return
+    ;;
+    esac
 } &&
 complete -F _notmuch notmuch
--
2.31.1
_______________________________________________
notmuch mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]
David Bremner-2 David Bremner-2
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] cli: handle bash completion of all shared options

Tobias Backer Dirks <[hidden email]> writes:

> A slight rework of base bash completion to handle all shared options.
> This simplifies the handling of base commands and adds creature comforts
> such as directory completion for --config, and removing trailing spaces
> from options ending in an `=`. Also fixes a bug reported in [1], wherein
> completion after an `=` produced erroneous output.
>
> [1]: id:[hidden email]
>
> Signed-off-by: Tobias Backer Dirks <[hidden email]>

Thanks for the patch. Zsh user here, so I'm hoping it will be reviewed
by someone who uses bash as their daily driver.

d
_______________________________________________
notmuch mailing list -- [hidden email]
To unsubscribe send an email to [hidden email]