prompt 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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.1.0 (Mar 8, 2011)
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, 2011)
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, 2011)
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, 2011)
35
+ ## 0.0.1 (Feb 16, 2012)
29
36
 
30
37
  * Initial release
@@ -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|
@@ -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
- all_expansions(args[0,last_idx], word_starting_with)
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 all_expansions(args, partial_arg)
61
+ def all_completions(args, partial_arg)
62
62
  commands.select { |c| c.could_match? args }.map do |c|
63
- c.expansions(args.length, partial_arg)
63
+ c.completions(args.length, partial_arg)
64
64
  end.flatten(1)
65
65
  end
66
66
 
@@ -46,22 +46,21 @@ module Prompt
46
46
 
47
47
  def clear_cached_values
48
48
  @parameters.each do |p|
49
- p.clear_cached_value if p.respond_to?(:clear_cached_value)
49
+ p.clear_cached_values if p.respond_to?(:clear_cached_values)
50
50
  end
51
51
  end
52
52
 
53
- def expansions(word_idx, starting_with)
54
- # TODO - combine glob parameters with any that follow
55
- word = @words[0..word_idx].find { |w| w.kind_of? MultiMatcher }
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
- case word
61
- when Matcher
62
- word.parameter.expansions(starting_with)
63
- when String
64
- word.start_with?(starting_with) ? [word] : []
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
@@ -21,6 +21,8 @@ module Prompt
21
21
  save_history history_file if history_file
22
22
  end
23
23
 
24
+ trap('INT') { puts; exit }
25
+
24
26
  Readline.completion_proc = CompletionProc
25
27
 
26
28
  load_history history_file if history_file
@@ -12,12 +12,12 @@ module Prompt
12
12
  end
13
13
 
14
14
  def clear_cached_values
15
- @cached_value = nil
15
+ @cached_values = nil
16
16
  end
17
17
 
18
- def expansions(starting_with = "")
18
+ def completions(starting_with = "")
19
19
  all = if @proc
20
- @cached_value = @proc.call
20
+ @cached_values ||= @proc.call
21
21
  else
22
22
  @values
23
23
  end
@@ -1,5 +1,5 @@
1
1
  module Prompt
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
 
4
4
  class << self
5
5
 
@@ -115,51 +115,75 @@ describe Prompt::Command do
115
115
  end
116
116
  end
117
117
 
118
- describe "#expansions" do
118
+ describe "#completions" do
119
119
 
120
120
  it "expands correctly with no parameters" do
121
121
  c = Command.new ["one"]
122
- c.expansions(0, "").should == ["one"]
123
- c.expansions(0, "on").should == ["one"]
124
- c.expansions(0, "z").should == []
125
- c.expansions(1, "").should == []
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.expansions(0, "").should == ["help"]
129
- c.expansions(0, "he").should == ["help"]
130
- c.expansions(0, "z").should == []
131
- c.expansions(1, "").should == ["-v"]
132
- c.expansions(1, "-v").should == ["-v"]
133
- c.expansions(1, "z").should == []
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.expansions(0, "").should == ["go"]
139
- c.expansions(0, "g").should == ["go"]
140
- c.expansions(0, "go").should == ["go"]
141
- c.expansions(1, "").should == []
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.expansions(1, "").should == DIRECTIONS
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.expansions(2, "").should == SPEEDS
152
- c.expansions(2, "slow").should == SPEEDS
153
- c.expansions(2, "slowe").should == ["slower"]
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.expansions(1, "").should == ['fast', 'very fast']
160
- c.expansions(1, "f").should == ['fast']
161
- c.expansions(1, "v").should == ['very fast']
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.1.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-08 00:00:00.000000000 Z
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