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 135 135: def initialize(in_stream, out_stream, err_stream=STDERR) 136: @ins = in_stream 137: @outs = out_stream 138: @errs = err_stream 139: 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 @ins.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 225 225: def ask_for_password(question) 226: return nil if not @ins.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
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 @ins.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
Asks for a password that works on unix
# File lib/rubygems/user_interaction.rb, line 274 274: def ask_for_password_on_unix 275: system "stty -echo" 276: password = @ins.gets 277: password.chomp! if password 278: system "stty echo" 279: password 280: 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: require "Win32API" 255: char = nil 256: password = '' 257: 258: while char = Win32API.new("crtdll", "_getch", [ ], "L").Call do 259: break if char == 10 || char == 13 # received carriage return or newline 260: if char == 127 || char == 8 # backspace and delete 261: password.slice!(-1, 1) 262: else 263: password << char.chr 264: end 265: end 266: 267: puts 268: password 269: 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 169 169: def ask_yes_no(question, default=nil) 170: unless @ins.tty? then 171: if default.nil? then 172: raise Gem::OperationNotSupportedError, 173: "Not connected to a tty and no default specified" 174: else 175: return default 176: end 177: end 178: 179: qstr = case default 180: when nil 181: 'yn' 182: when true 183: 'Yn' 184: else 185: 'yN' 186: end 187: 188: result = nil 189: 190: while result.nil? 191: result = ask("#{question} [#{qstr}]") 192: result = case result 193: when /^[Yy].*/ 194: true 195: when /^[Nn].*/ 196: false 197: when /^$/ 198: default 199: else 200: 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 146 146: def choose_from_list(question, list) 147: @outs.puts question 148: 149: list.each_with_index do |item, index| 150: @outs.puts " #{index+1}. #{item}" 151: end 152: 153: @outs.print "> " 154: @outs.flush 155: 156: result = @ins.gets 157: 158: return nil, nil unless result 159: 160: result = result.strip.to_i - 1 161: return list[result], result 162: end
Return a download reporter object chosen from the current verbosity
# File lib/rubygems/user_interaction.rb, line 436 436: def download_reporter(*args) 437: case Gem.configuration.verbose 438: when nil, false 439: SilentDownloadReporter.new(@outs, *args) 440: else 441: VerboseDownloadReporter.new(@outs, *args) 442: end 443: end
Return a progress reporter object chosen from the current verbosity.
# File lib/rubygems/user_interaction.rb, line 334 334: def progress_reporter(*args) 335: case Gem.configuration.verbose 336: when nil, false 337: SilentProgressReporter.new(@outs, *args) 338: when true 339: SimpleProgressReporter.new(@outs, *args) 340: else 341: VerboseProgressReporter.new(@outs, *args) 342: end 343: end
Display a statement.
# File lib/rubygems/user_interaction.rb, line 286 286: def say(statement="") 287: @outs.puts statement 288: end