Skip to content

Commit 88699ab

Browse files
committed
implement default command completions
1 parent e996014 commit 88699ab

5 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎lib/bashly/views/command/completion_function.gtx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ end
99
>
1010
= render(:completion_word_filter).indent 2
1111
= render(:completion_command_candidates).indent 2
12+
if default_command
13+
> {{ default_command.function_name }}_completion "$completion_current"
14+
end
1215
= render(:completion_flag_candidates).indent 2
1316
= render(:completion_argument_candidates).indent 2
1417
> }

‎lib/bashly/views/command/completion_word_filter.gtx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ end
2525
> *)
2626
if args.any?
2727
= render(:completion_argument_filter).indent 6
28+
elsif default_command
29+
> {{ default_command.function_name }}_completion "$completion_current" "$@"
30+
> return
2831
else
2932
> return
3033
end

‎lib/bashly/views/command/completions.gtx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
> completion_run() {
44
> local -a completion_words=("$@")
55
> local -A completion_blocked_flags=()
6+
> local -A completion_emitted=()
67
> local completion_count=${#completion_words[@]}
78
> local completion_current=""
89
>
@@ -20,8 +21,10 @@
2021
>
2122
> local completion_candidate
2223
> for completion_candidate in "$@"; do
23-
> if [[ $completion_candidate == "$completion_current"* ]]; then
24+
> if [[ $completion_candidate == "$completion_current"* ]] &&
25+
> [[ -z "${completion_emitted[$completion_candidate]:-}" ]]; then
2426
> printf '%s\n' "$completion_candidate"
27+
> completion_emitted["$completion_candidate"]=1
2528
> fi
2629
> done
2730
> }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
parent and default command candidates:
2+
words: ['']
3+
expected: [run, inspect, container, image]
4+
5+
parent and default command prefix:
6+
words: [i]
7+
expected: [inspect, image]
8+
9+
explicit default command:
10+
words: [inspect, '']
11+
expected: [container, image]
12+
13+
implicit default command flag:
14+
words: [--format, '']
15+
expected: [json, yaml]
16+
17+
implicit default command argument:
18+
words: [container, '']
19+
expected: []
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: cli
2+
help: Default command completion fixture
3+
4+
commands:
5+
- name: run
6+
help: Run a target
7+
8+
- name: inspect
9+
help: Inspect a resource
10+
default: true
11+
args:
12+
- name: resource
13+
allowed: [container, image]
14+
help: Resource to inspect
15+
flags:
16+
- long: --format
17+
short: -f
18+
arg: format
19+
allowed: [json, yaml]
20+
help: Output format

0 commit comments

Comments
 (0)