Skip to content

Commit d45a6ac

Browse files
Add shell completions for Bash and Zsh (#591)
1 parent e486fdf commit d45a6ac

File tree

6 files changed

+127
-11
lines changed

6 files changed

+127
-11
lines changed

.github/workflows/ci.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ on:
1111
jobs:
1212
ci:
1313
name: Run checks and tests over ${{matrix.otp_vsn}}
14-
runs-on: ubuntu-22.04
14+
runs-on: ubuntu-24.04
1515
strategy:
1616
matrix:
17-
otp_vsn: ['24', '25', '26', '27']
18-
rebar3_vsn: ['3.23']
17+
otp_vsn: ['25', '26', '27']
18+
rebar3_vsn: ['3.24']
1919
steps:
20-
- uses: actions/checkout@v3
20+
- uses: actions/checkout@v4
2121
- uses: erlef/setup-beam@v1
2222
id: setup-beam
2323
with:
2424
otp-version: ${{matrix.otp_vsn}}
2525
rebar3-version: ${{matrix.rebar3_vsn}}
2626
- name: Restore _build
27-
uses: actions/cache@v3
27+
uses: actions/cache@v4
2828
with:
2929
path: _build
3030
key: "_build-cache-for\
@@ -33,7 +33,7 @@ jobs:
3333
-rebar3-${{steps.setup-beam.outputs.rebar3-version}}\
3434
-hash-${{hashFiles('rebar.lock')}}"
3535
- name: Restore rebar3's cache
36-
uses: actions/cache@v3
36+
uses: actions/cache@v4
3737
with:
3838
path: ~/.cache/rebar3
3939
key: "rebar3-cache-for\

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ logs
77

88
# Ignore elvis escript
99
elvis
10+
!priv/bash_completion/elvis
11+
!priv/zsh_completion/_elvis

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ i.e. `.bashrc`, `.zshrc`, ...)
4646
Now run it by calling `elvis` (or `elvis help`), and you should get to the `Usage: elvis`
4747
instructions.
4848

49+
### Shell completion
50+
51+
Elvis also comes with shell completions.
52+
53+
Optionally, download and install:
54+
55+
- Bash completion from <https://github.com/inaka/elvis/raw/master/priv/bash_completion/elvis>
56+
- Zsh completion from <https://github.com/inaka/elvis/master/priv/zsh_completion/_elvis>
57+
58+
depending on your preferred shell.
59+
4960
## Usage
5061

5162
The most common use case is to `cd` into a folder containing an `elvis.config` file

priv/bash_completion/elvis

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
# shellcheck disable=SC2207 # Prefer mapfile or read -a to split command output (or quote to avoid splitting)
3+
4+
# Bash completion for Elvis, the Erlang style reviewer
5+
_elvis() {
6+
local cur prev
7+
COMPREPLY=()
8+
cur="${COMP_WORDS[COMP_CWORD]}"
9+
prev="${COMP_WORDS[COMP_CWORD - 1]}"
10+
11+
if [[ ${prev} == "--config" ]] || [[ ${prev} == "rock" ]]; then
12+
local files_and_dirs=($(compgen -f -- "${cur}"))
13+
for item in "${files_and_dirs[@]}"; do
14+
if [[ -d "${item}" ]]; then
15+
compopt -o nospace
16+
COMPREPLY+=("${item}/")
17+
else
18+
COMPREPLY+=("${item}")
19+
fi
20+
done
21+
return 0
22+
elif [[ ${prev} == "--code_path" ]]; then
23+
compopt -o nospace
24+
COMPREPLY=($(compgen -d -- "${cur}"))
25+
elif [[ ${prev} == "install" ]]; then
26+
COMPREPLY=($(compgen -W "git-hook" -- "${cur}"))
27+
elif [[ ${prev} == "--output_format" ]]; then
28+
COMPREPLY=($(compgen -W "plain colors parsable" -- "${cur}"))
29+
elif [[ ${prev} == "--parallel" ]]; then
30+
COMPREPLY=($(compgen -W "auto" -- "${cur}"))
31+
elif [[ ${prev} == "git-branch" ]]; then
32+
COMPREPLY=($(compgen -W "$(git branch --format='%(refname)' | sed 's|refs/heads/||' || true)" -- "${cur}"))
33+
else
34+
COMPREPLY=($(compgen -W '--help --config --commands git-branch git-hook install rock --output_format --parallel --quiet --verbose --version --code_path --keep_rocking' -- "${cur}"))
35+
fi
36+
37+
return 0
38+
}
39+
40+
complete -F _elvis elvis

priv/zsh_completion/_elvis

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#compdef elvis
2+
3+
# Bash completion for Elvis, the Erlang style reviewer
4+
5+
_elvis() {
6+
local -a commands
7+
local -a formats
8+
local -a parallel_opts
9+
local -a branches
10+
local cur prev
11+
12+
cur="${1}"
13+
prev="${2}"
14+
15+
commands=(
16+
'--help[Show help information]'
17+
'--config[Provide path to a non-default configuration file]'
18+
'--commands[Show available commands]'
19+
'git-branch[Execute on files changed since <branch> or <commit>]'
20+
'git-hook[Execute on the pre-commit hook Git-staged files]'
21+
'install[Install as a Git pre-commit hook]'
22+
'rock[Execute on identified files]'
23+
'--output_format[Control the output format]'
24+
'--parallel[Analyze files concurrently]'
25+
'--quiet[Suppress all output]'
26+
'--verbose[Enable verbose output]'
27+
'--version[Show the current version]'
28+
'--code_path[Add <dir> to analysis code path]'
29+
'--keep_rocking[Don’t stop on errors]'
30+
)
31+
32+
formats=(
33+
'plain[Plain text output]'
34+
'colors[Colored output]'
35+
'parsable[Output suitable for parsing]'
36+
)
37+
38+
parallel_opts=(
39+
'auto[Automatically determine parallelism]'
40+
'n[Specify number of parallel processes]'
41+
)
42+
43+
if [[ "$prev" == "--config" ]]; then
44+
_files
45+
elif [[ "$prev" == "--code_path" ]]; then
46+
_files -d
47+
elif [[ "$prev" == "--output_format" ]]; then
48+
_describe -t formats
49+
elif [[ "$prev" == "--parallel" ]]; then
50+
_describe -t parallel 'Parallel options' parallel_opts
51+
elif [[ "$prev" == "git-branch" ]]; then
52+
branches=($(git branch --format='%(refname)' | sed 's|refs/heads/||' || true))
53+
_describe -t branches 'Git branches' branches
54+
elif [[ "$prev" == "install" ]]; then
55+
_describe -t install 'Install commands' 'git-hook'
56+
elif [[ "$prev" == "rock" ]]; then
57+
_files
58+
else
59+
_describe -t commands 'elvis commands' commands
60+
fi
61+
}
62+
63+
compdef _elvis elvis

rebar.config

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{erl_opts,
44
[warn_unused_import, warn_export_vars, warnings_as_errors, verbose, report, debug_info]}.
55

6-
{minimum_otp_vsn, "23"}.
6+
{minimum_otp_vsn, "25"}.
77

88
{profiles,
99
[{test,
@@ -24,13 +24,13 @@
2424

2525
%% == Dependencies and plugins ==
2626

27-
{deps, [{elvis_core, "3.2.5"}, {getopt, "1.0.2"}, {egithub, "0.7.0"}]}.
27+
{deps, [{elvis_core, "3.2.5"}, {getopt, "1.0.3"}, {egithub, "0.7.0"}]}.
2828

2929
{project_plugins,
30-
[{rebar3_hank, "~> 1.4.0"},
31-
{rebar3_hex, "~> 7.0.7"},
30+
[{rebar3_hank, "~> 1.4.1"},
31+
{rebar3_hex, "~> 7.0.8"},
3232
{rebar3_format, "~> 1.3.0"},
33-
{rebar3_ex_doc, "~> 0.2.20"}]}.
33+
{rebar3_ex_doc, "~> 0.2.24"}]}.
3434

3535
%% == Documentation ==
3636

0 commit comments

Comments
 (0)