Skip to content
Myr

support completions for command aliases (#669)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* support completions for command aliases * try newer version of shellcheck * initialize aliashash only when BASH_VERSION > 3

authored by Rajat Jindal · committed by Eric Paris · parent 6154259 · Browse files · 2 files+30 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+30 added−1 removed

  • other:+30 added−1 removed

2 files+30 added−1 removed

modified:.circleci/config.yml+1 added-1 removed
modified:bash_completions.go+29 added
249 unmodified lines
250 elif __%[1]s_contains_word "${words[c]}" "${commands[@]}"; then251 __%[1]s_handle_command252 elif [[ $c -eq 0 ]]; then253 __%[1]s_handle_command254 elif __%[1]s_contains_word "${words[c]}" "${command_aliases[@]}"; then255 # aliashash variable is an associative array which is only supported in bash > 3.256 if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then257 words[c]=${aliashash[${words[c]}]}258 __%[1]s_handle_command259 else260 __%[1]s_handle_noun261 fi262 else263 __%[1]s_handle_noun264 fi265 __%[1]s_handle_word
7 unmodified lines
273 buf.WriteString(fmt.Sprintf("__start_%s()\n", name))274 buf.WriteString(fmt.Sprintf(`{275 local cur prev words cword276 declare -A flaghash 2>/dev/null || :277 declare -A aliashash 2>/dev/null || :278 if declare -F _init_completion >/dev/null 2>&1; then279 _init_completion -s || return280 else281 __%[1]s_init_completion -n "=" || return
31 unmodified lines
313 if !c.IsAvailableCommand() || c == cmd.helpCommand {314 continue315 }316 buf.WriteString(fmt.Sprintf(" commands+=(%q)\n", c.Name()))317 writeCmdAliases(buf, c)318 }319 buf.WriteString("\n")320}321
130 unmodified lines
452 buf.WriteString(fmt.Sprintf(" must_have_one_noun+=(%q)\n", value))453 }454}455456func writeCmdAliases(buf *bytes.Buffer, cmd *Command) {457 if len(cmd.Aliases) == 0 {458 return459 }460461 sort.Sort(sort.StringSlice(cmd.Aliases))462463 buf.WriteString(fmt.Sprint(` if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then`, "\n"))464 for _, value := range cmd.Aliases {465 buf.WriteString(fmt.Sprintf(" command_aliases+=(%q)\n", value))466 buf.WriteString(fmt.Sprintf(" aliashash[%q]=%q\n", value, cmd.Name()))467 }468 buf.WriteString(` fi`)469 buf.WriteString("\n")470}471func writeArgAliases(buf *bytes.Buffer, cmd *Command) {472 buf.WriteString(" noun_aliases=()\n")473 sort.Sort(sort.StringSlice(cmd.ArgAliases))474 for _, value := range cmd.ArgAliases {
18 unmodified lines
493 buf.WriteString(fmt.Sprintf("_%s()\n{\n", commandName))494 }495496 buf.WriteString(fmt.Sprintf(" last_command=%q\n", commandName))497 buf.WriteString("\n")498 buf.WriteString(" command_aliases=()\n")499 buf.WriteString("\n")500501 writeCommands(buf, cmd)502 writeFlags(buf, cmd)503 writeRequiredFlag(buf, cmd)504 writeRequiredNouns(buf, cmd)
80 unmodified lines