diff options
Diffstat (limited to 'lib/rubygems/user_interaction.rb')
-rw-r--r-- | lib/rubygems/user_interaction.rb | 71 |
1 files changed, 28 insertions, 43 deletions
diff --git a/lib/rubygems/user_interaction.rb b/lib/rubygems/user_interaction.rb index fad29b4afd..f5e460f994 100644 --- a/lib/rubygems/user_interaction.rb +++ b/lib/rubygems/user_interaction.rb @@ -4,6 +4,11 @@ # See LICENSE.txt for permissions. #++ +begin + require 'io/console' +rescue LoadError +end + ## # Module that defines the default UserInteraction. Any class including this # module will have access to the +ui+ method that returns the default UI. @@ -283,41 +288,27 @@ class Gem::StreamUI result end - if RUBY_VERSION > '1.9.2' then - ## - # Ask for a password. Does not echo response to terminal. - - def ask_for_password(question) - return nil if not tty? - - require 'io/console' - - @outs.print(question + " ") - @outs.flush + ## + # Ask for a password. Does not echo response to terminal. - 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 tty? - def ask_for_password(question) - return nil if not tty? + @outs.print(question, " ") + @outs.flush - @outs.print(question + " ") - @outs.flush + password = _gets_noecho + @outs.puts + password.chomp! if password + password + end - Gem.win_platform? ? ask_for_password_on_windows : ask_for_password_on_unix + if IO.method_defined?(:noecho) then + def _gets_noecho + @ins.noecho {@ins.gets} end - - ## - # Asks for a password that works on windows. Ripped from the Heroku gem. - - def ask_for_password_on_windows - return nil if not tty? - + elsif Gem.win_platform? + def _gets_noecho require "Win32API" char = nil password = '' @@ -330,22 +321,16 @@ class Gem::StreamUI password << char.chr end end - - puts password end - - ## - # Asks for a password that works on unix - - def ask_for_password_on_unix - return nil if not tty? - + else + def _gets_noecho system "stty -echo" - password = @ins.gets - password.chomp! if password - system "stty echo" - password + begin + @ins.gets + ensure + system "stty echo" + end end end |