123456789_123456789_123456789_123456789_123456789_

Class: Gem::StreamUI

Relationships & Source Files
Namespace Children
Classes:
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/rubygems/user_interaction.rb

Overview

StreamUI implements a simple stream based user interface.

Class Method Summary

Instance Attribute Summary

  • #errs readonly

    The error stream.

  • #ins readonly

    The input stream.

  • #outs readonly

    The output stream.

  • #tty? ⇒ Boolean readonly

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

Instance Method Summary

Constructor Details

.new(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.

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 197

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

#errs (readonly)

The error stream

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 189

attr_reader :errs

#ins (readonly)

The input stream

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 179

attr_reader :ins

#outs (readonly)

The output stream

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 184

attr_reader :outs

#tty?Boolean (readonly)

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

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 207

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

Instance Method Details

#_gets_noecho

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 325

def _gets_noecho
  require_io_console
  if IO.method_defined?(:noecho) then
    @ins.noecho {@ins.gets}
  elsif Gem.win_platform?
    require "Win32API"
    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
    password
  else
    system "stty -echo"
    begin
      @ins.gets
    ensure
      system "stty echo"
    end
  end
end

#alert(statement, question = nil)

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

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 362

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

#alert_error(statement, question = nil)

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

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 379

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

#alert_warning(statement, question = nil)

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

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 370

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

#ask(question)

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

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 289

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)

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

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 303

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)

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.

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 253

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)

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

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 219

def backtrace exception
  return unless Gem.configuration.backtrace

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

#choose_from_list(question, list)

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

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 230

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

#close

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 400

def close
end

#debug(statement)

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

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 387

def debug(statement)
  @errs.puts statement
end

#download_reporter(*args)

Return a download reporter object chosen from the current verbosity

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 539

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)

Return a progress reporter object chosen from the current verbosity.

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 406

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

#require_io_console

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 315

def require_io_console
  @require_io_console ||= begin
    begin
      require 'io/console'
    rescue LoadError
    end
    true
  end
end

#say(statement = "")

Display a statement.

[ GitHub ]

  
# File 'lib/rubygems/user_interaction.rb', line 355

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

#terminate_interaction(status = 0)

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

[ GitHub ]

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

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