Skip to content

Commit 690e9e2

Browse files
glennjee7
andauthored
completions: add completions for bash and fish (#629)
For later: - Add completions for zsh and PowerShell. - Make `fetch-configlet` detect the user's shell and explain how to install completions. - Make `configlet completion -s <shell>` print the corresponding completion script to stdout. This commit also refactors `create-artifact` slightly. Closes: #615 Co-authored-by: ee7 <45465154+ee7@users.noreply.github.com>
1 parent e5423a0 commit 690e9e2

File tree

3 files changed

+151
-5
lines changed

3 files changed

+151
-5
lines changed

.github/bin/create-artifact

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ case "${OS}" in
1010
artifact_file="${artifacts_dir}/${binary_name}-${OS}-${ARCH}.zip"
1111
7z a "${artifact_file}" "${binary_name}.exe"
1212
;;
13-
linux)
14-
artifact_file="${artifacts_dir}/${binary_name}-${OS}-${ARCH}.tgz"
15-
tar -cvzf "${artifact_file}" "${binary_name}"
16-
;;
17-
mac)
13+
linux | mac)
1814
artifact_file="${artifacts_dir}/${binary_name}-${OS}-${ARCH}.tgz"
1915
tar -cvzf "${artifact_file}" "${binary_name}"
2016
;;

completions/configlet.bash

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# completions for the configlet command, bash flavour
2+
3+
# remove any prior completions
4+
complete -r bin/configlet configlet 2>/dev/null
5+
# and install this one
6+
complete -F _configlet_completion_ configlet
7+
8+
# @(pattern1|pattern2|...) is bash extended pattern matching meaning
9+
# "one of pattern1 or pattern2 or ..."
10+
# ref: https://www.gnu.org/software/bash/manual/bash.html#Pattern-Matching
11+
12+
_configlet_completion_() {
13+
local global_opts='-h --help --version -t --track-dir -v --verbosity'
14+
local cur=${COMP_WORDS[COMP_CWORD]}
15+
local prev=${COMP_WORDS[COMP_CWORD - 1]}
16+
17+
# Check for global options that take a value
18+
if _configlet_complete_global_option_; then
19+
return
20+
fi
21+
22+
local i
23+
for ((i = 1; i < COMP_CWORD; i++)); do
24+
if [[ ${COMP_WORDS[i]} == @(lint|generate|info|uuid|fmt|sync) ]]; then
25+
"_configlet_complete_${COMP_WORDS[i]}_"
26+
return
27+
fi
28+
done
29+
30+
_configlet_complete_options_ "fmt generate info lint sync uuid $global_opts"
31+
}
32+
33+
_configlet_complete_global_option_() {
34+
case $prev in
35+
'-v' | '--verbosity')
36+
_configlet_complete_options_ "quiet normal detailed"
37+
return 0
38+
;;
39+
'-t' | '--track-dir')
40+
# Complete a directory based on what the user's typed so far
41+
mapfile -t COMPREPLY < <(compgen -A directory -- "$cur")
42+
return 0
43+
;;
44+
esac
45+
return 1
46+
}
47+
48+
_configlet_complete_lint_() {
49+
_configlet_complete_options_ "$global_opts"
50+
}
51+
52+
_configlet_complete_generate_() {
53+
_configlet_complete_options_ "$global_opts"
54+
}
55+
56+
_configlet_complete_info_() {
57+
_configlet_complete_options_ "-o --offline $global_opts"
58+
}
59+
60+
_configlet_complete_uuid_() {
61+
_configlet_complete_options_ "-n --num $global_opts"
62+
}
63+
64+
_configlet_complete_fmt_() {
65+
case $prev in
66+
'-e' | '--exercise')
67+
_configlet_complete_slugs_ "practice" "concept"
68+
;;
69+
*)
70+
_configlet_complete_options_ "-e --exercise -u --update -y --yes $global_opts"
71+
;;
72+
esac
73+
}
74+
75+
_configlet_complete_sync_() {
76+
case $prev in
77+
'--tests')
78+
_configlet_complete_options_ "choose include exclude"
79+
;;
80+
'-e' | '--exercise')
81+
_configlet_complete_slugs_ "practice"
82+
;;
83+
*)
84+
local options=(
85+
-e --exercise
86+
-o --offline
87+
-u --update
88+
-y --yes
89+
--docs
90+
--filepaths
91+
--metadata
92+
--tests
93+
)
94+
_configlet_complete_options_ "${options[*]} $global_opts"
95+
;;
96+
esac
97+
}
98+
99+
# Note that configlet expects to be called from the track's root dir.
100+
_configlet_complete_slugs_() {
101+
local subdir
102+
mapfile -t COMPREPLY < <(
103+
for subdir in "$@"; do
104+
if [[ -d "./exercises/$subdir" ]]; then
105+
( cd "./exercises/$subdir" && compgen -A directory -- "$cur" )
106+
fi
107+
done
108+
)
109+
}
110+
111+
_configlet_complete_options_() {
112+
local choices=$1
113+
mapfile -t COMPREPLY < <(compgen -o nosort -W "$choices" -- "$cur")
114+
}

completions/configlet.fish

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# global options
2+
complete -c configlet -s h -l help -f -d "Show help"
3+
complete -c configlet -l version -f -d "Show version info"
4+
complete -c configlet -s t -l track-dir -d "Select a track directory"
5+
complete -c configlet -s v -l verbosity -x -a "quiet normal detailed" -d "Verbose level"
6+
7+
# subcommands with no options
8+
complete -c configlet -n "__fish_use_subcommand" -a lint -f -d "Check the track configuration for correctness"
9+
complete -c configlet -n "__fish_use_subcommand" -a generate -f -d "Generate concept exercise introductions"
10+
11+
# info subcommand
12+
complete -c configlet -n "__fish_use_subcommand" -a info -f -d "Track info"
13+
complete -c configlet -n "__fish_seen_subcommand_from info" -s o -l offline -f -d "Do not update prob-specs cache"
14+
15+
# uuid subcommand
16+
complete -c configlet -n "__fish_use_subcommand" -a uuid -f -d "Output a UUID"
17+
complete -c configlet -n "__fish_seen_subcommand_from uuid" -s n -l num -f -d "How many UUIDs"
18+
19+
# fmt subcommand
20+
complete -c configlet -n "__fish_use_subcommand" -a fmt -f -d "Format the exercise '.meta/config.json' files"
21+
complete -c configlet -n "__fish_seen_subcommand_from fmt" -s u -l update -d "Write changes"
22+
complete -c configlet -n "__fish_seen_subcommand_from fmt" -s y -l yes -d "Auto-confirm update"
23+
complete -c configlet -n "__fish_seen_subcommand_from fmt" -s e -l exercise -d "exercise slug" \
24+
-x -a "(find ./exercises/{concept,practice} -maxdepth 1 -mindepth 1 -type d -printf '%P\n' | sort)"
25+
26+
# sync subcommand
27+
complete -c configlet -n "__fish_use_subcommand" -a sync -d "Check or update Practice Exercise docs, metadata, and tests"
28+
complete -c configlet -n "__fish_seen_subcommand_from sync" -s o -l offline -f -d "Do not update prob-specs cache"
29+
complete -c configlet -n "__fish_seen_subcommand_from sync" -s u -l update -d "Write changes"
30+
complete -c configlet -n "__fish_seen_subcommand_from sync" -s y -l yes -d "Auto-confirm update"
31+
complete -c configlet -n "__fish_seen_subcommand_from sync" -l docs -d "Sync docs only"
32+
complete -c configlet -n "__fish_seen_subcommand_from sync" -l filepaths -f -d 'Populate .meta/config.json "files" entry'
33+
complete -c configlet -n "__fish_seen_subcommand_from sync" -l metadata -d "Sync metadata only"
34+
complete -c configlet -n "__fish_seen_subcommand_from sync" -l tests -d "For auto-confirming" -x -a "choose include exclude"
35+
complete -c configlet -n "__fish_seen_subcommand_from sync" -s e -l exercise -d "exercise slug" \
36+
-x -a "(find ./exercises/practice -maxdepth 1 -mindepth 1 -type d -printf '%P\n' | sort)"

0 commit comments

Comments
 (0)