Class | Gem::StreamUI |
In: |
lib/rubygems/user_interaction.rb
|
Parent: | Object |
Gem::StreamUI implements a simple stream based user interface.
errs | [R] | |
ins | [R] | |
outs | [R] |
# File lib/rubygems/user_interaction.rb, line 131 131: def initialize(in_stream, out_stream, err_stream=STDERR, usetty=true) 132: @ins = in_stream 133: @outs = out_stream 134: @errs = err_stream 135: @usetty = usetty 136: end
Ask a question. Returns an answer if connected to a tty, nil otherwise.
# File lib/rubygems/user_interaction.rb, line 210 210: def ask(question) 211: return nil if not tty? 212: 213: @outs.print(question + " ") 214: @outs.flush 215: 216: result = @ins.gets 217: result.chomp! if result 218: result 219: end
Ask for a password. Does not echo response to terminal.
# File lib/rubygems/user_interaction.rb, line 241 241: def ask_for_password(question) 242: return nil if not tty? 243: 244: @outs.print(question + " ") 245: @outs.flush 246: 247: Gem.win_platform? ? ask_for_password_on_windows : ask_for_password_on_unix 248: end
Ask for a password. Does not echo response to terminal.
# File lib/rubygems/user_interaction.rb, line 225 225: def ask_for_password(question) 226: return nil if not tty? 227: 228: require 'io/console' 229: 230: @outs.print(question + " ") 231: @outs.flush 232: 233: password = @ins.noecho {@ins.gets} 234: password.chomp! if password 235: password 236: end
Asks for a password that works on unix
# File lib/rubygems/user_interaction.rb, line 276 276: def ask_for_password_on_unix 277: return nil if not tty? 278: 279: system "stty -echo" 280: password = @ins.gets 281: password.chomp! if password 282: system "stty echo" 283: password 284: end
Asks for a password that works on windows. Ripped from the Heroku gem.
# File lib/rubygems/user_interaction.rb, line 253 253: def ask_for_password_on_windows 254: return nil if not tty? 255: 256: require "Win32API" 257: char = nil 258: password = '' 259: 260: while char = Win32API.new("crtdll", "_getch", [ ], "L").Call do 261: break if char == 10 || char == 13 # received carriage return or newline 262: if char == 127 || char == 8 # backspace and delete 263: password.slice!(-1, 1) 264: else 265: password << char.chr 266: end 267: end 268: 269: puts 270: password 271: 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.
# File lib/rubygems/user_interaction.rb, line 174 174: def ask_yes_no(question, default=nil) 175: unless tty? then 176: if default.nil? then 177: raise Gem::OperationNotSupportedError, 178: "Not connected to a tty and no default specified" 179: else 180: return default 181: end 182: end 183: 184: default_answer = case default 185: when nil 186: 'yn' 187: when true 188: 'Yn' 189: else 190: 'yN' 191: end 192: 193: result = nil 194: 195: while result.nil? do 196: result = case ask "#{question} [#{default_answer}]" 197: when /^y/i then true 198: when /^n/i then false 199: when /^$/ then default 200: else nil 201: end 202: end 203: 204: return result 205: end
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].
# File lib/rubygems/user_interaction.rb, line 151 151: def choose_from_list(question, list) 152: @outs.puts question 153: 154: list.each_with_index do |item, index| 155: @outs.puts " #{index+1}. #{item}" 156: end 157: 158: @outs.print "> " 159: @outs.flush 160: 161: result = @ins.gets 162: 163: return nil, nil unless result 164: 165: result = result.strip.to_i - 1 166: return list[result], result 167: end
Return a download reporter object chosen from the current verbosity
# File lib/rubygems/user_interaction.rb, line 444 444: def download_reporter(*args) 445: if self.kind_of?(Gem::SilentUI) 446: return SilentDownloadReporter.new(@outs, *args) 447: end 448: 449: case Gem.configuration.verbose 450: when nil, false 451: SilentDownloadReporter.new(@outs, *args) 452: else 453: VerboseDownloadReporter.new(@outs, *args) 454: end 455: end
Return a progress reporter object chosen from the current verbosity.
# File lib/rubygems/user_interaction.rb, line 338 338: def progress_reporter(*args) 339: if self.kind_of?(Gem::SilentUI) 340: return SilentProgressReporter.new(@outs, *args) 341: end 342: 343: case Gem.configuration.verbose 344: when nil, false 345: SilentProgressReporter.new(@outs, *args) 346: when true 347: SimpleProgressReporter.new(@outs, *args) 348: else 349: VerboseProgressReporter.new(@outs, *args) 350: end 351: end
Display a statement.
# File lib/rubygems/user_interaction.rb, line 290 290: def say(statement="") 291: @outs.puts statement 292: end