2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2007-11-10 02:48:56 -05:00
|
|
|
#--
|
|
|
|
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
|
|
|
|
# All rights reserved.
|
|
|
|
# See LICENSE.txt for permissions.
|
|
|
|
#++
|
|
|
|
|
2018-05-17 21:39:13 -04:00
|
|
|
require 'rubygems/deprecate'
|
2019-03-04 22:32:58 -05:00
|
|
|
require 'rubygems/text'
|
2016-04-27 20:33:08 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Module that defines the default UserInteraction. Any class including this
|
|
|
|
# module will have access to the +ui+ method that returns the default UI.
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
module Gem::DefaultUserInteraction
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2019-03-04 22:32:58 -05:00
|
|
|
include Gem::Text
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# The default UI is a class variable of the singleton class for this
|
|
|
|
# module.
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
@ui = nil
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Return the default UI.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def self.ui
|
|
|
|
@ui ||= Gem::ConsoleUI.new
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Set the default UI. If the default UI is never explicitly set, a simple
|
|
|
|
# console based UserInteraction will be used automatically.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def self.ui=(new_ui)
|
|
|
|
@ui = new_ui
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Use +new_ui+ for the duration of +block+.
|
|
|
|
|
|
|
|
def self.use_ui(new_ui)
|
|
|
|
old_ui = @ui
|
|
|
|
@ui = new_ui
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
@ui = old_ui
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# See DefaultUserInteraction::ui
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def ui
|
|
|
|
Gem::DefaultUserInteraction.ui
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# See DefaultUserInteraction::ui=
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def ui=(new_ui)
|
|
|
|
Gem::DefaultUserInteraction.ui = new_ui
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# See DefaultUserInteraction::use_ui
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def use_ui(new_ui, &block)
|
|
|
|
Gem::DefaultUserInteraction.use_ui(new_ui, &block)
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
2013-10-19 21:33:19 -04:00
|
|
|
# UserInteraction allows RubyGems to interact with the user through standard
|
|
|
|
# methods that can be replaced with more-specific UI methods for different
|
|
|
|
# displays.
|
|
|
|
#
|
|
|
|
# Since UserInteraction dispatches to a concrete UI class you may need to
|
|
|
|
# reference other classes for specific behavior such as Gem::ConsoleUI or
|
|
|
|
# Gem::SilentUI.
|
2009-06-09 17:38:59 -04:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# class X
|
|
|
|
# include Gem::UserInteraction
|
|
|
|
#
|
|
|
|
# def get_answer
|
|
|
|
# n = ask("What is the meaning of life?")
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
module Gem::UserInteraction
|
|
|
|
|
|
|
|
include Gem::DefaultUserInteraction
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Displays an alert +statement+. Asks a +question+ if given.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def alert(statement, question = nil)
|
2013-10-19 21:33:19 -04:00
|
|
|
ui.alert statement, question
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Displays an error +statement+ to the error output location. Asks a
|
|
|
|
# +question+ if given.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def alert_error(statement, question = nil)
|
2013-10-19 21:33:19 -04:00
|
|
|
ui.alert_error statement, question
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Displays a warning +statement+ to the warning output location. Asks a
|
|
|
|
# +question+ if given.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def alert_warning(statement, question = nil)
|
2013-10-19 21:33:19 -04:00
|
|
|
ui.alert_warning statement, question
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Asks a +question+ and returns the answer.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def ask(question)
|
2013-10-19 21:33:19 -04:00
|
|
|
ui.ask question
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Asks for a password with a +prompt+
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def ask_for_password(prompt)
|
2013-10-19 21:33:19 -04:00
|
|
|
ui.ask_for_password prompt
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Asks a yes or no +question+. Returns true for yes, false for no.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def ask_yes_no(question, default = nil)
|
2013-10-19 21:33:19 -04:00
|
|
|
ui.ask_yes_no question, default
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Asks the user to answer +question+ with an answer from the given +list+.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def choose_from_list(question, list)
|
2013-10-19 21:33:19 -04:00
|
|
|
ui.choose_from_list question, list
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Displays the given +statement+ on the standard output (or equivalent).
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def say(statement = '')
|
2013-10-19 21:33:19 -04:00
|
|
|
ui.say statement
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Terminates the RubyGems process with the given +exit_code+
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def terminate_interaction(exit_code = 0)
|
2013-10-19 21:33:19 -04:00
|
|
|
ui.terminate_interaction exit_code
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2014-09-13 23:30:02 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Calls +say+ with +msg+ or the results of the block if really_verbose
|
|
|
|
# is true.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def verbose(msg = nil)
|
2019-03-04 22:32:58 -05:00
|
|
|
say(clean_text(msg || yield)) if Gem.configuration.really_verbose
|
2014-09-13 23:30:02 -04:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Gem::StreamUI implements a simple stream based user interface.
|
|
|
|
|
|
|
|
class Gem::StreamUI
|
2018-05-17 21:39:13 -04:00
|
|
|
extend Gem::Deprecate
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# The input stream
|
|
|
|
|
|
|
|
attr_reader :ins
|
|
|
|
|
|
|
|
##
|
|
|
|
# The output stream
|
|
|
|
|
|
|
|
attr_reader :outs
|
|
|
|
|
|
|
|
##
|
|
|
|
# The error stream
|
|
|
|
|
|
|
|
attr_reader :errs
|
|
|
|
|
|
|
|
##
|
|
|
|
# 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.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-03-09 17:32:29 -05:00
|
|
|
def initialize(in_stream, out_stream, err_stream=STDERR, usetty=true)
|
2009-06-09 17:38:59 -04:00
|
|
|
@ins = in_stream
|
|
|
|
@outs = out_stream
|
|
|
|
@errs = err_stream
|
2011-03-09 17:32:29 -05:00
|
|
|
@usetty = usetty
|
|
|
|
end
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Returns true if TTY methods should be used on this StreamUI.
|
|
|
|
|
2011-03-09 17:32:29 -05:00
|
|
|
def tty?
|
2018-05-30 09:01:35 -04:00
|
|
|
@usetty && @ins.tty?
|
2011-07-26 21:40:07 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
##
|
|
|
|
# Prints a formatted backtrace to the errors stream if backtraces are
|
|
|
|
# enabled.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def backtrace(exception)
|
2012-11-29 01:52:18 -05:00
|
|
|
return unless Gem.configuration.backtrace
|
|
|
|
|
|
|
|
@errs.puts "\t#{exception.backtrace.join "\n\t"}"
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# 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].
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def choose_from_list(question, list)
|
|
|
|
@outs.puts question
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
list.each_with_index do |item, index|
|
2019-02-14 07:59:03 -05:00
|
|
|
@outs.puts " #{index + 1}. #{item}"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
@outs.print "> "
|
|
|
|
@outs.flush
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
result = @ins.gets
|
|
|
|
|
|
|
|
return nil, nil unless result
|
|
|
|
|
|
|
|
result = result.strip.to_i - 1
|
|
|
|
return list[result], result
|
|
|
|
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.
|
|
|
|
|
|
|
|
def ask_yes_no(question, default=nil)
|
2018-11-21 05:20:47 -05:00
|
|
|
unless tty?
|
|
|
|
if default.nil?
|
2009-06-09 17:38:59 -04:00
|
|
|
raise Gem::OperationNotSupportedError,
|
|
|
|
"Not connected to a tty and no default specified"
|
|
|
|
else
|
|
|
|
return default
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
default_answer = case default
|
|
|
|
when nil
|
|
|
|
'yn'
|
|
|
|
when true
|
|
|
|
'Yn'
|
|
|
|
else
|
|
|
|
'yN'
|
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
|
|
|
|
result = nil
|
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
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
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
return result
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Ask a question. Returns an answer if connected to a tty, nil otherwise.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def ask(question)
|
2011-03-09 17:32:29 -05:00
|
|
|
return nil if not tty?
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
@outs.print(question + " ")
|
|
|
|
@outs.flush
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
result = @ins.gets
|
|
|
|
result.chomp! if result
|
|
|
|
result
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2013-12-12 19:51:04 -05:00
|
|
|
##
|
|
|
|
# Ask for a password. Does not echo response to terminal.
|
2011-01-18 19:08:49 -05:00
|
|
|
|
2013-12-12 19:51:04 -05:00
|
|
|
def ask_for_password(question)
|
|
|
|
return nil if not tty?
|
2011-01-18 19:08:49 -05:00
|
|
|
|
2013-12-12 19:51:04 -05:00
|
|
|
@outs.print(question, " ")
|
|
|
|
@outs.flush
|
2011-01-18 19:08:49 -05:00
|
|
|
|
2013-12-12 19:51:04 -05:00
|
|
|
password = _gets_noecho
|
|
|
|
@outs.puts
|
|
|
|
password.chomp! if password
|
|
|
|
password
|
|
|
|
end
|
2011-01-18 19:08:49 -05:00
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
def require_io_console
|
|
|
|
@require_io_console ||= begin
|
|
|
|
begin
|
|
|
|
require 'io/console'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
true
|
2016-09-27 21:16:43 -04:00
|
|
|
end
|
2017-10-07 21:32:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def _gets_noecho
|
|
|
|
require_io_console
|
2020-06-10 13:46:05 -04:00
|
|
|
@ins.noecho { @ins.gets }
|
2010-02-21 21:52:35 -05:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Display a statement.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def say(statement="")
|
|
|
|
@outs.puts statement
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Display an informational alert. Will ask +question+ if it is not nil.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def alert(statement, question=nil)
|
|
|
|
@outs.puts "INFO: #{statement}"
|
|
|
|
ask(question) if question
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
2013-10-19 21:33:19 -04:00
|
|
|
# Display a warning on stderr. Will ask +question+ if it is not nil.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def alert_warning(statement, question=nil)
|
|
|
|
@errs.puts "WARNING: #{statement}"
|
|
|
|
ask(question) if question
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Display an error message in a location expected to get error messages.
|
|
|
|
# Will ask +question+ if it is not nil.
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def alert_error(statement, question=nil)
|
|
|
|
@errs.puts "ERROR: #{statement}"
|
|
|
|
ask(question) if question
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Terminate the application with exit code +status+, running any exit
|
|
|
|
# handlers that might have been defined.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def terminate_interaction(status = 0)
|
2014-07-25 02:53:31 -04:00
|
|
|
close
|
2009-06-09 17:38:59 -04:00
|
|
|
raise Gem::SystemExitException, status
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2014-07-25 02:53:31 -04:00
|
|
|
def close
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Return a progress reporter object chosen from the current verbosity.
|
|
|
|
|
|
|
|
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
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# An absolutely silent progress reporter.
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
class SilentProgressReporter
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# The count of items is never updated for the silent progress reporter.
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
attr_reader :count
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Creates a silent progress reporter that ignores all input arguments.
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def initialize(out_stream, size, initial_message, terminal_message = nil)
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Does not print +message+ when updated as this object has taken a vow of
|
|
|
|
# silence.
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def updated(message)
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Does not print anything when complete as this object has taken a vow of
|
|
|
|
# silence.
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def done
|
|
|
|
end
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# A basic dotted progress reporter.
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
class SimpleProgressReporter
|
|
|
|
include Gem::DefaultUserInteraction
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# The number of progress items counted so far.
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
attr_reader :count
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Creates a new progress reporter that will write to +out_stream+ for
|
|
|
|
# +size+ items. Shows the given +initial_message+ when progress starts
|
|
|
|
# and the +terminal_message+ when it is complete.
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def initialize(out_stream, size, initial_message,
|
|
|
|
terminal_message = "complete")
|
|
|
|
@out = out_stream
|
|
|
|
@total = size
|
|
|
|
@count = 0
|
|
|
|
@terminal_message = terminal_message
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
@out.puts initial_message
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Prints out a dot and ignores +message+.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def updated(message)
|
|
|
|
@count += 1
|
|
|
|
@out.print "."
|
|
|
|
@out.flush
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
2009-06-09 17:38:59 -04:00
|
|
|
# Prints out the terminal message.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def done
|
|
|
|
@out.puts "\n#{@terminal_message}"
|
|
|
|
end
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# A progress reporter that prints out messages about the current progress.
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
class VerboseProgressReporter
|
|
|
|
include Gem::DefaultUserInteraction
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# The number of progress items counted so far.
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
attr_reader :count
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Creates a new progress reporter that will write to +out_stream+ for
|
|
|
|
# +size+ items. Shows the given +initial_message+ when progress starts
|
|
|
|
# and the +terminal_message+ when it is complete.
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def initialize(out_stream, size, initial_message,
|
|
|
|
terminal_message = 'complete')
|
|
|
|
@out = out_stream
|
|
|
|
@total = size
|
|
|
|
@count = 0
|
|
|
|
@terminal_message = terminal_message
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
@out.puts initial_message
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Prints out the position relative to the total and the +message+.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def updated(message)
|
|
|
|
@count += 1
|
|
|
|
@out.puts "#{@count}/#{@total}: #{message}"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Prints out the terminal message.
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def done
|
|
|
|
@out.puts @terminal_message
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
##
|
|
|
|
# Return a download reporter object chosen from the current verbosity
|
|
|
|
|
|
|
|
def download_reporter(*args)
|
2018-09-18 04:37:18 -04:00
|
|
|
if [nil, false].include?(Gem.configuration.verbose) || !@outs.tty?
|
2011-01-18 19:08:49 -05:00
|
|
|
SilentDownloadReporter.new(@outs, *args)
|
|
|
|
else
|
2018-09-18 04:37:18 -04:00
|
|
|
ThreadedDownloadReporter.new(@outs, *args)
|
2011-01-18 19:08:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# An absolutely silent download reporter.
|
|
|
|
|
|
|
|
class SilentDownloadReporter
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# The silent download reporter ignores all arguments
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
def initialize(out_stream, *args)
|
|
|
|
end
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# The silent download reporter does not display +filename+ or care about
|
|
|
|
# +filesize+ because it is silent.
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
def fetch(filename, filesize)
|
|
|
|
end
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Nothing can update the silent download reporter.
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
def update(current)
|
|
|
|
end
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# The silent download reporter won't tell you when the download is done.
|
|
|
|
# Because it is silent.
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
def done
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2018-09-18 04:37:18 -04:00
|
|
|
# A progress reporter that behaves nicely with threaded downloading.
|
2011-01-18 19:08:49 -05:00
|
|
|
|
2018-09-18 04:37:18 -04:00
|
|
|
class ThreadedDownloadReporter
|
|
|
|
MUTEX = Mutex.new
|
2013-10-19 21:33:19 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# The current file name being displayed
|
|
|
|
|
|
|
|
attr_reader :file_name
|
|
|
|
|
|
|
|
##
|
2018-09-18 04:37:18 -04:00
|
|
|
# Creates a new threaded download reporter that will display on
|
2013-10-19 21:33:19 -04:00
|
|
|
# +out_stream+. The other arguments are ignored.
|
2011-01-18 19:08:49 -05:00
|
|
|
|
|
|
|
def initialize(out_stream, *args)
|
2018-11-21 05:20:47 -05:00
|
|
|
@file_name = nil
|
2011-01-18 19:08:49 -05:00
|
|
|
@out = out_stream
|
|
|
|
end
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
2018-09-18 04:37:18 -04:00
|
|
|
# Tells the download reporter that the +file_name+ is being fetched.
|
|
|
|
# The other arguments are ignored.
|
2013-10-19 21:33:19 -04:00
|
|
|
|
2018-09-18 04:37:18 -04:00
|
|
|
def fetch(file_name, *args)
|
|
|
|
if @file_name.nil?
|
|
|
|
@file_name = file_name
|
|
|
|
locked_puts "Fetching #{@file_name}"
|
|
|
|
end
|
2011-01-18 19:08:49 -05:00
|
|
|
end
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
2018-09-18 04:37:18 -04:00
|
|
|
# Updates the threaded download reporter for the given number of +bytes+.
|
2013-10-19 21:33:19 -04:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
def update(bytes)
|
2018-09-18 04:37:18 -04:00
|
|
|
# Do nothing.
|
2011-01-18 19:08:49 -05:00
|
|
|
end
|
|
|
|
|
2013-10-19 21:33:19 -04:00
|
|
|
##
|
|
|
|
# Indicates the download is complete.
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
def done
|
2018-09-18 04:37:18 -04:00
|
|
|
# Do nothing.
|
2011-01-18 19:08:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2019-06-04 04:21:14 -04:00
|
|
|
|
2018-09-18 04:37:18 -04:00
|
|
|
def locked_puts(message)
|
|
|
|
MUTEX.synchronize do
|
|
|
|
@out.puts message
|
2011-01-18 19:08:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Subclass of StreamUI that instantiates the user interaction using STDIN,
|
|
|
|
# STDOUT, and STDERR.
|
|
|
|
|
|
|
|
class Gem::ConsoleUI < Gem::StreamUI
|
2013-11-25 14:14:49 -05:00
|
|
|
##
|
|
|
|
# The Console UI has no arguments as it defaults to reading input from
|
|
|
|
# stdin, output to stdout and warnings or errors to stderr.
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def initialize
|
2011-03-09 17:32:29 -05:00
|
|
|
super STDIN, STDOUT, STDERR, true
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# SilentUI is a UI choice that is absolutely silent.
|
|
|
|
|
2011-01-31 22:11:34 -05:00
|
|
|
class Gem::SilentUI < Gem::StreamUI
|
2013-11-25 14:14:49 -05:00
|
|
|
##
|
|
|
|
# The SilentUI has no arguments as it does not use any stream.
|
|
|
|
|
2011-01-31 22:11:34 -05:00
|
|
|
def initialize
|
|
|
|
reader, writer = nil, nil
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
reader = File.open(IO::NULL, 'r')
|
|
|
|
writer = File.open(IO::NULL, 'w')
|
2011-01-31 22:11:34 -05:00
|
|
|
|
2011-03-09 17:32:29 -05:00
|
|
|
super reader, writer, writer, false
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2014-07-25 02:53:31 -04:00
|
|
|
def close
|
|
|
|
super
|
|
|
|
@ins.close
|
|
|
|
@outs.close
|
|
|
|
end
|
|
|
|
|
2013-11-25 14:14:49 -05:00
|
|
|
def download_reporter(*args) # :nodoc:
|
2011-03-01 04:41:32 -05:00
|
|
|
SilentDownloadReporter.new(@outs, *args)
|
|
|
|
end
|
|
|
|
|
2013-11-25 14:14:49 -05:00
|
|
|
def progress_reporter(*args) # :nodoc:
|
2011-03-01 04:41:32 -05:00
|
|
|
SilentProgressReporter.new(@outs, *args)
|
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|