aboutsummaryrefslogtreecommitdiffstats
path: root/lib/rubygems/user_interaction.rb
diff options
context:
space:
mode:
authorryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-01-19 00:08:49 +0000
committerryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-01-19 00:08:49 +0000
commite798ccbacf489a3af2201ae30058ff0ae7f79045 (patch)
tree4147a7834eb88323057fd2120a2ddc96c1eb32ab /lib/rubygems/user_interaction.rb
parentd26fb035cae8d351dc238376722c980230dc5fbd (diff)
downloadruby-e798ccbacf489a3af2201ae30058ff0ae7f79045.tar.gz
Import rubygems 1.5.0 (release candidate)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30599 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rubygems/user_interaction.rb')
-rw-r--r--lib/rubygems/user_interaction.rb142
1 files changed, 132 insertions, 10 deletions
diff --git a/lib/rubygems/user_interaction.rb b/lib/rubygems/user_interaction.rb
index 5db2dfd21c..06b847cf41 100644
--- a/lib/rubygems/user_interaction.rb
+++ b/lib/rubygems/user_interaction.rb
@@ -1,3 +1,9 @@
+######################################################################
+# This file is imported from the rubygems project.
+# DO NOT make modifications in this repo. They _will_ be reverted!
+# File a patch instead and assign it to Ryan Davis or Eric Hodel.
+######################################################################
+
#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
@@ -218,20 +224,66 @@ class Gem::StreamUI
result
end
- ##
- # Ask for a password. Does not echo response to terminal.
+ if RUBY_VERSION >= "1.9" then
+ ##
+ # Ask for a password. Does not echo response to terminal.
- def ask_for_password(question)
- return nil if not @ins.tty?
+ def ask_for_password(question)
+ return nil if not @ins.tty?
- require 'io/console'
+ require 'io/console'
- @outs.print(question + " ")
- @outs.flush
+ @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)
+ return nil if not @ins.tty?
+
+ @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
+ 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
- password = @ins.noecho {@ins.gets}
- password.chomp! if password
- password
+ ##
+ # Asks for a password that works on unix
+
+ def ask_for_password_on_unix
+ system "stty -echo"
+ password = @ins.gets
+ password.chomp! if password
+ system "stty echo"
+ password
+ end
end
##
@@ -384,6 +436,76 @@ class Gem::StreamUI
end
end
+ ##
+ # Return a download reporter object chosen from the current verbosity
+
+ def download_reporter(*args)
+ 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)
+ @file_name, @total_bytes = file_name, total_bytes
+ update_display(false)
+ end
+
+ def update(bytes)
+ new_progress = ((bytes.to_f * 100) / total_bytes.to_f).ceil
+ return if new_progress == @progress
+
+ @progress = new_progress
+ update_display
+ end
+
+ def done
+ @progress = 100
+ update_display(true, true)
+ end
+
+ private
+
+ def update_display(show_progress = true, new_line = false)
+ return unless @out.tty?
+ if show_progress
+ @out.print "\rFetching: %s (%3d%%)" % [@file_name, @progress]
+ else
+ @out.print "Fetching: %s" % @file_name
+ end
+ @out.puts if new_line
+ end
+ end
end
##