Skip to content

Commit 9f1f4f5

Browse files
authored
Merge pull request #39 from DannyBen/fix/wildcards
Fix broken script when wildcards follow the first word
2 parents 26efd19 + eeb3873 commit 9f1f4f5

File tree

5 files changed

+79
-8
lines changed

5 files changed

+79
-8
lines changed

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ source 'https://rubygems.org'
22

33
gem 'byebug'
44
gem 'lp'
5-
gem 'rentacop'
65
gem 'rspec'
76
gem 'rspec_approvals'
87
gem 'runfile'

lib/completely/pattern.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ def actions
2828
end
2929

3030
def prefix
31-
text.split[0]
31+
text.split(/ |\*/).first
3232
end
3333

3434
def case_string
3535
if text_without_prefix.empty?
3636
'*'
3737
elsif text_without_prefix.include? '*'
38-
%['#{text_without_prefix.gsub '*', "'*'"}']
38+
text_without_prefix.gsub(/([^*]+)/, "'\\1'")
3939
else
4040
"'#{text_without_prefix}'*"
4141
end
4242
end
4343

4444
def text_without_prefix
45-
@text_without_prefix ||= text.split[1..].join ' '
45+
@text_without_prefix ||= text[/^#{prefix}\s*(.*)/, 1]
4646
end
4747

4848
def compgen

spec/completely/integration.yml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ftp:
99
expected: [download]
1010

1111
- compline: "ftp download "
12-
expected: [another-dir, dir with spaces, dummy-dir, file with spaces.txt, ftp.yaml, gradual.yaml]
12+
expected: [another-dir, dir with spaces, dummy-dir, file with spaces.txt, ftp.yaml, gradual.yaml, wildcard.yaml]
1313

1414
- compline: "ftp download -"
1515
expected: [--help, --override]
@@ -70,4 +70,27 @@ gradual:
7070
expected: [blue, red]
7171

7272
- compline: "cli command subcommand anything --color gr"
73-
expected: [gray, green]
73+
expected: [gray, green]
74+
75+
wildcard:
76+
- compline: "wildcard "
77+
expected: [download, upload]
78+
79+
- compline: "wildcard d"
80+
expected: [download]
81+
82+
- compline: "wildcard --"
83+
expected: [--debug, --env]
84+
85+
- compline: "wildcard download --"
86+
expected: [--confirm, --contest]
87+
88+
- compline: "wildcard --env "
89+
expected: [dev, prod]
90+
91+
- compline: "wildcard --env prod download --contest "
92+
expected: [everything, nothing]
93+
94+
- compline: "wildcard download --contest "
95+
expected: [everything, nothing]
96+

spec/completely/pattern_spec.rb

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,25 @@
4040
end
4141

4242
describe '#prefix' do
43-
it 'returns the first word from text' do
43+
it 'returns the first word of the pattern' do
4444
expect(subject.prefix).to eq 'git'
4545
end
46+
47+
context 'when the pattern includes a * right after the first word' do
48+
let(:text) { 'git*--checkout' }
49+
50+
it 'returns the first word of the pattern' do
51+
expect(subject.prefix).to eq 'git'
52+
end
53+
end
54+
55+
context 'when the pattern includes a * anywhere else' do
56+
let(:text) { 'git --checkout*something' }
57+
58+
it 'returns the first word of the pattern' do
59+
expect(subject.prefix).to eq 'git'
60+
end
61+
end
4662
end
4763

4864
describe '#case_string' do
@@ -68,9 +84,25 @@
6884
end
6985

7086
describe '#text_without_prefix' do
71-
it 'returns all but the first word from text' do
87+
it 'returns all but the first word' do
7288
expect(subject.text_without_prefix).to eq 'commit'
7389
end
90+
91+
context 'when the pattern includes a * right after the first word' do
92+
let(:text) { 'git*--checkout' }
93+
94+
it 'returns all but the first word' do
95+
expect(subject.text_without_prefix).to eq '*--checkout'
96+
end
97+
end
98+
99+
context 'when the pattern includes a * anywhere else' do
100+
let(:text) { 'git --checkout*something' }
101+
102+
it 'returns all but the first word' do
103+
expect(subject.text_without_prefix).to eq '--checkout*something'
104+
end
105+
end
74106
end
75107

76108
describe '#compgen' do
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
wildcard:
2+
- --debug
3+
- --env
4+
- download
5+
- upload
6+
7+
wildcard*--env:
8+
- prod
9+
- dev
10+
11+
wildcard*download:
12+
- --confirm
13+
- --contest
14+
15+
wildcard*download*--contest:
16+
- everything
17+
- nothing

0 commit comments

Comments
 (0)