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.
|
|
|
|
#++
|
|
|
|
|
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
|
|
|
|
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
|
|
|
##
|
2011-01-28 18:46:47 -05:00
|
|
|
# Make the default UI accessible without the "ui." prefix. Classes
|
2009-06-09 17:38:59 -04:00
|
|
|
# including this module may use the interaction methods on the default UI
|
|
|
|
# directly. Classes may also reference the ui and ui= methods.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def alert(*args)
|
|
|
|
ui.alert(*args)
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def alert_error(*args)
|
|
|
|
ui.alert_error(*args)
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def alert_warning(*args)
|
|
|
|
ui.alert_warning(*args)
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def ask(*args)
|
|
|
|
ui.ask(*args)
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def ask_for_password(*args)
|
|
|
|
ui.ask_for_password(*args)
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def ask_yes_no(*args)
|
|
|
|
ui.ask_yes_no(*args)
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def choose_from_list(*args)
|
|
|
|
ui.choose_from_list(*args)
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def say(*args)
|
|
|
|
ui.say(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def terminate_interaction(*args)
|
|
|
|
ui.terminate_interaction(*args)
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Gem::StreamUI implements a simple stream based user interface.
|
|
|
|
|
|
|
|
class Gem::StreamUI
|
|
|
|
|
|
|
|
attr_reader :ins, :outs, :errs
|
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
|
|
|
|
|
|
|
|
def tty?
|
2011-07-28 01:26:20 -04:00
|
|
|
if RUBY_VERSION < '1.9.3' and RUBY_PLATFORM =~ /mingw|mswin/ then
|
2011-07-26 21:40:07 -04:00
|
|
|
@usetty
|
|
|
|
else
|
|
|
|
@usetty && @ins.tty?
|
|
|
|
end
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
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|
|
|
|
|
@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)
|
2011-03-09 17:32:29 -05:00
|
|
|
unless tty? then
|
2009-06-09 17:38:59 -04:00
|
|
|
if default.nil? then
|
|
|
|
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
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
if RUBY_VERSION > '1.9.2' then
|
2011-01-18 19:08:49 -05:00
|
|
|
##
|
|
|
|
# Ask for a password. Does not echo response to terminal.
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
def ask_for_password(question)
|
2011-03-09 17:32:29 -05:00
|
|
|
return nil if not tty?
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
require 'io/console'
|
2010-05-08 00:11:30 -04:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
@outs.print(question + " ")
|
|
|
|
@outs.flush
|
|
|
|
|
|
|
|
password = @ins.noecho {@ins.gets}
|
|
|
|
password.chomp! if password
|
|
|
|
password
|
|
|
|
end
|
|
|
|
else
|
|
|
|
##
|
|
|
|
# Ask for a password. Does not echo response to terminal.
|
|
|
|
|
|
|
|
def ask_for_password(question)
|
2011-03-09 17:32:29 -05:00
|
|
|
return nil if not tty?
|
2011-01-18 19:08:49 -05:00
|
|
|
|
|
|
|
@outs.print(question + " ")
|
|
|
|
@outs.flush
|
|
|
|
|
|
|
|
Gem.win_platform? ? ask_for_password_on_windows : ask_for_password_on_unix
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Asks for a password that works on windows. Ripped from the Heroku gem.
|
|
|
|
|
|
|
|
def ask_for_password_on_windows
|
2011-03-09 17:32:29 -05:00
|
|
|
return nil if not tty?
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
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
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
##
|
|
|
|
# Asks for a password that works on unix
|
|
|
|
|
|
|
|
def ask_for_password_on_unix
|
2011-03-09 17:32:29 -05:00
|
|
|
return nil if not tty?
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
system "stty -echo"
|
|
|
|
password = @ins.gets
|
|
|
|
password.chomp! if password
|
|
|
|
system "stty echo"
|
|
|
|
password
|
|
|
|
end
|
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
|
|
|
##
|
|
|
|
# Display a warning in a location expected to get error messages. 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
|
|
|
##
|
|
|
|
# Display a debug message on the same location as error messages.
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def debug(statement)
|
|
|
|
@errs.puts statement
|
|
|
|
end
|
2007-11-10 02:48:56 -05: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)
|
|
|
|
raise Gem::SystemExitException, status
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Return a progress reporter object chosen from the current verbosity.
|
|
|
|
|
|
|
|
def progress_reporter(*args)
|
2011-03-09 17:32:29 -05:00
|
|
|
if self.kind_of?(Gem::SilentUI)
|
|
|
|
return SilentProgressReporter.new(@outs, *args)
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
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
|
|
|
|
attr_reader :count
|
|
|
|
|
|
|
|
def initialize(out_stream, size, initial_message, terminal_message = nil)
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def updated(message)
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
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
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
include Gem::DefaultUserInteraction
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
attr_reader :count
|
2008-06-17 18:04:18 -04:00
|
|
|
|
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
|
2007-11-10 02:48:56 -05: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
|
|
|
##
|
|
|
|
# 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
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
include Gem::DefaultUserInteraction
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
attr_reader :count
|
2007-11-10 02:48:56 -05:00
|
|
|
|
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)
|
2011-03-09 17:32:29 -05:00
|
|
|
if self.kind_of?(Gem::SilentUI)
|
|
|
|
return SilentDownloadReporter.new(@outs, *args)
|
|
|
|
end
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
case Gem.configuration.verbose
|
|
|
|
when nil, false
|
|
|
|
SilentDownloadReporter.new(@outs, *args)
|
|
|
|
else
|
|
|
|
VerboseDownloadReporter.new(@outs, *args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# An absolutely silent download reporter.
|
|
|
|
|
|
|
|
class SilentDownloadReporter
|
|
|
|
def initialize(out_stream, *args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch(filename, filesize)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(current)
|
|
|
|
end
|
|
|
|
|
|
|
|
def done
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# A progress reporter that prints out messages about the current progress.
|
|
|
|
|
|
|
|
class VerboseDownloadReporter
|
|
|
|
attr_reader :file_name, :total_bytes, :progress
|
|
|
|
|
|
|
|
def initialize(out_stream, *args)
|
|
|
|
@out = out_stream
|
|
|
|
@progress = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch(file_name, total_bytes)
|
2011-01-28 18:46:47 -05:00
|
|
|
@file_name = file_name
|
|
|
|
@total_bytes = total_bytes.to_i
|
|
|
|
@units = @total_bytes.zero? ? 'B' : '%'
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
update_display(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(bytes)
|
2011-01-28 18:46:47 -05:00
|
|
|
new_progress = if @units == 'B' then
|
|
|
|
bytes
|
|
|
|
else
|
|
|
|
((bytes.to_f * 100) / total_bytes.to_f).ceil
|
|
|
|
end
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
return if new_progress == @progress
|
|
|
|
|
|
|
|
@progress = new_progress
|
|
|
|
update_display
|
|
|
|
end
|
|
|
|
|
|
|
|
def done
|
2011-01-28 18:46:47 -05:00
|
|
|
@progress = 100 if @units == '%'
|
2011-01-18 19:08:49 -05:00
|
|
|
update_display(true, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_display(show_progress = true, new_line = false)
|
|
|
|
return unless @out.tty?
|
2011-01-28 18:46:47 -05:00
|
|
|
|
|
|
|
if show_progress then
|
|
|
|
@out.print "\rFetching: %s (%3d%s)" % [@file_name, @progress, @units]
|
2011-01-18 19:08:49 -05:00
|
|
|
else
|
|
|
|
@out.print "Fetching: %s" % @file_name
|
|
|
|
end
|
|
|
|
@out.puts if new_line
|
|
|
|
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
|
|
|
|
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
|
|
|
|
def initialize
|
|
|
|
reader, writer = nil, nil
|
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
begin
|
2011-01-31 22:11:34 -05:00
|
|
|
reader = File.open('/dev/null', 'r')
|
|
|
|
writer = File.open('/dev/null', 'w')
|
2011-03-01 04:41:32 -05:00
|
|
|
rescue Errno::ENOENT
|
|
|
|
reader = File.open('nul', 'r')
|
|
|
|
writer = File.open('nul', 'w')
|
2011-01-31 22:11:34 -05:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
|
|
def download_reporter(*args)
|
|
|
|
SilentDownloadReporter.new(@outs, *args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def progress_reporter(*args)
|
|
|
|
SilentProgressReporter.new(@outs, *args)
|
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
|
|
|
|