diff options
author | TsT <tst2005@gmail.com> | 2022-04-26 21:40:35 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2022-05-01 10:54:13 +0200 |
commit | edc8b7589dcf1daad65b0e57488a3b3f86711926 (patch) | |
tree | d97e6416829a680092fe0201f91ad7defa6c8c87 | |
parent | f25690db47b110bcd0617d69cb0a35967313753c (diff) | |
download | git-bug-edc8b7589dcf1daad65b0e57488a3b3f86711926.tar.gz |
misc: fix bash completion with "git bug"
-rw-r--r-- | misc/bash_completion/git-bug | 36 | ||||
-rw-r--r-- | misc/gen_completion.go | 45 |
2 files changed, 76 insertions, 5 deletions
diff --git a/misc/bash_completion/git-bug b/misc/bash_completion/git-bug index dec15576..85d2a15b 100644 --- a/misc/bash_completion/git-bug +++ b/misc/bash_completion/git-bug @@ -1,4 +1,4 @@ -#TODO: completion code to map "git bug" to "git-bug"# bash completion V2 for git-bug -*- shell-script -*- +# bash completion V2 for git-bug -*- shell-script -*- __git-bug_debug() { @@ -286,3 +286,37 @@ else fi # ex: ts=4 sw=4 et filetype=sh + +_git_bug() { + local cur prev words cword split + + COMPREPLY=() + + # Call _init_completion from the bash-completion package + # to prepare the arguments properly + if declare -F _init_completion >/dev/null 2>&1; then + _init_completion -n "=:" || return + else + __git-bug_init_completion -n "=:" || return + fi + + # START PATCH + # replace in the array ("git","bug", ...) to ("git-bug", ...) and adjust the index in cword + words=("git-bug" "${words[@]:2}") + cword=$(($cword-1)) + # END PATCH + + __git-bug_debug + __git-bug_debug "========= starting completion logic ==========" + __git-bug_debug "cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}, cword is $cword" + + # The user could have moved the cursor backwards on the command-line. + # We need to trigger completion from the $cword location, so we need + # to truncate the command-line ($words) up to the $cword location. + words=("${words[@]:0:$cword+1}") + __git-bug_debug "Truncated words[*]: ${words[*]}," + + local out directive + __git-bug_get_completion_results + __git-bug_process_completion_results +} diff --git a/misc/gen_completion.go b/misc/gen_completion.go index fc0f1f68..1f86124d 100644 --- a/misc/gen_completion.go +++ b/misc/gen_completion.go @@ -50,14 +50,51 @@ func genBash(root *cobra.Command) error { } defer f.Close() - // Custom bash code to connect the git completion for "git bug" to the - // git-bug completion for "git-bug" - _, err = f.WriteString(`#TODO: completion code to map "git bug" to "git-bug"`) + const patch = ` +_git_bug() { + local cur prev words cword split + + COMPREPLY=() + + # Call _init_completion from the bash-completion package + # to prepare the arguments properly + if declare -F _init_completion >/dev/null 2>&1; then + _init_completion -n "=:" || return + else + __git-bug_init_completion -n "=:" || return + fi + + # START PATCH + # replace in the array ("git","bug", ...) to ("git-bug", ...) and adjust the index in cword + words=("git-bug" "${words[@]:2}") + cword=$(($cword-1)) + # END PATCH + + __git-bug_debug + __git-bug_debug "========= starting completion logic ==========" + __git-bug_debug "cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}, cword is $cword" + + # The user could have moved the cursor backwards on the command-line. + # We need to trigger completion from the $cword location, so we need + # to truncate the command-line ($words) up to the $cword location. + words=("${words[@]:0:$cword+1}") + __git-bug_debug "Truncated words[*]: ${words[*]}," + + local out directive + __git-bug_get_completion_results + __git-bug_process_completion_results +} +` + err = root.GenBashCompletionV2(f, true) if err != nil { return err } - return root.GenBashCompletionV2(f, true) + // Custom bash code to connect the git completion for "git bug" to the + // git-bug completion for "git-bug" + _, err = f.WriteString(patch) + + return err } func genFish(root *cobra.Command) error { |