Skip to content

Commit 285e53f

Browse files
authored
Merge pull request #78 from DannyBen/add/complete-options
Add support for modifying the `complete` command options
2 parents 188f0d6 + 8619e0d commit 285e53f

File tree

9 files changed

+81
-3
lines changed

9 files changed

+81
-3
lines changed

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require:
1+
plugins:
22
- rubocop-performance
33
- rubocop-rspec
44

Runfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ summary "Runfile tasks for building the Completely gem"
66

77
import_gem 'runfile-tasks/gem'
88
import_gem 'runfile-tasks/docker', image: 'dannyben/completely', version: Completely::VERSION
9+
import 'debug'
910

1011
help 'Test the completely JSON schema'
1112
action :schema do

lib/completely/completions.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,12 @@ def pattern_function_name
8181
def pattern_prefixes
8282
patterns.map(&:prefix)
8383
end
84+
85+
def complete_options_line
86+
options = config.options[:complete_options]
87+
return nil if options.nil? || options.strip.empty?
88+
89+
"#{options} "
90+
end
8491
end
8592
end

lib/completely/config.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Completely
22
class Config
3-
attr_reader :config
3+
attr_reader :config, :options
44

55
class << self
66
def load(config_path)
@@ -17,6 +17,7 @@ def load(config_path)
1717
end
1818

1919
def initialize(config)
20+
@options = config.delete('completely_options')&.transform_keys(&:to_sym) || {}
2021
@config = config
2122
end
2223

lib/completely/templates/template.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@
4444
% end
4545
esac
4646
} &&
47-
complete -F <%= function_name %> <%= command %>
47+
complete <%= complete_options_line %>-F <%= function_name %> <%= command %>
4848

4949
# ex: filetype=sh
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# mygit completion -*- shell-script -*-
2+
3+
# This bash completions script was generated by
4+
# completely (https://github.com/dannyben/completely)
5+
# Modifying it manually is not recommended
6+
7+
_mygit_completions_filter() {
8+
local words="$1"
9+
local cur=${COMP_WORDS[COMP_CWORD]}
10+
local result=()
11+
12+
if [[ "${cur:0:1}" == "-" ]]; then
13+
echo "$words"
14+
15+
else
16+
for word in $words; do
17+
[[ "${word:0:1}" != "-" ]] && result+=("$word")
18+
done
19+
20+
echo "${result[*]}"
21+
22+
fi
23+
}
24+
25+
_mygit_completions() {
26+
local cur=${COMP_WORDS[COMP_CWORD]}
27+
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
28+
local compline="${compwords[*]}"
29+
30+
case "$compline" in
31+
*)
32+
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "status commit")" -- "$cur")
33+
;;
34+
35+
esac
36+
} &&
37+
complete -o nosort -F _mygit_completions mygit
38+
39+
# ex: filetype=sh

spec/completely/completions_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@
4949
.except(/case.*/m)
5050
end
5151
end
52+
53+
context 'with a configuration file that includes complete_options' do
54+
let(:file) { 'complete_options' }
55+
56+
it 'adds the complete_options to the complete command' do
57+
expect(subject.script).to match_approval 'completions/script-complete-options'
58+
end
59+
end
5260
end
5361

5462
describe '#wrapper_function' do

spec/completely/config_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,20 @@
99
expect(subject.flat_config.to_yaml).to match_approval('config/flat_config')
1010
end
1111
end
12+
13+
context 'when complete_options is defined' do
14+
let(:file) { 'complete_options' }
15+
16+
describe 'config' do
17+
it 'ignores the completely_config YAML key' do
18+
expect(subject.config.keys).to eq ['mygit']
19+
end
20+
end
21+
22+
describe 'options' do
23+
it 'returns the completely_options hash from the YAML file' do
24+
expect(subject.options[:complete_options]).to eq '-o nosort'
25+
end
26+
end
27+
end
1228
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
completely_options:
2+
complete_options: -o nosort
3+
4+
mygit:
5+
- status
6+
- commit

0 commit comments

Comments
 (0)