Skip to content

Commit

Permalink
completion: add zsh (exercism#653)
Browse files Browse the repository at this point in the history
Add a zsh completion script and the ability to run

    configlet completion --shell zsh

to write that script to stdout.

This commit also bumps the version of the shellcheck action, which is
necessary to prevent the action from running `shellcheck` on the new zsh
script. Shellcheck does not support zsh, so CI would indicate an error
otherwise.

The bump must be to the latest commit - we already used the latest
release (1.1.0). An upstream commit [1] that claims to exclude zsh
scripts seems to be insufficient - we need to use the `ignore_names`
input that does not exist in the latest release [2].

[1] ludeeus/action-shellcheck@ceeca77f6538
[2] ludeeus/action-shellcheck@c2b45ddc5f85

Closes: exercism#633
  • Loading branch information
ee7 committed Aug 13, 2022
1 parent 1d689ed commit e4c8643
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/shellcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ jobs:
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b

- name: Run shellcheck
uses: ludeeus/action-shellcheck@94e0aab03ca135d11a35e5bfc14e6746dc56e7e9
uses: ludeeus/action-shellcheck@203a3fd018dfe73f8ae7e3aa8da2c149a5f41c33
with:
ignore_names: configlet.zsh
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Commands:
Options for completion:
-s, --shell <shell> Choose the shell type (required)
Allowed values: b[ash], f[ish]
Allowed values: b[ash], f[ish], z[sh]
Options for fmt:
-e, --exercise <slug> Only operate on this exercise
Expand Down
112 changes: 112 additions & 0 deletions completions/configlet.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#compdef configlet

autoload -U is-at-least

(( $+functions[_configlet_commands] )) ||
_configlet_commands() {
local commands
commands=(
# subcommands with no options
"generate:Generate concept exercise introductions" \
"lint:Check the track configuration for correctness" \
# subcommands with options
"completion:Output a completion script for a given shell" \
"fmt:Format the exercise '.meta/config.json' files" \
"info:Print track information" \
"sync:Check or update Practice Exercise docs, metadata, and tests" \
"uuid:Output a version 4 UUID" \
)
_describe -t commands 'configlet commands' commands "$@"
}

_configlet() {
typeset -a _arguments_options

if is-at-least 5.2; then
_arguments_options=(-s -S -C)
else
_arguments_options=(-s -C)
fi

local line
local curcontext="$curcontext"

_configlet_global_opts=(
{-h,--help}'[Show help]'
'--version[Show version information]'
'(-t --track-dir)'{-t+,--track-dir=}'[Select a track directory]:directory:_directories'
{-v,--verbosity}'[Verbosity level]: :(quiet normal detailed)'
)

_arguments "${_arguments_options[@]}" \
"$_configlet_global_opts[@]" \
":: :_configlet_commands" \
"*::: :->configlet"

words=($line[1] "${words[@]}")
(( CURRENT += 1 ))
curcontext="${curcontext%:*:*}:configlet-command-$line[1]:"

_configlet_complete_any_exercise_slug() {
local -a slugs slug_paths
slug_paths=( ./exercises/{concept,practice}/*(/) )
slugs=( "${slug_paths[@]##*/}" )
compadd "$@" -a slugs
}

_configlet_complete_practice_exercise_slug() {
local -a slugs slug_paths
slug_paths=( ./exercises/practice/*(/) )
slugs=( "${slug_paths[@]##*/}" )
compadd "$@" -a slugs
}

case $line[1] in
# subcommands with no options
(generate)
_arguments "${_arguments_options[@]}" \
"$_configlet_global_opts[@]" \
;;
(lint)
_arguments "${_arguments_options[@]}" \
"$_configlet_global_opts[@]" \
;;
# subcommands with options
(completion)
_arguments "${_arguments_options[@]}" \
"$_configlet_global_opts[@]" \
{-s,--shell}'[Select the shell type]: :(bash fish zsh)' \
;;
(fmt)
_arguments "${_arguments_options[@]}" \
"$_configlet_global_opts[@]" \
'(-e --exercise)'{-e+,--exercise=}'[exercise slug]:slug:_configlet_complete_any_exercise_slug' \
{-u,--update}'[Write changes]' \
{-y,--yes}'[Auto-confirm update]' \
;;
(info)
_arguments "${_arguments_options[@]}" \
"$_configlet_global_opts[@]" \
{-o,--offline}'[Do not update prob-specs cache]' \
;;
(sync)
_arguments "${_arguments_options[@]}" \
"$_configlet_global_opts[@]" \
'(-e --exercise)'{-e+,--exercise=}'[exercise slug]:slug:_configlet_complete_practice_exercise_slug' \
{-o,--offline}'[Do not update prob-specs cache]' \
{-u,--update}'[Write changes]' \
{-y,--yes}'[Auto-confirm update]' \
'--docs[Sync docs only]' \
'--filepaths[Populate .meta/config.json "files" entry]' \
'--metadata[Sync metadata only]' \
'--tests[For auto-confirming]: :(choose include exclude)' \
;;
(uuid)
_arguments "${_arguments_options[@]}" \
"$_configlet_global_opts[@]" \
'(-n --num)'{-n+,--num=}'[How many UUIDs]:' \
;;
esac
}

_configlet "$@"
1 change: 1 addition & 0 deletions src/cli.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type
sNil = "nil"
sBash = "bash"
sFish = "fish"
sZsh = "zsh"

SyncKind* = enum
skDocs = "docs"
Expand Down
4 changes: 2 additions & 2 deletions tests/test_binary.nim
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ proc testsForGenerate(binaryPath: string) =
proc testsForCompletion(binaryPath: string) =
suite "completion":
const completionsDir = repoRootDir / "completions"
for shell in ["bash", "fish"]:
for shell in ["bash", "fish", "zsh"]:
test shell:
let c = shell[0]
# Convert platform-specific line endings (e.g. CR+LF on Windows) to LF
Expand All @@ -943,7 +943,7 @@ proc testsForCompletion(binaryPath: string) =
execAndCheck(0, &"{binaryPath} completion -s {shell}", expected)
execAndCheck(0, &"{binaryPath} completion -s {c}", expected)
execAndCheck(0, &"{binaryPath} completion -s{c}", expected)
for shell in ["powershell", "zsh"]:
for shell in ["powershell"]:
test &"{shell} (produces an error)":
let (outp, exitCode) = execCmdEx(&"{binaryPath} completion -s {shell}")
check:
Expand Down

0 comments on commit e4c8643

Please sign in to comment.