From 31c94ffeb5f09d09ac2c86fc9e6614e38251a43d Mon Sep 17 00:00:00 2001 From: drbrain Date: Tue, 9 Jun 2009 21:38:59 +0000 Subject: Update to RubyGems 1.3.4 r2223 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23659 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/rubygems/user_interaction.rb | 558 +++++++++++++++++++++------------------ 1 file changed, 294 insertions(+), 264 deletions(-) (limited to 'lib/rubygems/user_interaction.rb') diff --git a/lib/rubygems/user_interaction.rb b/lib/rubygems/user_interaction.rb index 30a728c597..6a80c7c0bc 100644 --- a/lib/rubygems/user_interaction.rb +++ b/lib/rubygems/user_interaction.rb @@ -4,357 +4,387 @@ # See LICENSE.txt for permissions. #++ -module Gem +## +# Module that defines the default UserInteraction. Any class including this +# module will have access to the +ui+ method that returns the default UI. - ## - # Module that defines the default UserInteraction. Any class including this - # module will have access to the +ui+ method that returns the default UI. +module Gem::DefaultUserInteraction - module DefaultUserInteraction + ## + # The default UI is a class variable of the singleton class for this + # module. - ## - # The default UI is a class variable of the singleton class for this - # module. + @ui = nil - @ui = nil + ## + # Return the default UI. - ## - # Return the default UI. + def self.ui + @ui ||= Gem::ConsoleUI.new + end - def self.ui - @ui ||= Gem::ConsoleUI.new - end + ## + # Set the default UI. If the default UI is never explicitly set, a simple + # console based UserInteraction will be used automatically. - ## - # Set the default UI. If the default UI is never explicitly set, a simple - # console based UserInteraction will be used automatically. + def self.ui=(new_ui) + @ui = new_ui + end - def self.ui=(new_ui) - @ui = new_ui - end + ## + # 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 - ## - # 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 + ## + # See DefaultUserInteraction::ui - ## - # See DefaultUserInteraction::ui + def ui + Gem::DefaultUserInteraction.ui + end - def ui - DefaultUserInteraction.ui - end + ## + # See DefaultUserInteraction::ui= - ## - # See DefaultUserInteraction::ui= + def ui=(new_ui) + Gem::DefaultUserInteraction.ui = new_ui + end - def ui=(new_ui) - DefaultUserInteraction.ui = new_ui - end + ## + # See DefaultUserInteraction::use_ui - ## - # See DefaultUserInteraction::use_ui + def use_ui(new_ui, &block) + Gem::DefaultUserInteraction.use_ui(new_ui, &block) + end - def use_ui(new_ui, &block) - DefaultUserInteraction.use_ui(new_ui, &block) - end +end - end +## +# 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 ## - # 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 UserInteraction - - include DefaultUserInteraction - - [:alert, - :alert_error, - :alert_warning, - :ask, - :ask_yes_no, - :choose_from_list, - :say, - :terminate_interaction ].each do |methname| - class_eval %{ - def #{methname}(*args) - ui.#{methname}(*args) - end - }, __FILE__, __LINE__ - end - end + # :method: alert ## - # StreamUI implements a simple stream based user interface. + # :method: alert_error - class StreamUI + ## + # :method: alert_warning - attr_reader :ins, :outs, :errs + ## + # :method: ask - def initialize(in_stream, out_stream, err_stream=STDERR) - @ins = in_stream - @outs = out_stream - @errs = err_stream - end + ## + # :method: ask_yes_no - ## - # 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]. + ## + # :method: choose_from_list - def choose_from_list(question, list) - @outs.puts question + ## + # :method: say - list.each_with_index do |item, index| - @outs.puts " #{index+1}. #{item}" + ## + # :method: terminate_interaction + + [:alert, + :alert_error, + :alert_warning, + :ask, + :ask_yes_no, + :choose_from_list, + :say, + :terminate_interaction ].each do |methname| + class_eval %{ + def #{methname}(*args) + ui.#{methname}(*args) end + }, __FILE__, __LINE__ + end +end + +## +# Gem::StreamUI implements a simple stream based user interface. + +class Gem::StreamUI + + attr_reader :ins, :outs, :errs - @outs.print "> " - @outs.flush + def initialize(in_stream, out_stream, err_stream=STDERR) + @ins = in_stream + @outs = out_stream + @errs = err_stream + end - result = @ins.gets + ## + # 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]. - return nil, nil unless result + def choose_from_list(question, list) + @outs.puts question - result = result.strip.to_i - 1 - return list[result], result + list.each_with_index do |item, index| + @outs.puts " #{index+1}. #{item}" 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 - end - end + @outs.print "> " + @outs.flush - 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 + 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 end + end - return result + 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 end - ## - # Ask a question. Returns an answer if connected to a tty, nil otherwise. + return result + end - def ask(question) - return nil if not @ins.tty? + ## + # Ask a question. Returns an answer if connected to a tty, nil otherwise. - @outs.print(question + " ") - @outs.flush + def ask(question) + return nil if not @ins.tty? - result = @ins.gets - result.chomp! if result - result - end + @outs.print(question + " ") + @outs.flush - ## - # Display a statement. + result = @ins.gets + result.chomp! if result + result + end - def say(statement="") - @outs.puts statement - end + ## + # Display a statement. - ## - # Display an informational alert. Will ask +question+ if it is not nil. + def say(statement="") + @outs.puts statement + end - def alert(statement, question=nil) - @outs.puts "INFO: #{statement}" - ask(question) if question - end + ## + # Display an informational alert. Will ask +question+ if it is not nil. - ## - # Display a warning in a location expected to get error messages. Will - # ask +question+ if it is not nil. + def alert(statement, question=nil) + @outs.puts "INFO: #{statement}" + ask(question) if question + end - def alert_warning(statement, question=nil) - @errs.puts "WARNING: #{statement}" - ask(question) if question - end + ## + # Display a warning in a location expected to get error messages. Will + # ask +question+ if it is not nil. - ## - # Display an error message in a location expected to get error messages. - # Will ask +question+ if it is not nil. + def alert_warning(statement, question=nil) + @errs.puts "WARNING: #{statement}" + ask(question) if question + end - def alert_error(statement, question=nil) - @errs.puts "ERROR: #{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. - ## - # Terminate the application with exit code +status+, running any exit - # handlers that might have been defined. + def alert_error(statement, question=nil) + @errs.puts "ERROR: #{statement}" + ask(question) if question + end - def terminate_interaction(status = 0) - raise Gem::SystemExitException, status - end + ## + # Display a debug message on the same location as error messages. - ## - # 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 + def debug(statement) + @errs.puts statement + end - ## - # An absolutely silent progress reporter. + ## + # Terminate the application with exit code +status+, running any exit + # handlers that might have been defined. - class SilentProgressReporter - attr_reader :count + def terminate_interaction(status = 0) + raise Gem::SystemExitException, status + end - def initialize(out_stream, size, initial_message, terminal_message = nil) - end + ## + # 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 - def updated(message) - end + ## + # An absolutely silent progress reporter. - def done - end + class SilentProgressReporter + attr_reader :count + + def initialize(out_stream, size, initial_message, terminal_message = nil) end - ## - # A basic dotted progress reporter. + def updated(message) + end - class SimpleProgressReporter - include DefaultUserInteraction + def done + end + end - attr_reader :count + ## + # A basic dotted progress reporter. - def initialize(out_stream, size, initial_message, - terminal_message = "complete") - @out = out_stream - @total = size - @count = 0 - @terminal_message = terminal_message + class SimpleProgressReporter - @out.puts initial_message - end + include Gem::DefaultUserInteraction - ## - # Prints out a dot and ignores +message+. + attr_reader :count - def updated(message) - @count += 1 - @out.print "." - @out.flush - end + def initialize(out_stream, size, initial_message, + terminal_message = "complete") + @out = out_stream + @total = size + @count = 0 + @terminal_message = terminal_message - ## - # Prints out the terminal message. + @out.puts initial_message + end - def done - @out.puts "\n#{@terminal_message}" - end + ## + # Prints out a dot and ignores +message+. + def updated(message) + @count += 1 + @out.print "." + @out.flush end ## - # A progress reporter that prints out messages about the current progress. + # Prints out the terminal message. - class VerboseProgressReporter - include DefaultUserInteraction + def done + @out.puts "\n#{@terminal_message}" + end - attr_reader :count + end - def initialize(out_stream, size, initial_message, - terminal_message = 'complete') - @out = out_stream - @total = size - @count = 0 - @terminal_message = terminal_message + ## + # A progress reporter that prints out messages about the current progress. - @out.puts initial_message - end + class VerboseProgressReporter - ## - # Prints out the position relative to the total and the +message+. + include Gem::DefaultUserInteraction - def updated(message) - @count += 1 - @out.puts "#{@count}/#{@total}: #{message}" - end + attr_reader :count - ## - # Prints out the terminal message. + def initialize(out_stream, size, initial_message, + terminal_message = 'complete') + @out = out_stream + @total = size + @count = 0 + @terminal_message = terminal_message - def done - @out.puts @terminal_message - end + @out.puts initial_message end - end - ## - # Subclass of StreamUI that instantiates the user interaction using STDIN, - # STDOUT, and STDERR. + ## + # Prints out the position relative to the total and the +message+. - class ConsoleUI < StreamUI - def initialize - super(STDIN, STDOUT, STDERR) + def updated(message) + @count += 1 + @out.puts "#{@count}/#{@total}: #{message}" end - end - ## - # SilentUI is a UI choice that is absolutely silent. + ## + # Prints out the terminal message. - class SilentUI - def method_missing(sym, *args, &block) - self + def done + @out.puts @terminal_message end end end +## +# 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 + -- cgit v1.2.3