From d45a6ac2253012d45043fb3cb522289b062c4a3c Mon Sep 17 00:00:00 2001 From: "Paulo F. Oliveira" Date: Wed, 25 Sep 2024 07:09:55 +0100 Subject: [PATCH] Add shell completions for Bash and Zsh (#591) --- .github/workflows/ci.yml | 12 ++++---- .gitignore | 2 ++ README.md | 11 +++++++ priv/bash_completion/elvis | 40 ++++++++++++++++++++++++ priv/zsh_completion/_elvis | 63 ++++++++++++++++++++++++++++++++++++++ rebar.config | 10 +++--- 6 files changed, 127 insertions(+), 11 deletions(-) create mode 100644 priv/bash_completion/elvis create mode 100644 priv/zsh_completion/_elvis diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 44242b0..0258821 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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\ @@ -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\ diff --git a/.gitignore b/.gitignore index 522c36d..e20de91 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ logs # Ignore elvis escript elvis +!priv/bash_completion/elvis +!priv/zsh_completion/_elvis diff --git a/README.md b/README.md index 6d4f0f4..107e898 100644 --- a/README.md +++ b/README.md @@ -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 +- Zsh completion from + +depending on your preferred shell. + ## Usage The most common use case is to `cd` into a folder containing an `elvis.config` file diff --git a/priv/bash_completion/elvis b/priv/bash_completion/elvis new file mode 100644 index 0000000..1f33487 --- /dev/null +++ b/priv/bash_completion/elvis @@ -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 diff --git a/priv/zsh_completion/_elvis b/priv/zsh_completion/_elvis new file mode 100644 index 0000000..86c9ec2 --- /dev/null +++ b/priv/zsh_completion/_elvis @@ -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 or ]' + '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 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 diff --git a/rebar.config b/rebar.config index 6ab50a9..91bbde7 100644 --- a/rebar.config +++ b/rebar.config @@ -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, @@ -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 ==