Support for the Ruby 2.4 series has ended. See here for reference.
Gem::StreamUI
implements a simple stream based user interface.
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.
# File rubygems/user_interaction.rb, line 202 def initialize(in_stream, out_stream, err_stream=STDERR, usetty=true) @ins = in_stream @outs = out_stream @errs = err_stream @usetty = usetty end
# File rubygems/user_interaction.rb, line 321 def _gets_noecho @ins.noecho {@ins.gets} end
Display an informational alert. Will ask question
if it is not nil.
# File rubygems/user_interaction.rb, line 360 def alert(statement, question=nil) @outs.puts "INFO: #{statement}" ask(question) if question end
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 377 def alert_error(statement, question=nil) @errs.puts "ERROR: #{statement}" ask(question) if question end
Display a warning on stderr. Will ask question
if it is not nil.
# File rubygems/user_interaction.rb, line 368 def alert_warning(statement, question=nil) @errs.puts "WARNING: #{statement}" ask(question) if question end
Ask a question. Returns an answer if connected to a tty, nil otherwise.
# File rubygems/user_interaction.rb, line 294 def ask(question) return nil if not tty? @outs.print(question + " ") @outs.flush result = @ins.gets result.chomp! if result result end
Ask for a password. Does not echo response to terminal.
# File rubygems/user_interaction.rb, line 308 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 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 258 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
Prints a formatted backtrace to the errors stream if backtraces are enabled.
# File rubygems/user_interaction.rb, line 224 def backtrace exception return unless Gem.configuration.backtrace @errs.puts "\t#{exception.backtrace.join "\n\t"}" end
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 235 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
Display a debug message on the same location as error messages.
# File rubygems/user_interaction.rb, line 385 def debug(statement) @errs.puts statement end
Return a download reporter object chosen from the current verbosity
# File rubygems/user_interaction.rb, line 537 def download_reporter(*args) case Gem.configuration.verbose when nil, false SilentDownloadReporter.new(@outs, *args) else VerboseDownloadReporter.new(@outs, *args) end end
Return a progress reporter object chosen from the current verbosity.
# File rubygems/user_interaction.rb, line 404 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
Display a statement.
# File rubygems/user_interaction.rb, line 353 def say(statement="") @outs.puts statement end
Terminate the application with exit code status
, running any exit handlers that might have been defined.
# File rubygems/user_interaction.rb, line 393 def terminate_interaction(status = 0) close raise Gem::SystemExitException, status end
Returns true if TTY methods should be used on this StreamUI
.
# File rubygems/user_interaction.rb, line 212 def tty? if RUBY_VERSION < '1.9.3' and RUBY_PLATFORM =~ /mingw|mswin/ then @usetty else @usetty && @ins.tty? end end