| Class | Gem::StreamUI |
| In: |
lib/rubygems/user_interaction.rb
|
| Parent: | Object |
StreamUI implements a simple stream based user interface.
| errs | [R] | |
| ins | [R] | |
| outs | [R] |
# File lib/rubygems/user_interaction.rb, line 112
112: def initialize(in_stream, out_stream, err_stream=STDERR)
113: @ins = in_stream
114: @outs = out_stream
115: @errs = err_stream
116: end
Ask a question. Returns an answer if connected to a tty, nil otherwise.
# File lib/rubygems/user_interaction.rb, line 187
187: def ask(question)
188: return nil if not @ins.tty?
189:
190: @outs.print(question + " ")
191: @outs.flush
192:
193: result = @ins.gets
194: result.chomp! if result
195: result
196: end
Ask a question. Returns a true for yes, false for no. If not connected to a tty, raises an exception if default is nil, otherwise returns default.
# File lib/rubygems/user_interaction.rb, line 146
146: def ask_yes_no(question, default=nil)
147: unless @ins.tty? then
148: if default.nil? then
149: raise Gem::OperationNotSupportedError,
150: "Not connected to a tty and no default specified"
151: else
152: return default
153: end
154: end
155:
156: qstr = case default
157: when nil
158: 'yn'
159: when true
160: 'Yn'
161: else
162: 'yN'
163: end
164:
165: result = nil
166:
167: while result.nil?
168: result = ask("#{question} [#{qstr}]")
169: result = case result
170: when /^[Yy].*/
171: true
172: when /^[Nn].*/
173: false
174: when /^$/
175: default
176: else
177: nil
178: end
179: end
180:
181: return result
182: end
Choose from a list of options. question is a prompt displayed above the list. list is a list of option strings. Returns the pair [option_name, option_index].
# File lib/rubygems/user_interaction.rb, line 123
123: def choose_from_list(question, list)
124: @outs.puts question
125:
126: list.each_with_index do |item, index|
127: @outs.puts " #{index+1}. #{item}"
128: end
129:
130: @outs.print "> "
131: @outs.flush
132:
133: result = @ins.gets
134:
135: return nil, nil unless result
136:
137: result = result.strip.to_i - 1
138: return list[result], result
139: end
Return a progress reporter object chosen from the current verbosity.
# File lib/rubygems/user_interaction.rb, line 242
242: def progress_reporter(*args)
243: case Gem.configuration.verbose
244: when nil, false
245: SilentProgressReporter.new(@outs, *args)
246: when true
247: SimpleProgressReporter.new(@outs, *args)
248: else
249: VerboseProgressReporter.new(@outs, *args)
250: end
251: end
Display a statement.
# File lib/rubygems/user_interaction.rb, line 201
201: def say(statement="")
202: @outs.puts statement
203: end