user_interaction.rb [plain text]
module Gem::DefaultUserInteraction
@ui = nil
def self.ui
@ui ||= Gem::ConsoleUI.new
end
def self.ui=(new_ui)
@ui = new_ui
end
def self.use_ui(new_ui)
old_ui = @ui
@ui = new_ui
yield
ensure
@ui = old_ui
end
def ui
Gem::DefaultUserInteraction.ui
end
def ui=(new_ui)
Gem::DefaultUserInteraction.ui = new_ui
end
def use_ui(new_ui, &block)
Gem::DefaultUserInteraction.use_ui(new_ui, &block)
end
end
module Gem::UserInteraction
include Gem::DefaultUserInteraction
def alert(*args)
ui.alert(*args)
end
def alert_error(*args)
ui.alert_error(*args)
end
def alert_warning(*args)
ui.alert_warning(*args)
end
def ask(*args)
ui.ask(*args)
end
def ask_for_password(*args)
ui.ask_for_password(*args)
end
def ask_yes_no(*args)
ui.ask_yes_no(*args)
end
def choose_from_list(*args)
ui.choose_from_list(*args)
end
def say(*args)
ui.say(*args)
end
def terminate_interaction(*args)
ui.terminate_interaction(*args)
end
end
class Gem::StreamUI
attr_reader :ins, :outs, :errs
def initialize(in_stream, out_stream, err_stream=STDERR, usetty=true)
@ins = in_stream
@outs = out_stream
@errs = err_stream
@usetty = usetty
end
def tty?
if RUBY_VERSION < '1.9.3' and RUBY_PLATFORM =~ /mingw|mswin/ then
@usetty
else
@usetty && @ins.tty?
end
end
def backtrace exception
return unless Gem.configuration.backtrace
@errs.puts "\t#{exception.backtrace.join "\n\t"}"
end
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
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
def ask(question)
return nil if not tty?
@outs.print(question + " ")
@outs.flush
result = @ins.gets
result.chomp! if result
result
end
if RUBY_VERSION > '1.9.2' then
def ask_for_password(question)
return nil if not tty?
require 'io/console'
@outs.print(question + " ")
@outs.flush
password = @ins.noecho {@ins.gets}
password.chomp! if password
password
end
else
def ask_for_password(question)
return nil if not tty?
@outs.print(question + " ")
@outs.flush
Gem.win_platform? ? ask_for_password_on_windows : ask_for_password_on_unix
end
def ask_for_password_on_windows
return nil if not tty?
require "Win32API"
char = nil
password = ''
while char = Win32API.new("crtdll", "_getch", [ ], "L").Call do
break if char == 10 || char == 13 if char == 127 || char == 8 password.slice!(-1, 1)
else
password << char.chr
end
end
puts
password
end
def ask_for_password_on_unix
return nil if not tty?
system "stty -echo"
password = @ins.gets
password.chomp! if password
system "stty echo"
password
end
end
def say(statement="")
@outs.puts statement
end
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
def alert_error(statement, question=nil)
@errs.puts "ERROR: #{statement}"
ask(question) if question
end
def debug(statement)
@errs.puts statement
end
def terminate_interaction(status = 0)
raise Gem::SystemExitException, status
end
def progress_reporter(*args)
if self.kind_of?(Gem::SilentUI)
return SilentProgressReporter.new(@outs, *args)
end
case Gem.configuration.verbose
when nil, false
SilentProgressReporter.new(@outs, *args)
when true
SimpleProgressReporter.new(@outs, *args)
else
VerboseProgressReporter.new(@outs, *args)
end
end
class SilentProgressReporter
attr_reader :count
def initialize(out_stream, size, initial_message, terminal_message = nil)
end
def updated(message)
end
def done
end
end
class SimpleProgressReporter
include Gem::DefaultUserInteraction
attr_reader :count
def initialize(out_stream, size, initial_message,
terminal_message = "complete")
@out = out_stream
@total = size
@count = 0
@terminal_message = terminal_message
@out.puts initial_message
end
def updated(message)
@count += 1
@out.print "."
@out.flush
end
def done
@out.puts "\n#{@terminal_message}"
end
end
class VerboseProgressReporter
include Gem::DefaultUserInteraction
attr_reader :count
def initialize(out_stream, size, initial_message,
terminal_message = 'complete')
@out = out_stream
@total = size
@count = 0
@terminal_message = terminal_message
@out.puts initial_message
end
def updated(message)
@count += 1
@out.puts "#{@count}/#{@total}: #{message}"
end
def done
@out.puts @terminal_message
end
end
def download_reporter(*args)
if self.kind_of?(Gem::SilentUI)
return SilentDownloadReporter.new(@outs, *args)
end
case Gem.configuration.verbose
when nil, false
SilentDownloadReporter.new(@outs, *args)
else
VerboseDownloadReporter.new(@outs, *args)
end
end
class SilentDownloadReporter
def initialize(out_stream, *args)
end
def fetch(filename, filesize)
end
def update(current)
end
def done
end
end
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)
@file_name = file_name
@total_bytes = total_bytes.to_i
@units = @total_bytes.zero? ? 'B' : '%'
update_display(false)
end
def update(bytes)
new_progress = if @units == 'B' then
bytes
else
((bytes.to_f * 100) / total_bytes.to_f).ceil
end
return if new_progress == @progress
@progress = new_progress
update_display
end
def done
@progress = 100 if @units == '%'
update_display(true, true)
end
private
def update_display(show_progress = true, new_line = false)
return unless @out.tty?
if show_progress then
@out.print "\rFetching: %s (%3d%s)" % [@file_name, @progress, @units]
else
@out.print "Fetching: %s" % @file_name
end
@out.puts if new_line
end
end
end
class Gem::ConsoleUI < Gem::StreamUI
def initialize
super STDIN, STDOUT, STDERR, true
end
end
class Gem::SilentUI < Gem::StreamUI
def initialize
reader, writer = nil, nil
begin
reader = File.open('/dev/null', 'r')
writer = File.open('/dev/null', 'w')
rescue Errno::ENOENT
reader = File.open('nul', 'r')
writer = File.open('nul', 'w')
end
super reader, writer, writer, false
end
def download_reporter(*args)
SilentDownloadReporter.new(@outs, *args)
end
def progress_reporter(*args)
SilentProgressReporter.new(@outs, *args)
end
end