easyprompt 0.1.2 → 0.1.3

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.
Files changed (3) hide show
  1. data/lib/easyprompt.rb +57 -33
  2. data/lib/easyprompt.rb~ +6 -5
  3. metadata +2 -2
data/lib/easyprompt.rb CHANGED
@@ -24,42 +24,30 @@ require 'contxtlservice'
24
24
  require 'observer'
25
25
 
26
26
  class EasyPrompt
27
- Version = '0.1.2'
27
+ Version = '0.1.3'
28
28
 
29
- def initialize; @stdout = MockableStdout.get_mockable_stdout; end
29
+ def initialize
30
+ @stdout = MockableStdout.get_mockable_stdout
31
+ @stdin = MockableStdin.get_mockable_stdin
32
+ end
30
33
 
31
34
  # Asks the user for input.
32
35
  # msg:: The prompt that tells the user what information to enter next.
33
36
  # default:: The default value that will be returned if the user enters a newline (usually by pressing "Enter") without typing any information. The default value will be displayed in square brackets after +msg+.
34
37
  # response_class:: The sort of value that EasyPrompt#ask should return. Valid response classes are:
35
38
  # [:string] This is the default.
39
+ # [:array] Takes an arbitrary number of lines entered by the user, and returns them as an array. The entry stops when the user enters a blank line.
36
40
  # [:boolean] Values will be turned into <tt>true</tt> or <tt>false</tt> depending on whether the user enters "y" or "n".
37
41
  # [:regexp] Values will be read as a regular expression source. If the user enters an invalid regexp source, EasyPrompt will re-prompt until a valid regexp source is entered.
38
42
  def ask( msg, default = nil, response_class = :string )
39
- success = false
40
- last_error = nil
41
- until success
42
- success = true
43
+ @success = false
44
+ @last_error = nil
45
+ until @success
46
+ @success = true
43
47
  full_msg = msg
44
- full_msg = last_error + ' ' + full_msg if last_error
48
+ full_msg = @last_error + ' ' + full_msg if @last_error
45
49
  @stdout.write( prompt( full_msg, default, response_class ) + ' ' )
46
- stdin = MockableStdin.get_mockable_stdin
47
- response = stdin.gets
48
- response.chomp!
49
- if response == ''
50
- response = default
51
- else
52
- if response_class == :boolean
53
- response = response =~ /^y/i
54
- elsif response_class == :regexp
55
- begin
56
- response = Regexp.new response
57
- rescue RegexpError
58
- success = false
59
- last_error = "I'm sorry, but that was not a valid regular expression."
60
- end
61
- end
62
- end
50
+ response = try_parse_response( default, response_class )
63
51
  end
64
52
  response
65
53
  end
@@ -78,6 +66,40 @@ class EasyPrompt
78
66
  end
79
67
  prompt
80
68
  end
69
+
70
+ def try_parse_array_response( response ) #:nodoc:
71
+ response = [ response ]
72
+ while ( ( next_line = @stdin.gets ) != "\n" )
73
+ response << next_line.chomp
74
+ end
75
+ response
76
+ end
77
+
78
+ def try_parse_regexp_response( response ) #:nodoc:
79
+ begin
80
+ Regexp.new response
81
+ rescue RegexpError
82
+ @success = false
83
+ @last_error = "I'm sorry, but that was not a valid regular expression."
84
+ nil
85
+ end
86
+ end
87
+
88
+ def try_parse_response( default, response_class ) #:nodoc:
89
+ response = @stdin.gets
90
+ response.chomp!
91
+ if response == ''
92
+ default
93
+ else
94
+ if response_class == :boolean
95
+ response =~ /^y/i
96
+ elsif response_class == :regexp
97
+ try_parse_regexp_response response
98
+ elsif response_class == :array
99
+ try_parse_array_response response
100
+ end
101
+ end
102
+ end
81
103
 
82
104
  # The MockCommandLineUser can be used if you want to unit-test the command-line part of a program.
83
105
  #
@@ -139,13 +161,17 @@ class EasyPrompt
139
161
 
140
162
  def match_regexp #:nodoc:
141
163
  arg = @mock_stdout.string
142
- @mock_stdout.string = ''
143
- matching = @responses.map { |regexp, response_pair |
144
- ( loc = arg =~ regexp ) ? [ regexp, loc ] : nil
145
- }
146
- matching.compact!
147
- fail "Can't match \"#{ arg }\"" if matching.empty?
148
- ( matching.sort_by { |regexp, loc| loc } ).last.first
164
+ if arg != ''
165
+ @mock_stdout.string = ''
166
+ matching = @responses.map { |regexp, response_pair |
167
+ ( loc = arg =~ regexp ) ? [ regexp, loc ] : nil
168
+ }
169
+ matching.compact!
170
+ fail "Can't match \"#{ arg }\"" if matching.empty?
171
+ ( matching.sort_by { |regexp, loc| loc } ).last.first
172
+ else
173
+ nil
174
+ end
149
175
  end
150
176
 
151
177
  def respond( response ) #:nodoc:
@@ -171,8 +197,6 @@ class EasyPrompt
171
197
  @match_count[regexp] += 1
172
198
  elsif regexp
173
199
  raise "Exceeded limit of #{ @responses[regexp].last } for #{ regexp }"
174
- else
175
- raise "Can't find a response for " + arg
176
200
  end
177
201
  end
178
202
  end
data/lib/easyprompt.rb~ CHANGED
@@ -20,10 +20,11 @@
20
20
  #
21
21
  # The Rubyforge project page can be found at http://rubyforge.org/projects/easyprompt/ .
22
22
 
23
- require 'lafcadio'
23
+ require 'contxtlservice'
24
+ require 'observer'
24
25
 
25
26
  class EasyPrompt
26
- Version = '0.1.1'
27
+ Version = '0.1.2'
27
28
 
28
29
  def initialize; @stdout = MockableStdout.get_mockable_stdout; end
29
30
 
@@ -124,7 +125,7 @@ class EasyPrompt
124
125
  def initialize
125
126
  @responses = {}
126
127
  flush
127
- context = Lafcadio::Context.instance
128
+ context = ContextualService::Context.instance
128
129
  @mock_stdin = StringIO.new
129
130
  @mock_stdin.add_observer( self )
130
131
  context.set_resource( MockableStdin, @mock_stdin )
@@ -176,13 +177,13 @@ class EasyPrompt
176
177
  end
177
178
  end
178
179
 
179
- class MockableStdin < Lafcadio::ContextualService #:nodoc:
180
+ class MockableStdin < ContextualService::Service #:nodoc:
180
181
  def method_missing( symbol, *args )
181
182
  $stdin.send( symbol, *args )
182
183
  end
183
184
  end
184
185
 
185
- class MockableStdout < Lafcadio::ContextualService #:nodoc:
186
+ class MockableStdout < ContextualService::Service #:nodoc:
186
187
  def method_missing( symbol, *args )
187
188
  $stdout.send( symbol, *args )
188
189
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.6
3
3
  specification_version: 1
4
4
  name: easyprompt
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.2
7
- date: 2005-08-09
6
+ version: 0.1.3
7
+ date: 2005-09-14
8
8
  summary: EasyPrompt is a utility for command-line scripts.
9
9
  require_paths:
10
10
  - lib