| Class | Gem::StreamUI |
| In: |
lib/rubygems/user_interaction.rb
|
| Parent: | Object |
Gem::StreamUI implements a simple stream based user interface.
| errs | [R] | |
| ins | [R] | |
| outs | [R] |
# File lib/rubygems/user_interaction.rb, line 135
135: def initialize(in_stream, out_stream, err_stream=STDERR)
136: @ins = in_stream
137: @outs = out_stream
138: @errs = err_stream
139: end
Ask a question. Returns an answer if connected to a tty, nil otherwise.
# File lib/rubygems/user_interaction.rb, line 210
210: def ask(question)
211: return nil if not @ins.tty?
212:
213: @outs.print(question + " ")
214: @outs.flush
215:
216: result = @ins.gets
217: result.chomp! if result
218: result
219: end
Ask for a password. Does not echo response to terminal.
# File lib/rubygems/user_interaction.rb, line 224
224: def ask_for_password(question)
225: return nil if not @ins.tty?
226:
227: @outs.print(question + " ")
228: @outs.flush
229:
230: Gem.win_platform? ? ask_for_password_on_windows : ask_for_password_on_unix
231: end
Asks for a password that works on unix
# File lib/rubygems/user_interaction.rb, line 257
257: def ask_for_password_on_unix
258: system "stty -echo"
259: password = @ins.gets
260: password.chomp! if password
261: system "stty echo"
262: password
263: end
Asks for a password that works on windows. Ripped from the Heroku gem.
# File lib/rubygems/user_interaction.rb, line 236
236: def ask_for_password_on_windows
237: require "Win32API"
238: char = nil
239: password = ''
240:
241: while char = Win32API.new("crtdll", "_getch", [ ], "L").Call do
242: break if char == 10 || char == 13 # received carriage return or newline
243: if char == 127 || char == 8 # backspace and delete
244: password.slice!(-1, 1)
245: else
246: password << char.chr
247: end
248: end
249:
250: puts
251: password
252: 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 169
169: def ask_yes_no(question, default=nil)
170: unless @ins.tty? then
171: if default.nil? then
172: raise Gem::OperationNotSupportedError,
173: "Not connected to a tty and no default specified"
174: else
175: return default
176: end
177: end
178:
179: qstr = case default
180: when nil
181: 'yn'
182: when true
183: 'Yn'
184: else
185: 'yN'
186: end
187:
188: result = nil
189:
190: while result.nil?
191: result = ask("#{question} [#{qstr}]")
192: result = case result
193: when /^[Yy].*/
194: true
195: when /^[Nn].*/
196: false
197: when /^$/
198: default
199: else
200: nil
201: end
202: end
203:
204: return result
205: 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 146
146: def choose_from_list(question, list)
147: @outs.puts question
148:
149: list.each_with_index do |item, index|
150: @outs.puts " #{index+1}. #{item}"
151: end
152:
153: @outs.print "> "
154: @outs.flush
155:
156: result = @ins.gets
157:
158: return nil, nil unless result
159:
160: result = result.strip.to_i - 1
161: return list[result], result
162: end
Return a progress reporter object chosen from the current verbosity.
# File lib/rubygems/user_interaction.rb, line 316
316: def progress_reporter(*args)
317: case Gem.configuration.verbose
318: when nil, false
319: SilentProgressReporter.new(@outs, *args)
320: when true
321: SimpleProgressReporter.new(@outs, *args)
322: else
323: VerboseProgressReporter.new(@outs, *args)
324: end
325: end
Display a statement.
# File lib/rubygems/user_interaction.rb, line 268
268: def say(statement="")
269: @outs.puts statement
270: end