Skip to content

Commit e7048a8

Browse files
authored
Merge pull request #85 from DannyBen/refactor/completions
Refactor completions to allow adding custom functions (like git branches)
2 parents 4504ec9 + 86cc50a commit e7048a8

File tree

8 files changed

+34
-46
lines changed

8 files changed

+34
-46
lines changed

README.md

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -349,50 +349,29 @@ functionality to your script in one of three ways:
349349

350350
The bash completions generation is completely automatic, and you will have to
351351
rerun the `bashly add comp *` command whenever you change your `bashly.yml`
352-
script.
352+
file.
353353

354354
In addition to suggesting subcommands and flags, you can instruct bashly to
355-
also suggest files, directories, users and more. To do this, add another option
356-
in your `bashly.yml` on the command you wish to alter:
355+
also suggest files, directories, users, git branches and more. To do this,
356+
add another option in your `bashly.yml` on the command you wish to alter:
357357

358358
```yaml
359359
# bashly.yml
360360
commands:
361361
- name: upload
362362
help: Upload a file
363-
completions: [directory, user]
363+
completions:
364+
- <directory>
365+
- <user>
366+
- $(git branch 2> /dev/null)
364367

365368
```
366369

367-
Valid completion additions are:
368-
369-
| Keyword | Meaning
370-
|-------------|---------------------
371-
| `alias` | Alias names
372-
| `arrayvar` | Array variable names
373-
| `binding` | Readline key binding names
374-
| `builtin` | Names of shell builtin commands
375-
| `command` | Command names
376-
| `directory` | Directory names
377-
| `disabled` | Names of disabled shell builtins
378-
| `enabled` | Names of enabled shell builtins
379-
| `export` | Names of exported shell variables
380-
| `file` | File names
381-
| `function` | Names of shell functions
382-
| `group` | Group names
383-
| `helptopic` | Help topics as accepted by the help builtin
384-
| `hostname` | Hostnames, as taken from the file specified by the HOSTFILE shell variable
385-
| `job` | Job names
386-
| `keyword` | Shell reserved words
387-
| `running` | Names of running jobs
388-
| `service` | Service names
389-
| `signal` | Signal names
390-
| `stopped` | Names of stopped jobs
391-
| `user` | User names
392-
| `variable` | Names of all shell variables
393-
394-
Note that these are taken from the [Programmable Completion Builtin][compgen],
395-
and will simply be added using the `compgen -A action` command.
370+
- Anything between `<...>` will be added using the `compgen -A action` flag.
371+
- Anything else, will be appended to the `compgen -W` flag.
372+
373+
For more information about these custom completions, see the documentation for
374+
the [completely][completely] gem.
396375

397376

398377
## Real World Examples

examples/completions/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ commands:
2626
- name: download
2727
short: d
2828
help: Download a file
29-
completions: [file]
29+
completions:
30+
- <file>
3031

3132
args:
3233
- name: source
@@ -51,7 +52,9 @@ commands:
5152
- name: upload
5253
short: u
5354
help: Upload a file
54-
completions: [directory, user]
55+
completions:
56+
- <directory>
57+
- <user>
5558
args:
5659
- name: source
5760
required: true

examples/completions/src/bashly.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ commands:
1111
- name: download
1212
short: d
1313
help: Download a file
14-
completions: [file]
14+
completions:
15+
- <file>
1516

1617
args:
1718
- name: source
@@ -36,7 +37,9 @@ commands:
3637
- name: upload
3738
short: u
3839
help: Upload a file
39-
completions: [directory, user]
40+
completions:
41+
- <directory>
42+
- <user>
4043
args:
4144
- name: source
4245
required: true

lib/bashly/concerns/completions.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,15 @@ def completion_flag_names
3131
flags.map(&:name) + flags.map(&:short)
3232
end
3333

34-
def completion_actions
35-
completions ? completions.map { |c| "<#{c}>" } : []
36-
end
37-
3834
def completion_words(with_version: false)
3935
trivial_flags = %w[--help -h]
4036
trivial_flags += %w[--version -v] if with_version
4137
all = (
4238
command_names + trivial_flags +
43-
completion_flag_names + completion_actions
39+
completion_flag_names
4440
)
4541

42+
all += completions if completions
4643
all.compact.uniq.sort
4744
end
4845

spec/approvals/completions/advanced

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ say goodbye:
2222
- "-h"
2323
- universe
2424
say goodbye universe:
25+
- "$(git branch)"
2526
- "--color"
2627
- "--help"
2728
- "--verbose"

spec/approvals/completions/script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ _say_completions() {
77
local cur=${COMP_WORDS[COMP_CWORD]}
88

99
case "$COMP_LINE" in
10-
'say goodbye universe'*) COMPREPLY=($(compgen -W "--color --help --verbose -c -h -v" -- "$cur")) ;;
10+
'say goodbye universe'*) COMPREPLY=($(compgen -W "$(git branch) --color --help --verbose -c -h -v" -- "$cur")) ;;
1111
'say hello world'*) COMPREPLY=($(compgen -A directory -A user -W "--force --help --verbose -h" -- "$cur")) ;;
1212
'say goodbye'*) COMPREPLY=($(compgen -W "--help -h universe" -- "$cur")) ;;
1313
'say hello'*) COMPREPLY=($(compgen -W "--help -h world" -- "$cur")) ;;

spec/bashly/models/command_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
end
9494
end
9595

96-
describe '#catch_all_help', :focus do
96+
describe '#catch_all_help' do
9797
context "when catch_all is disabled" do
9898
it "returns nil" do
9999
expect(subject.catch_all_help).to be_nil

spec/fixtures/models/commands.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@
130130

131131
:completions_simple:
132132
name: get
133-
completions: [file]
133+
completions:
134+
- <file>
134135

135136
flags:
136137
- long: --force
@@ -142,13 +143,17 @@
142143
- name: hello
143144
commands:
144145
- name: world
145-
completions: [directory, user]
146+
completions:
147+
- <directory>
148+
- <user>
146149
flags:
147150
- long: --force
148151
- long: --verbose
149152
- name: goodbye
150153
commands:
151154
- name: universe
155+
completions:
156+
- $(git branch)
152157
flags:
153158
- long: --color
154159
short: -c

0 commit comments

Comments
 (0)