prompt 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +11 -4
- data/examples/file_manager +3 -1
- data/lib/prompt/application.rb +3 -3
- data/lib/prompt/command.rb +19 -11
- data/lib/prompt/console/console_module.rb +2 -0
- data/lib/prompt/parameter.rb +3 -3
- data/lib/prompt/prompt_module.rb +1 -1
- data/spec/prompt/command_spec.rb +46 -22
- metadata +2 -2
data/CHANGELOG.md
CHANGED
@@ -2,14 +2,21 @@
|
|
2
2
|
|
3
3
|
This is updated when a new version is pushed to http://rubygems.org
|
4
4
|
|
5
|
-
## 0.
|
5
|
+
## 0.2.0 (Mar 17, 2012)
|
6
|
+
|
7
|
+
* Exit immediately on Ctrl-C
|
8
|
+
* Tab completing a `*param` will now include the completions for the following word
|
9
|
+
if the `*param` has already been matched at least once
|
10
|
+
* [BUG] Use the cached parameter values, when available
|
11
|
+
|
12
|
+
## 0.1.0 (Mar 8, 2012)
|
6
13
|
|
7
14
|
* DSL change: `desc` was renamed to `group`
|
8
15
|
* Tab completion will now list only the completions for the current word, instead of
|
9
16
|
the whole command. This makes it behave like bash and works much better with `*params`
|
10
17
|
* Hitting ENTER with an empty line will no longer print a Command Not Found error
|
11
18
|
|
12
|
-
## 0.0.3 (Feb 22,
|
19
|
+
## 0.0.3 (Feb 22, 2012)
|
13
20
|
|
14
21
|
* DSL change: `variable` & `dynamic_variable` were renamed to `param`
|
15
22
|
* A glob parameter (e.g. `*param`) will now return its matches as an array of strings
|
@@ -17,7 +24,7 @@ This is updated when a new version is pushed to http://rubygems.org
|
|
17
24
|
* Dynamic variables are cached until the next command is run
|
18
25
|
* Added LICENSE.txt
|
19
26
|
|
20
|
-
## 0.0.2 (Feb 17,
|
27
|
+
## 0.0.2 (Feb 17, 2012)
|
21
28
|
|
22
29
|
* Allow the command prompt to be changed while it's running
|
23
30
|
* Quoted strings will now match to a single `:param`
|
@@ -25,6 +32,6 @@ This is updated when a new version is pushed to http://rubygems.org
|
|
25
32
|
* [BUG] Don't memoize Command#regex. This prevented dynamic variables
|
26
33
|
from working correctly after the values changed
|
27
34
|
|
28
|
-
## 0.0.1 (Feb 16,
|
35
|
+
## 0.0.1 (Feb 16, 2012)
|
29
36
|
|
30
37
|
* Initial release
|
data/examples/file_manager
CHANGED
@@ -39,7 +39,9 @@ command "pwd", "Print current directory" do
|
|
39
39
|
end
|
40
40
|
|
41
41
|
param :file, "File" do
|
42
|
-
Dir.entries(@pwd)
|
42
|
+
Dir.entries(@pwd).select do |e|
|
43
|
+
File.file?(File.join(@pwd, e))
|
44
|
+
end
|
43
45
|
end
|
44
46
|
|
45
47
|
command "cp *file :dir", "Copy one or more files to dest" do |files, dir|
|
data/lib/prompt/application.rb
CHANGED
@@ -35,7 +35,7 @@ module Prompt
|
|
35
35
|
def completions line_starting_with, word_starting_with
|
36
36
|
args = Console.split(line_starting_with)
|
37
37
|
last_idx = index_of_last_word(line_starting_with)
|
38
|
-
|
38
|
+
all_completions(args[0,last_idx], word_starting_with)
|
39
39
|
end
|
40
40
|
|
41
41
|
private
|
@@ -58,9 +58,9 @@ module Prompt
|
|
58
58
|
@command_groups.map(&:commands).flatten(1)
|
59
59
|
end
|
60
60
|
|
61
|
-
def
|
61
|
+
def all_completions(args, partial_arg)
|
62
62
|
commands.select { |c| c.could_match? args }.map do |c|
|
63
|
-
c.
|
63
|
+
c.completions(args.length, partial_arg)
|
64
64
|
end.flatten(1)
|
65
65
|
end
|
66
66
|
|
data/lib/prompt/command.rb
CHANGED
@@ -46,22 +46,21 @@ module Prompt
|
|
46
46
|
|
47
47
|
def clear_cached_values
|
48
48
|
@parameters.each do |p|
|
49
|
-
p.
|
49
|
+
p.clear_cached_values if p.respond_to?(:clear_cached_values)
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
def
|
54
|
-
|
55
|
-
word = @words[
|
56
|
-
word = word || @words[word_idx]
|
57
|
-
|
53
|
+
def completions(word_idx, starting_with)
|
54
|
+
idx = @words[0...word_idx].find_index { |w| w.kind_of? MultiMatcher } || word_idx
|
55
|
+
word = @words[idx]
|
58
56
|
return [] if word.nil?
|
59
57
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
58
|
+
next_word = @words[idx+1]
|
59
|
+
|
60
|
+
if idx < word_idx and next_word
|
61
|
+
completions_for(word, starting_with) + completions_for(next_word, starting_with)
|
62
|
+
else
|
63
|
+
completions_for(word, starting_with)
|
65
64
|
end
|
66
65
|
end
|
67
66
|
|
@@ -96,5 +95,14 @@ module Prompt
|
|
96
95
|
end
|
97
96
|
end
|
98
97
|
|
98
|
+
def completions_for(word, starting_with)
|
99
|
+
case word
|
100
|
+
when Matcher
|
101
|
+
word.parameter.completions(starting_with)
|
102
|
+
when String
|
103
|
+
word.start_with?(starting_with) ? [word] : []
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
99
107
|
end
|
100
108
|
end
|
data/lib/prompt/parameter.rb
CHANGED
@@ -12,12 +12,12 @@ module Prompt
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def clear_cached_values
|
15
|
-
@
|
15
|
+
@cached_values = nil
|
16
16
|
end
|
17
17
|
|
18
|
-
def
|
18
|
+
def completions(starting_with = "")
|
19
19
|
all = if @proc
|
20
|
-
@
|
20
|
+
@cached_values ||= @proc.call
|
21
21
|
else
|
22
22
|
@values
|
23
23
|
end
|
data/lib/prompt/prompt_module.rb
CHANGED
data/spec/prompt/command_spec.rb
CHANGED
@@ -115,51 +115,75 @@ describe Prompt::Command do
|
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
|
-
describe "#
|
118
|
+
describe "#completions" do
|
119
119
|
|
120
120
|
it "expands correctly with no parameters" do
|
121
121
|
c = Command.new ["one"]
|
122
|
-
c.
|
123
|
-
c.
|
124
|
-
c.
|
125
|
-
c.
|
122
|
+
c.completions(0, "").should == ["one"]
|
123
|
+
c.completions(0, "on").should == ["one"]
|
124
|
+
c.completions(0, "z").should == []
|
125
|
+
c.completions(1, "").should == []
|
126
126
|
|
127
127
|
c = Command.new ["help", "-v"]
|
128
|
-
c.
|
129
|
-
c.
|
130
|
-
c.
|
131
|
-
c.
|
132
|
-
c.
|
133
|
-
c.
|
128
|
+
c.completions(0, "").should == ["help"]
|
129
|
+
c.completions(0, "he").should == ["help"]
|
130
|
+
c.completions(0, "z").should == []
|
131
|
+
c.completions(1, "").should == ["-v"]
|
132
|
+
c.completions(1, "-v").should == ["-v"]
|
133
|
+
c.completions(1, "z").should == []
|
134
134
|
end
|
135
135
|
|
136
136
|
it "expands correctly with undefined parameters" do
|
137
137
|
c = Command.new(["go", matcher(:dir)])
|
138
|
-
c.
|
139
|
-
c.
|
140
|
-
c.
|
141
|
-
c.
|
138
|
+
c.completions(0, "").should == ["go"]
|
139
|
+
c.completions(0, "g").should == ["go"]
|
140
|
+
c.completions(0, "go").should == ["go"]
|
141
|
+
c.completions(1, "").should == []
|
142
142
|
end
|
143
143
|
|
144
144
|
it "expands correctly with defined parameters" do
|
145
145
|
dir = Parameter.new(:dir, "", DIRECTIONS)
|
146
146
|
c = Command.new(["go", matcher(dir)])
|
147
|
-
c.
|
147
|
+
c.completions(1, "").should == DIRECTIONS
|
148
148
|
|
149
149
|
speed = Parameter.new(:speed, "", SPEEDS)
|
150
150
|
c = Command.new(["go", matcher(dir), matcher(speed)])
|
151
|
-
c.
|
152
|
-
c.
|
153
|
-
c.
|
151
|
+
c.completions(2, "").should == SPEEDS
|
152
|
+
c.completions(2, "slow").should == SPEEDS
|
153
|
+
c.completions(2, "slowe").should == ["slower"]
|
154
154
|
end
|
155
155
|
|
156
156
|
it "expands correctly if parameter values have spaces" do
|
157
157
|
speed = Parameter.new(:speed, "", ["fast", "very fast"])
|
158
158
|
c = Command.new(["go", matcher(speed)])
|
159
|
-
c.
|
160
|
-
c.
|
161
|
-
c.
|
159
|
+
c.completions(1, "").should == ['fast', 'very fast']
|
160
|
+
c.completions(1, "f").should == ['fast']
|
161
|
+
c.completions(1, "v").should == ['very fast']
|
162
162
|
end
|
163
|
+
|
164
|
+
it "expands correctly when *param is followed by :param" do
|
165
|
+
speed = Parameter.new(:speed, "", SPEEDS)
|
166
|
+
dir = Parameter.new(:dir, "", DIRECTIONS)
|
167
|
+
c = Command.new [multi_matcher(speed), matcher(dir)]
|
168
|
+
c.completions(0, "").should == SPEEDS
|
169
|
+
c.completions(1, "").should == (SPEEDS + DIRECTIONS)
|
170
|
+
c.completions(2, "").should == (SPEEDS + DIRECTIONS)
|
171
|
+
c.completions(1, "s").should == (SPEEDS + DIRECTIONS).grep(/^s/)
|
172
|
+
end
|
173
|
+
|
174
|
+
it "caches dynamic parameters" do
|
175
|
+
year = Parameter.new(:year, "", "") do
|
176
|
+
Range.new(1, 100).to_a.map &:to_s
|
177
|
+
end
|
178
|
+
c = Command.new [matcher(year)]
|
179
|
+
Range.should_receive(:new).once.and_return(["42"])
|
180
|
+
c.completions(0, "").should == ["42"]
|
181
|
+
c.completions(0, "").should == ["42"] # this time it should be cached
|
182
|
+
c.clear_cached_values
|
183
|
+
Range.should_receive(:new).once.and_return(["44"])
|
184
|
+
c.completions(0, "").should == ["44"]
|
185
|
+
end
|
186
|
+
|
163
187
|
end
|
164
188
|
|
165
189
|
describe "#usage" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prompt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-17 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Prompt makes it easy to build slick command-line applications with Tab
|
15
15
|
Completion, Command History, and Built-in Help
|