Skip to content

Commit

Permalink
Add shell completions
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ferraz-oliveira committed Sep 24, 2024
1 parent e486fdf commit 8270a45
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ logs

# Ignore elvis escript
elvis
!priv/bash_completion/elvis
!priv/zsh_completion/_elvis
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ i.e. `.bashrc`, `.zshrc`, ...)
Now run it by calling `elvis` (or `elvis help`), and you should get to the `Usage: elvis`
instructions.

### Shell completion

Elvis also comes with shell completions.

Optionally, download and install:

- Bash completion from <https://github.com/inaka/elvis/raw/master/priv/bash_completion/elvis>
- Zsh completion from <https://github.com/inaka/elvis/master/priv/zsh_completion/_elvis>

depending on your preferred shell.

## Usage

The most common use case is to `cd` into a folder containing an `elvis.config` file
Expand Down
40 changes: 40 additions & 0 deletions priv/bash_completion/elvis
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash
# shellcheck disable=SC2207 # Prefer mapfile or read -a to split command output (or quote to avoid splitting)

# Bash completion for Elvis, the Erlang style reviewer
_elvis() {
local cur prev
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD - 1]}"

if [[ ${prev} == "--config" ]] || [[ ${prev} == "rock" ]]; then
local files_and_dirs=($(compgen -f -- "${cur}"))
for item in "${files_and_dirs[@]}"; do
if [[ -d "${item}" ]]; then
compopt -o nospace
COMPREPLY+=("${item}/")
else
COMPREPLY+=("${item}")
fi
done
return 0
elif [[ ${prev} == "--code_path" ]]; then
compopt -o nospace
COMPREPLY=($(compgen -d -- "${cur}"))
elif [[ ${prev} == "install" ]]; then
COMPREPLY=($(compgen -W "git-hook" -- "${cur}"))
elif [[ ${prev} == "--output_format" ]]; then
COMPREPLY=($(compgen -W "plain colors parsable" -- "${cur}"))
elif [[ ${prev} == "--parallel" ]]; then
COMPREPLY=($(compgen -W "auto" -- "${cur}"))
elif [[ ${prev} == "git-branch" ]]; then
COMPREPLY=($(compgen -W "$(git branch --format='%(refname)' | sed 's|refs/heads/||' || true)" -- "${cur}"))
else
COMPREPLY=($(compgen -W '--help --config --commands git-branch git-hook install rock --output_format --parallel --quiet --verbose --version --code_path --keep_rocking' -- "${cur}"))
fi

return 0
}

complete -F _elvis elvis
63 changes: 63 additions & 0 deletions priv/zsh_completion/_elvis
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#compdef elvis

# Bash completion for Elvis, the Erlang style reviewer

_elvis() {
local -a commands
local -a formats
local -a parallel_opts
local -a branches
local cur prev

cur="${1}"
prev="${2}"

commands=(
'--help[Show help information]'
'--config[Provide path to a non-default configuration file]'
'--commands[Show available commands]'
'git-branch[Execute on files changed since <branch> or <commit>]'
'git-hook[Execute on the pre-commit hook Git-staged files]'
'install[Install as a Git pre-commit hook]'
'rock[Execute on identified files]'
'--output_format[Control the output format]'
'--parallel[Analyze files concurrently]'
'--quiet[Suppress all output]'
'--verbose[Enable verbose output]'
'--version[Show the current version]'
'--code_path[Add <dir> to analysis code path]'
'--keep_rocking[Don’t stop on errors]'
)

formats=(
'plain[Plain text output]'
'colors[Colored output]'
'parsable[Output suitable for parsing]'
)

parallel_opts=(
'auto[Automatically determine parallelism]'
'n[Specify number of parallel processes]'
)

if [[ "$prev" == "--config" ]]; then
_files
elif [[ "$prev" == "--code_path" ]]; then
_files -d
elif [[ "$prev" == "--output_format" ]]; then
_describe -t formats
elif [[ "$prev" == "--parallel" ]]; then
_describe -t parallel 'Parallel options' parallel_opts
elif [[ "$prev" == "git-branch" ]]; then
branches=($(git branch --format='%(refname)' | sed 's|refs/heads/||' || true))
_describe -t branches 'Git branches' branches
elif [[ "$prev" == "install" ]]; then
_describe -t install 'Install commands' 'git-hook'
elif [[ "$prev" == "rock" ]]; then
_files
else
_describe -t commands 'elvis commands' commands
fi
}

compdef _elvis elvis

0 comments on commit 8270a45

Please sign in to comment.