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
|
|
|
##
|
|
|
|
# Make the default UI accessable without the "ui." prefix. Classes
|
|
|
|
# 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
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
2009-06-09 17:38:59 -04:00
|
|
|
# :method: alert
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
##
|
2009-06-09 17:38:59 -04:00
|
|
|
# :method: alert_error
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# :method: alert_warning
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# :method: ask
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# :method: ask_yes_no
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# :method: choose_from_list
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# :method: say
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# :method: terminate_interaction
|
|
|
|
|
|
|
|
[:alert,
|
|
|
|
:alert_error,
|
|
|
|
:alert_warning,
|
|
|
|
:ask,
|
2010-02-21 21:52:35 -05:00
|
|
|
:ask_for_password,
|
2009-06-09 17:38:59 -04:00
|
|
|
:ask_yes_no,
|
|
|
|
:choose_from_list,
|
|
|
|
:say,
|
|
|
|
:terminate_interaction ].each do |methname|
|
|
|
|
class_eval %{
|
|
|
|
def #{methname}(*args)
|
|
|
|
ui.#{methname}(*args)
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
2009-06-09 17:38:59 -04:00
|
|
|
}, __FILE__, __LINE__
|
|
|
|
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
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def initialize(in_stream, out_stream, err_stream=STDERR)
|
|
|
|
@ins = in_stream
|
|
|
|
@outs = out_stream
|
|
|
|
@errs = err_stream
|
|
|
|
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)
|
|
|
|
unless @ins.tty? then
|
|
|
|
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
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
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
|
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)
|
|
|
|
return nil if not @ins.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
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
##
|
|
|
|
# Ask for a password. Does not echo response to terminal.
|
|
|
|
|
|
|
|
def ask_for_password(question)
|
|
|
|
return nil if not @ins.tty?
|
|
|
|
|
2010-05-08 00:11:30 -04:00
|
|
|
require 'io/console'
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
@outs.print(question + " ")
|
|
|
|
@outs.flush
|
|
|
|
|
2010-05-08 00:11:30 -04:00
|
|
|
password = @ins.noecho {@ins.gets}
|
2010-02-21 21:52:35 -05:00
|
|
|
password.chomp! if password
|
|
|
|
password
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
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
|
|
|
|
super STDIN, STDOUT, STDERR
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# SilentUI is a UI choice that is absolutely silent.
|
|
|
|
|
|
|
|
class Gem::SilentUI
|
|
|
|
def method_missing(sym, *args, &block)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|