Parent

Class Index [+]

Quicksearch

Gem::StreamUI

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

Attributes

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

Public Class Methods

new(in_stream, out_stream, err_stream=STDERR, usetty=true) click to toggle source
     # File lib/rubygems/user_interaction.rb, line 131
131:   def initialize(in_stream, out_stream, err_stream=STDERR, usetty=true)
132:     @ins = in_stream
133:     @outs = out_stream
134:     @errs = err_stream
135:     @usetty = usetty
136:   end

Public Instance Methods

alert(statement, question=nil) click to toggle source

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

     # File lib/rubygems/user_interaction.rb, line 297
297:   def alert(statement, question=nil)
298:     @outs.puts "INFO:  #{statement}"
299:     ask(question) if question
300:   end
alert_error(statement, question=nil) click to toggle source

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

     # File lib/rubygems/user_interaction.rb, line 315
315:   def alert_error(statement, question=nil)
316:     @errs.puts "ERROR:  #{statement}"
317:     ask(question) if question
318:   end
alert_warning(statement, question=nil) click to toggle source

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

     # File lib/rubygems/user_interaction.rb, line 306
306:   def alert_warning(statement, question=nil)
307:     @errs.puts "WARNING:  #{statement}"
308:     ask(question) if question
309:   end
ask(question) click to toggle source

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 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_password(question) click to toggle source

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

     # File lib/rubygems/user_interaction.rb, line 225
225:     def ask_for_password(question)
226:       return nil if not 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_password(question) click to toggle source

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

     # File lib/rubygems/user_interaction.rb, line 241
241:     def ask_for_password(question)
242:       return nil if not 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
ask_for_password_on_unix() click to toggle source

Asks for a password that works on unix

     # File lib/rubygems/user_interaction.rb, line 276
276:     def ask_for_password_on_unix
277:       return nil if not tty?
278: 
279:       system "stty -echo"
280:       password = @ins.gets
281:       password.chomp! if password
282:       system "stty echo"
283:       password
284:     end
ask_for_password_on_windows() click to toggle source

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

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

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 174
174:   def ask_yes_no(question, default=nil)
175:     unless tty? then
176:       if default.nil? then
177:         raise Gem::OperationNotSupportedError,
178:               "Not connected to a tty and no default specified"
179:       else
180:         return default
181:       end
182:     end
183: 
184:     default_answer = case default
185:                      when nil
186:                        'yn'
187:                      when true
188:                        'Yn'
189:                      else
190:                        'yN'
191:                      end
192: 
193:     result = nil
194: 
195:     while result.nil? do
196:       result = case ask "#{question} [#{default_answer}]"
197:                when /^y/ then true
198:                when /^n/ then false
199:                when /^$/  then default
200:                else            nil
201:                end
202:     end
203: 
204:     return result
205:   end
choose_from_list(question, list) click to toggle source

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 151
151:   def choose_from_list(question, list)
152:     @outs.puts question
153: 
154:     list.each_with_index do |item, index|
155:       @outs.puts " #{index+1}. #{item}"
156:     end
157: 
158:     @outs.print "> "
159:     @outs.flush
160: 
161:     result = @ins.gets
162: 
163:     return nil, nil unless result
164: 
165:     result = result.strip.to_i - 1
166:     return list[result], result
167:   end
debug(statement) click to toggle source

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

     # File lib/rubygems/user_interaction.rb, line 323
323:   def debug(statement)
324:     @errs.puts statement
325:   end
download_reporter(*args) click to toggle source

Return a download reporter object chosen from the current verbosity

     # File lib/rubygems/user_interaction.rb, line 444
444:   def download_reporter(*args)
445:     if self.kind_of?(Gem::SilentUI)
446:       return SilentDownloadReporter.new(@outs, *args)
447:     end
448: 
449:     case Gem.configuration.verbose
450:     when nil, false
451:       SilentDownloadReporter.new(@outs, *args)
452:     else
453:       VerboseDownloadReporter.new(@outs, *args)
454:     end
455:   end
progress_reporter(*args) click to toggle source

Return a progress reporter object chosen from the current verbosity.

     # File lib/rubygems/user_interaction.rb, line 338
338:   def progress_reporter(*args)
339:     if self.kind_of?(Gem::SilentUI)
340:       return SilentProgressReporter.new(@outs, *args)
341:     end
342: 
343:     case Gem.configuration.verbose
344:     when nil, false
345:       SilentProgressReporter.new(@outs, *args)
346:     when true
347:       SimpleProgressReporter.new(@outs, *args)
348:     else
349:       VerboseProgressReporter.new(@outs, *args)
350:     end
351:   end
say(statement="") click to toggle source

Display a statement.

     # File lib/rubygems/user_interaction.rb, line 290
290:   def say(statement="")
291:     @outs.puts statement
292:   end
terminate_interaction(status = 0) click to toggle source

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

     # File lib/rubygems/user_interaction.rb, line 331
331:   def terminate_interaction(status = 0)
332:     raise Gem::SystemExitException, status
333:   end
tty?() click to toggle source
     # File lib/rubygems/user_interaction.rb, line 138
138:   def tty?
139:     if RUBY_VERSION < '1.9.3' and RUBY_PLATFORM =~ /mingw|mswin/ then
140:       @usetty
141:     else
142:       @usetty && @ins.tty?
143:     end
144:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.