Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more

In Files

  • rubygems/user_interaction.rb

Class/Module Index [+]

Quicksearch

Gem::StreamUI

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

Attributes

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

Public Class Methods

new(in_stream, out_stream, err_stream=STDERR) click to toggle source
 
               # File rubygems/user_interaction.rb, line 135
def initialize(in_stream, out_stream, err_stream=STDERR)
  @ins = in_stream
  @outs = out_stream
  @errs = err_stream
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 rubygems/user_interaction.rb, line 275
def alert(statement, question=nil)
  @outs.puts "INFO:  #{statement}"
  ask(question) if question
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 rubygems/user_interaction.rb, line 293
def alert_error(statement, question=nil)
  @errs.puts "ERROR:  #{statement}"
  ask(question) if question
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 rubygems/user_interaction.rb, line 284
def alert_warning(statement, question=nil)
  @errs.puts "WARNING:  #{statement}"
  ask(question) if question
end
            
ask(question) click to toggle source

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

 
               # File rubygems/user_interaction.rb, line 210
def ask(question)
  return nil if not @ins.tty?

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

  result = @ins.gets
  result.chomp! if result
  result
end
            
ask_for_password(question) click to toggle source

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

 
               # File rubygems/user_interaction.rb, line 224
def ask_for_password(question)
  return nil if not @ins.tty?

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

  Gem.win_platform? ? ask_for_password_on_windows : ask_for_password_on_unix
end
            
ask_for_password_on_unix() click to toggle source

Asks for a password that works on unix

 
               # File rubygems/user_interaction.rb, line 257
def ask_for_password_on_unix
  system "stty -echo"
  password = @ins.gets
  password.chomp! if password
  system "stty echo"
  password
end
            
ask_for_password_on_windows() click to toggle source

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

 
               # File rubygems/user_interaction.rb, line 236
def ask_for_password_on_windows
  require "Win32API"
  char = nil
  password = ''

  while char = Win32API.new("crtdll", "_getch", [ ], "L").Call do
    break if char == 10 || char == 13 # received carriage return or newline
    if char == 127 || char == 8 # backspace and delete
      password.slice!(-1, 1)
    else
      password << char.chr
    end
  end

  puts
  password
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 rubygems/user_interaction.rb, line 169
def ask_yes_no(question, default=nil)
  unless @ins.tty? then
    if default.nil? then
      raise Gem::OperationNotSupportedError,
            "Not connected to a tty and no default specified"
    else
      return default
    end
  end

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

  result = nil

  while result.nil?
    result = ask("#{question} [#{qstr}]")
    result = case result
    when /^[Yy].*/
      true
    when /^[Nn].*/
      false
    when /^$/
      default
    else
      nil
    end
  end

  return result
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 rubygems/user_interaction.rb, line 146
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
            
debug(statement) click to toggle source

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

 
               # File rubygems/user_interaction.rb, line 301
def debug(statement)
  @errs.puts statement
end
            
progress_reporter(*args) click to toggle source

Return a progress reporter object chosen from the current verbosity.

 
               # File rubygems/user_interaction.rb, line 316
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="") click to toggle source

Display a statement.

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

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus