diff --git a/README.md b/README.md index 3700b15c..197c4107 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,27 @@ make install The abra binary will be in `$GOPATH/bin`. +## Autocompletion + +**bash** + +Copy `autocomplete/bash_autocomplete` into `/etc/bash_completion.d/` and rename +it to abra. +``` +sudo cp autocomplete/bash_autocomplete /etc/bash_completion.d/abra +source /etc/bash_completion.d/abra +``` + +**(fi)zsh** + +(fi)zsh doesn't have an autocompletion folder by default but you can create one, then copy `zsh_autocomplete` into it and add a couple lines to your `~/.zshrc` or `~/.fizsh/.fizshrc` +``` +sudo mkdir /etc/zsh/completion.d/ +sudo cp autocomplete/zsh_autocomplete /etc/zsh/completion.d/abra +echo "PROG=abra\n_CLI_ZSH_AUTOCOMPLETE_HACK=1\nsource /etc/zsh/completion.d/abra" >> ~/.zshrc +``` +(replace .zshrc with ~/.fizsh/.fizshrc if you use fizsh) + ## Hacking Install direnv, run `cp .envrc.sample .envrc`, then run `direnv allow` in this directory. This will set coopcloud repos as private due to [this bug.](https://git.coopcloud.tech/coop-cloud/coopcloud.tech/issues/20#issuecomment-8201). Or you can run `go env -w GOPRIVATE=coopcloud.tech` but I'm not sure how persistent this is. diff --git a/autocomplete/bash_autocomplete b/autocomplete/bash_autocomplete new file mode 100755 index 00000000..f0f62418 --- /dev/null +++ b/autocomplete/bash_autocomplete @@ -0,0 +1,21 @@ +#! /bin/bash + +: ${PROG:=$(basename ${BASH_SOURCE})} + +_cli_bash_autocomplete() { + if [[ "${COMP_WORDS[0]}" != "source" ]]; then + local cur opts base + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + if [[ "$cur" == "-"* ]]; then + opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion ) + else + opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion ) + fi + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + fi +} + +complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG +unset PROG diff --git a/autocomplete/powershell_autocomplete.ps1 b/autocomplete/powershell_autocomplete.ps1 new file mode 100644 index 00000000..81812a6a --- /dev/null +++ b/autocomplete/powershell_autocomplete.ps1 @@ -0,0 +1,9 @@ +$fn = $($MyInvocation.MyCommand.Name) +$name = $fn -replace "(.*)\.ps1$", '$1' +Register-ArgumentCompleter -Native -CommandName $name -ScriptBlock { + param($commandName, $wordToComplete, $cursorPosition) + $other = "$wordToComplete --generate-bash-completion" + Invoke-Expression $other | ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) + } + } \ No newline at end of file diff --git a/autocomplete/zsh_autocomplete b/autocomplete/zsh_autocomplete new file mode 100644 index 00000000..cf39c888 --- /dev/null +++ b/autocomplete/zsh_autocomplete @@ -0,0 +1,23 @@ +#compdef $PROG + +_cli_zsh_autocomplete() { + + local -a opts + local cur + cur=${words[-1]} + if [[ "$cur" == "-"* ]]; then + opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}") + else + opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}") + fi + + if [[ "${opts[1]}" != "" ]]; then + _describe 'values' opts + else + _files + fi + + return +} + +compdef _cli_zsh_autocomplete $PROG diff --git a/cli/cli.go b/cli/cli.go index 97a20b1d..7f9ffb98 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -67,6 +67,7 @@ func RunApp(version, commit string) { }, }, } + app.EnableBashCompletion = true if err := app.Run(os.Args); err != nil { logrus.Fatal(err)