Skip to content

Commit

Permalink
Add shell completions for Bash and Zsh (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ferraz-oliveira authored Sep 25, 2024
1 parent e486fdf commit d45a6ac
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 11 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ on:
jobs:
ci:
name: Run checks and tests over ${{matrix.otp_vsn}}
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
strategy:
matrix:
otp_vsn: ['24', '25', '26', '27']
rebar3_vsn: ['3.23']
otp_vsn: ['25', '26', '27']
rebar3_vsn: ['3.24']
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
id: setup-beam
with:
otp-version: ${{matrix.otp_vsn}}
rebar3-version: ${{matrix.rebar3_vsn}}
- name: Restore _build
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: _build
key: "_build-cache-for\
Expand All @@ -33,7 +33,7 @@ jobs:
-rebar3-${{steps.setup-beam.outputs.rebar3-version}}\
-hash-${{hashFiles('rebar.lock')}}"
- name: Restore rebar3's cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.cache/rebar3
key: "rebar3-cache-for\
Expand Down
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
10 changes: 5 additions & 5 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{erl_opts,
[warn_unused_import, warn_export_vars, warnings_as_errors, verbose, report, debug_info]}.

{minimum_otp_vsn, "23"}.
{minimum_otp_vsn, "25"}.

{profiles,
[{test,
Expand All @@ -24,13 +24,13 @@

%% == Dependencies and plugins ==

{deps, [{elvis_core, "3.2.5"}, {getopt, "1.0.2"}, {egithub, "0.7.0"}]}.
{deps, [{elvis_core, "3.2.5"}, {getopt, "1.0.3"}, {egithub, "0.7.0"}]}.

{project_plugins,
[{rebar3_hank, "~> 1.4.0"},
{rebar3_hex, "~> 7.0.7"},
[{rebar3_hank, "~> 1.4.1"},
{rebar3_hex, "~> 7.0.8"},
{rebar3_format, "~> 1.3.0"},
{rebar3_ex_doc, "~> 0.2.20"}]}.
{rebar3_ex_doc, "~> 0.2.24"}]}.

%% == Documentation ==

Expand Down

0 comments on commit d45a6ac

Please sign in to comment.