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) click to toggle 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

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 275
275:   def alert(statement, question=nil)
276:     @outs.puts "INFO:  #{statement}"
277:     ask(question) if question
278:   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 293
293:   def alert_error(statement, question=nil)
294:     @errs.puts "ERROR:  #{statement}"
295:     ask(question) if question
296:   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 284
284:   def alert_warning(statement, question=nil)
285:     @errs.puts "WARNING:  #{statement}"
286:     ask(question) if question
287:   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 @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_password(question) click to toggle source

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

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
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 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_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 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_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 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
debug(statement) click to toggle source

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

     # File lib/rubygems/user_interaction.rb, line 301
301:   def debug(statement)
302:     @errs.puts statement
303:   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 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
say(statement="") click to toggle source

Display a statement.

     # File lib/rubygems/user_interaction.rb, line 268
268:   def say(statement="")
269:     @outs.puts statement
270:   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 309
309:   def terminate_interaction(status = 0)
310:     raise Gem::SystemExitException, status
311:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.