Class: Gem::StreamUI

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems/user_interaction.rb

Overview

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

Direct Known Subclasses

ConsoleUI, MockGemUi, SilentUI

Defined Under Namespace

Classes: SilentDownloadReporter, SilentProgressReporter, SimpleProgressReporter, VerboseDownloadReporter, VerboseProgressReporter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(in_stream, out_stream, err_stream = STDERR, usetty = true) ⇒ StreamUI

Creates a new StreamUI wrapping in_stream for user input, out_stream for standard output, err_stream for error output. If usetty is true then special operations (like asking for passwords) will use the TTY commands to disable character echo.



199
200
201
202
203
204
# File 'lib/rubygems/user_interaction.rb', line 199

def initialize(in_stream, out_stream, err_stream=STDERR, usetty=true)
  @ins = in_stream
  @outs = out_stream
  @errs = err_stream
  @usetty = usetty
end

Instance Attribute Details

#errsObject (readonly)

The error stream



191
192
193
# File 'lib/rubygems/user_interaction.rb', line 191

def errs
  @errs
end

#insObject (readonly)

The input stream



181
182
183
# File 'lib/rubygems/user_interaction.rb', line 181

def ins
  @ins
end

#outsObject (readonly)

The output stream



186
187
188
# File 'lib/rubygems/user_interaction.rb', line 186

def outs
  @outs
end

Instance Method Details

#_gets_noechoObject



318
319
320
# File 'lib/rubygems/user_interaction.rb', line 318

def _gets_noecho
  @ins.noecho {@ins.gets}
end

#alert(statement, question = nil) ⇒ Object

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



357
358
359
360
# File 'lib/rubygems/user_interaction.rb', line 357

def alert(statement, question=nil)
  @outs.puts "INFO:  #{statement}"
  ask(question) if question
end

#alert_error(statement, question = nil) ⇒ Object

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



374
375
376
377
# File 'lib/rubygems/user_interaction.rb', line 374

def alert_error(statement, question=nil)
  @errs.puts "ERROR:  #{statement}"
  ask(question) if question
end

#alert_warning(statement, question = nil) ⇒ Object

Display a warning on stderr. Will ask question if it is not nil.



365
366
367
368
# File 'lib/rubygems/user_interaction.rb', line 365

def alert_warning(statement, question=nil)
  @errs.puts "WARNING:  #{statement}"
  ask(question) if question
end

#ask(question) ⇒ Object

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



291
292
293
294
295
296
297
298
299
300
# File 'lib/rubygems/user_interaction.rb', line 291

def ask(question)
  return nil if not tty?

  @outs.print(question + "  ")
  @outs.flush

  result = @ins.gets
  result.chomp! if result
  result
end

#ask_for_password(question) ⇒ Object

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



305
306
307
308
309
310
311
312
313
314
315
# File 'lib/rubygems/user_interaction.rb', line 305

def ask_for_password(question)
  return nil if not tty?

  @outs.print(question, "  ")
  @outs.flush

  password = _gets_noecho
  @outs.puts
  password.chomp! if password
  password
end

#ask_yes_no(question, default = nil) ⇒ Object

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.



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/rubygems/user_interaction.rb', line 255

def ask_yes_no(question, default=nil)
  unless tty? then
    if default.nil? then
      raise Gem::OperationNotSupportedError,
            "Not connected to a tty and no default specified"
    else
      return default
    end
  end

  default_answer = case default
                   when nil
                     'yn'
                   when true
                     'Yn'
                   else
                     'yN'
                   end

  result = nil

  while result.nil? do
    result = case ask "#{question} [#{default_answer}]"
             when /^y/i then true
             when /^n/i then false
             when /^$/  then default
             else            nil
             end
  end

  return result
end

#backtrace(exception) ⇒ Object

Prints a formatted backtrace to the errors stream if backtraces are enabled.



221
222
223
224
225
# File 'lib/rubygems/user_interaction.rb', line 221

def backtrace exception
  return unless Gem.configuration.backtrace

  @errs.puts "\t#{exception.backtrace.join "\n\t"}"
end

#choose_from_list(question, list) ⇒ Object

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



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/rubygems/user_interaction.rb', line 232

def choose_from_list(question, list)
  @outs.puts question

  list.each_with_index do |item, index|
    @outs.puts " #{index+1}. #{item}"
  end

  @outs.print "> "
  @outs.flush

  result = @ins.gets

  return nil, nil unless result

  result = result.strip.to_i - 1
  return list[result], result
end

#closeObject



395
396
# File 'lib/rubygems/user_interaction.rb', line 395

def close
end

#debug(statement) ⇒ Object

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



382
383
384
# File 'lib/rubygems/user_interaction.rb', line 382

def debug(statement)
  @errs.puts statement
end

#download_reporter(*args) ⇒ Object

Return a download reporter object chosen from the current verbosity



534
535
536
537
538
539
540
541
# File 'lib/rubygems/user_interaction.rb', line 534

def download_reporter(*args)
  case Gem.configuration.verbose
  when nil, false
    SilentDownloadReporter.new(@outs, *args)
  else
    VerboseDownloadReporter.new(@outs, *args)
  end
end

#progress_reporter(*args) ⇒ Object

Return a progress reporter object chosen from the current verbosity.



401
402
403
404
405
406
407
408
409
410
# File 'lib/rubygems/user_interaction.rb', line 401

def progress_reporter(*args)
  case Gem.configuration.verbose
  when nil, false
    SilentProgressReporter.new(@outs, *args)
  when true
    SimpleProgressReporter.new(@outs, *args)
  else
    VerboseProgressReporter.new(@outs, *args)
  end
end

#say(statement = "") ⇒ Object

Display a statement.



350
351
352
# File 'lib/rubygems/user_interaction.rb', line 350

def say(statement="")
  @outs.puts statement
end

#terminate_interaction(status = 0) ⇒ Object

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



390
391
392
393
# File 'lib/rubygems/user_interaction.rb', line 390

def terminate_interaction(status = 0)
  close
  raise Gem::SystemExitException, status
end

#tty?Boolean

Returns true if TTY methods should be used on this StreamUI.

Returns:

  • (Boolean)


209
210
211
212
213
214
215
# File 'lib/rubygems/user_interaction.rb', line 209

def tty?
  if RUBY_VERSION < '1.9.3' and RUBY_PLATFORM =~ /mingw|mswin/ then
    @usetty
  else
    @usetty && @ins.tty?
  end
end