diff --git a/Gemfile b/Gemfile index 664f7d1..e4161df 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,6 @@ source 'https://rubygems.org' gem 'byebug' gem 'lp' -gem 'rentacop' gem 'rspec' gem 'rspec_approvals' gem 'runfile' diff --git a/lib/completely/pattern.rb b/lib/completely/pattern.rb index 17da567..a276a29 100644 --- a/lib/completely/pattern.rb +++ b/lib/completely/pattern.rb @@ -28,21 +28,21 @@ def actions end def prefix - text.split[0] + text.split(/ |\*/).first end def case_string if text_without_prefix.empty? '*' elsif text_without_prefix.include? '*' - %['#{text_without_prefix.gsub '*', "'*'"}'] + text_without_prefix.gsub(/([^*]+)/, "'\\1'") else "'#{text_without_prefix}'*" end end def text_without_prefix - @text_without_prefix ||= text.split[1..].join ' ' + @text_without_prefix ||= text[/^#{prefix}\s*(.*)/, 1] end def compgen diff --git a/spec/completely/integration.yml b/spec/completely/integration.yml index fc5e96e..eb90815 100644 --- a/spec/completely/integration.yml +++ b/spec/completely/integration.yml @@ -9,7 +9,7 @@ ftp: expected: [download] - compline: "ftp download " - expected: [another-dir, dir with spaces, dummy-dir, file with spaces.txt, ftp.yaml, gradual.yaml] + expected: [another-dir, dir with spaces, dummy-dir, file with spaces.txt, ftp.yaml, gradual.yaml, wildcard.yaml] - compline: "ftp download -" expected: [--help, --override] @@ -70,4 +70,27 @@ gradual: expected: [blue, red] - compline: "cli command subcommand anything --color gr" - expected: [gray, green] \ No newline at end of file + expected: [gray, green] + +wildcard: +- compline: "wildcard " + expected: [download, upload] + +- compline: "wildcard d" + expected: [download] + +- compline: "wildcard --" + expected: [--debug, --env] + +- compline: "wildcard download --" + expected: [--confirm, --contest] + +- compline: "wildcard --env " + expected: [dev, prod] + +- compline: "wildcard --env prod download --contest " + expected: [everything, nothing] + +- compline: "wildcard download --contest " + expected: [everything, nothing] + diff --git a/spec/completely/pattern_spec.rb b/spec/completely/pattern_spec.rb index a076634..5bc3a6c 100644 --- a/spec/completely/pattern_spec.rb +++ b/spec/completely/pattern_spec.rb @@ -40,9 +40,25 @@ end describe '#prefix' do - it 'returns the first word from text' do + it 'returns the first word of the pattern' do expect(subject.prefix).to eq 'git' end + + context 'when the pattern includes a * right after the first word' do + let(:text) { 'git*--checkout' } + + it 'returns the first word of the pattern' do + expect(subject.prefix).to eq 'git' + end + end + + context 'when the pattern includes a * anywhere else' do + let(:text) { 'git --checkout*something' } + + it 'returns the first word of the pattern' do + expect(subject.prefix).to eq 'git' + end + end end describe '#case_string' do @@ -68,9 +84,25 @@ end describe '#text_without_prefix' do - it 'returns all but the first word from text' do + it 'returns all but the first word' do expect(subject.text_without_prefix).to eq 'commit' end + + context 'when the pattern includes a * right after the first word' do + let(:text) { 'git*--checkout' } + + it 'returns all but the first word' do + expect(subject.text_without_prefix).to eq '*--checkout' + end + end + + context 'when the pattern includes a * anywhere else' do + let(:text) { 'git --checkout*something' } + + it 'returns all but the first word' do + expect(subject.text_without_prefix).to eq '--checkout*something' + end + end end describe '#compgen' do diff --git a/spec/fixtures/integration/wildcard.yaml b/spec/fixtures/integration/wildcard.yaml new file mode 100644 index 0000000..5a724de --- /dev/null +++ b/spec/fixtures/integration/wildcard.yaml @@ -0,0 +1,17 @@ +wildcard: +- --debug +- --env +- download +- upload + +wildcard*--env: +- prod +- dev + +wildcard*download: +- --confirm +- --contest + +wildcard*download*--contest: +- everything +- nothing