Class Gem::StreamUI
In: lib/rubygems/user_interaction.rb
Parent: Object

Gem::StreamUI implements a simple stream based user interface.

Methods

Classes and Modules

Class Gem::StreamUI::SilentDownloadReporter
Class Gem::StreamUI::SilentProgressReporter
Class Gem::StreamUI::SimpleProgressReporter
Class Gem::StreamUI::VerboseDownloadReporter
Class Gem::StreamUI::VerboseProgressReporter

Attributes

errs  [R] 
ins  [R] 
outs  [R] 

Public Class methods

[Source]

     # 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

Public Instance methods

Display an informational alert. Will ask question if it is not nil.

[Source]

     # File lib/rubygems/user_interaction.rb, line 293
293:   def alert(statement, question=nil)
294:     @outs.puts "INFO:  #{statement}"
295:     ask(question) if question
296:   end

Display an error message in a location expected to get error messages. Will ask question if it is not nil.

[Source]

     # File lib/rubygems/user_interaction.rb, line 311
311:   def alert_error(statement, question=nil)
312:     @errs.puts "ERROR:  #{statement}"
313:     ask(question) if question
314:   end

Display a warning in a location expected to get error messages. Will ask question if it is not nil.

[Source]

     # File lib/rubygems/user_interaction.rb, line 302
302:   def alert_warning(statement, question=nil)
303:     @errs.puts "WARNING:  #{statement}"
304:     ask(question) if question
305:   end

Ask a question. Returns an answer if connected to a tty, nil otherwise.

[Source]

     # 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.

[Source]

     # File lib/rubygems/user_interaction.rb, line 225
225:     def ask_for_password(question)
226:       return nil if not @ins.tty?
227: 
228:       require 'io/console'
229: 
230:       @outs.print(question + "  ")
231:       @outs.flush
232: 
233:       password = @ins.noecho {@ins.gets}
234:       password.chomp! if password
235:       password
236:     end

Ask for a password. Does not echo response to terminal.

[Source]

     # File lib/rubygems/user_interaction.rb, line 241
241:     def ask_for_password(question)
242:       return nil if not @ins.tty?
243: 
244:       @outs.print(question + "  ")
245:       @outs.flush
246: 
247:       Gem.win_platform? ? ask_for_password_on_windows : ask_for_password_on_unix
248:     end

Asks for a password that works on unix

[Source]

     # File lib/rubygems/user_interaction.rb, line 274
274:     def ask_for_password_on_unix
275:       system "stty -echo"
276:       password = @ins.gets
277:       password.chomp! if password
278:       system "stty echo"
279:       password
280:     end

Asks for a password that works on windows. Ripped from the Heroku gem.

[Source]

     # File lib/rubygems/user_interaction.rb, line 253
253:     def ask_for_password_on_windows
254:       require "Win32API"
255:       char = nil
256:       password = ''
257: 
258:       while char = Win32API.new("crtdll", "_getch", [ ], "L").Call do
259:         break if char == 10 || char == 13 # received carriage return or newline
260:         if char == 127 || char == 8 # backspace and delete
261:           password.slice!(-1, 1)
262:         else
263:           password << char.chr
264:         end
265:       end
266: 
267:       puts
268:       password
269:     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.

[Source]

     # 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].

[Source]

     # 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

Display a debug message on the same location as error messages.

[Source]

     # File lib/rubygems/user_interaction.rb, line 319
319:   def debug(statement)
320:     @errs.puts statement
321:   end

Return a download reporter object chosen from the current verbosity

[Source]

     # File lib/rubygems/user_interaction.rb, line 436
436:   def download_reporter(*args)
437:     case Gem.configuration.verbose
438:     when nil, false
439:       SilentDownloadReporter.new(@outs, *args)
440:     else
441:       VerboseDownloadReporter.new(@outs, *args)
442:     end
443:   end

Return a progress reporter object chosen from the current verbosity.

[Source]

     # File lib/rubygems/user_interaction.rb, line 334
334:   def progress_reporter(*args)
335:     case Gem.configuration.verbose
336:     when nil, false
337:       SilentProgressReporter.new(@outs, *args)
338:     when true
339:       SimpleProgressReporter.new(@outs, *args)
340:     else
341:       VerboseProgressReporter.new(@outs, *args)
342:     end
343:   end

Display a statement.

[Source]

     # File lib/rubygems/user_interaction.rb, line 286
286:   def say(statement="")
287:     @outs.puts statement
288:   end

Terminate the application with exit code status, running any exit handlers that might have been defined.

[Source]

     # File lib/rubygems/user_interaction.rb, line 327
327:   def terminate_interaction(status = 0)
328:     raise Gem::SystemExitException, status
329:   end

[Validate]