summaryrefslogtreecommitdiff
path: root/lib/rubygems/user_interaction.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rubygems/user_interaction.rb')
-rw-r--r--lib/rubygems/user_interaction.rb82
1 files changed, 46 insertions, 36 deletions
diff --git a/lib/rubygems/user_interaction.rb b/lib/rubygems/user_interaction.rb
index 0ab44fbf6c..0172c4ee89 100644
--- a/lib/rubygems/user_interaction.rb
+++ b/lib/rubygems/user_interaction.rb
@@ -1,19 +1,19 @@
# frozen_string_literal: true
+
#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
# See LICENSE.txt for permissions.
#++
-require_relative 'deprecate'
-require_relative 'text'
+require_relative "deprecate"
+require_relative "text"
##
# 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
-
include Gem::Text
##
@@ -68,7 +68,6 @@ module Gem::DefaultUserInteraction
def use_ui(new_ui, &block)
Gem::DefaultUserInteraction.use_ui(new_ui, &block)
end
-
end
##
@@ -91,7 +90,6 @@ end
# end
module Gem::UserInteraction
-
include Gem::DefaultUserInteraction
##
@@ -148,7 +146,7 @@ module Gem::UserInteraction
##
# Displays the given +statement+ on the standard output (or equivalent).
- def say(statement = '')
+ def say(statement = "")
ui.say statement
end
@@ -195,7 +193,7 @@ class Gem::StreamUI
# then special operations (like asking for passwords) will use the TTY
# commands to disable character echo.
- def initialize(in_stream, out_stream, err_stream=STDERR, usetty=true)
+ def initialize(in_stream, out_stream, err_stream=$stderr, usetty=true)
@ins = in_stream
@outs = out_stream
@errs = err_stream
@@ -239,7 +237,8 @@ class Gem::StreamUI
return nil, nil unless result
result = result.strip.to_i - 1
- return list[result], result
+ return nil, nil unless (0...list.size) === result
+ [list[result], result]
end
##
@@ -259,12 +258,12 @@ class Gem::StreamUI
default_answer = case default
when nil
- 'yn'
+ "yn"
when true
- 'Yn'
+ "Yn"
else
- 'yN'
- end
+ "yN"
+ end
result = nil
@@ -273,24 +272,23 @@ class Gem::StreamUI
when /^y/i then true
when /^n/i then false
when /^$/ then default
- else nil
- end
+ end
end
- return result
+ result
end
##
# Ask a question. Returns an answer if connected to a tty, nil otherwise.
def ask(question)
- return nil if not tty?
+ return nil unless tty?
@outs.print(question + " ")
@outs.flush
result = @ins.gets
- result.chomp! if result
+ result&.chomp!
result
end
@@ -298,21 +296,21 @@ class Gem::StreamUI
# Ask for a password. Does not echo response to terminal.
def ask_for_password(question)
- return nil if not tty?
+ return nil unless tty?
@outs.print(question, " ")
@outs.flush
password = _gets_noecho
@outs.puts
- password.chomp! if password
+ password&.chomp!
password
end
def require_io_console
@require_io_console ||= begin
begin
- require 'io/console'
+ require "io/console"
rescue LoadError
end
true
@@ -428,8 +426,7 @@ class Gem::StreamUI
# +size+ items. Shows the given +initial_message+ when progress starts
# and the +terminal_message+ when it is complete.
- def initialize(out_stream, size, initial_message,
- terminal_message = "complete")
+ def initialize(out_stream, size, initial_message, terminal_message = "complete")
@out = out_stream
@total = size
@count = 0
@@ -471,8 +468,7 @@ class Gem::StreamUI
# +size+ items. Shows the given +initial_message+ when progress starts
# and the +terminal_message+ when it is complete.
- def initialize(out_stream, size, initial_message,
- terminal_message = 'complete')
+ def initialize(out_stream, size, initial_message, terminal_message = "complete")
@out = out_stream
@total = size
@count = 0
@@ -595,8 +591,8 @@ class Gem::StreamUI
end
##
-# Subclass of StreamUI that instantiates the user interaction using STDIN,
-# STDOUT, and STDERR.
+# Subclass of StreamUI that instantiates the user interaction using $stdin,
+# $stdout, and $stderr.
class Gem::ConsoleUI < Gem::StreamUI
##
@@ -604,7 +600,7 @@ class Gem::ConsoleUI < Gem::StreamUI
# stdin, output to stdout and warnings or errors to stderr.
def initialize
- super STDIN, STDOUT, STDERR, true
+ super $stdin, $stdout, $stderr, true
end
end
@@ -616,18 +612,11 @@ class Gem::SilentUI < Gem::StreamUI
# The SilentUI has no arguments as it does not use any stream.
def initialize
- reader, writer = nil, nil
-
- reader = File.open(IO::NULL, 'r')
- writer = File.open(IO::NULL, 'w')
-
- super reader, writer, writer, false
+ io = NullIO.new
+ super io, io, io, false
end
def close
- super
- @ins.close
- @outs.close
end
def download_reporter(*args) # :nodoc:
@@ -637,4 +626,25 @@ class Gem::SilentUI < Gem::StreamUI
def progress_reporter(*args) # :nodoc:
SilentProgressReporter.new(@outs, *args)
end
+
+ ##
+ # An absolutely silent IO.
+
+ class NullIO
+ def puts(*args)
+ end
+
+ def print(*args)
+ end
+
+ def flush
+ end
+
+ def gets(*args)
+ end
+
+ def tty?
+ false
+ end
+ end
end