Class: Gem::StreamUI
Overview
Gem::StreamUI implements a simple stream based user interface.
Defined Under Namespace
Classes: SilentDownloadReporter, SilentProgressReporter, SimpleProgressReporter, VerboseDownloadReporter, VerboseProgressReporter
Instance Attribute Summary (collapse)
-
- (Object) errs
readonly
Returns the value of attribute errs.
-
- (Object) ins
readonly
Returns the value of attribute ins.
-
- (Object) outs
readonly
Returns the value of attribute outs.
Instance Method Summary (collapse)
-
- (Object) alert(statement, question = nil)
Display an informational alert.
-
- (Object) alert_error(statement, question = nil)
Display an error message in a location expected to get error messages.
-
- (Object) alert_warning(statement, question = nil)
Display a warning in a location expected to get error messages.
-
- (Object) ask(question)
Ask a question.
-
- (Object) ask_for_password(question)
Ask for a password.
-
- (Object) ask_for_password_on_unix
Asks for a password that works on unix.
-
- (Object) ask_for_password_on_windows
Asks for a password that works on windows.
-
- (Object) ask_yes_no(question, default = nil)
Ask a question.
-
- (Object) choose_from_list(question, list)
Choose from a list of options.
-
- (Object) debug(statement)
Display a debug message on the same location as error messages.
-
- (Object) download_reporter(*args)
Return a download reporter object chosen from the current verbosity.
-
- (StreamUI) initialize(in_stream, out_stream, err_stream = STDERR)
constructor
A new instance of StreamUI.
-
- (Object) progress_reporter(*args)
Return a progress reporter object chosen from the current verbosity.
-
- (Object) say(statement = "")
Display a statement.
-
- (Object) terminate_interaction(status = 0)
Terminate the application with exit code status, running any exit handlers that might have been defined.
Constructor Details
- (StreamUI) initialize(in_stream, out_stream, err_stream = STDERR)
A new instance of StreamUI
135 136 137 138 139 |
# File 'lib/rubygems/user_interaction.rb', line 135 def initialize(in_stream, out_stream, err_stream=STDERR) @ins = in_stream @outs = out_stream @errs = err_stream end |
Instance Attribute Details
- (Object) errs (readonly)
Returns the value of attribute errs
133 134 135 |
# File 'lib/rubygems/user_interaction.rb', line 133 def errs @errs end |
- (Object) ins (readonly)
Returns the value of attribute ins
133 134 135 |
# File 'lib/rubygems/user_interaction.rb', line 133 def ins @ins end |
- (Object) outs (readonly)
Returns the value of attribute outs
133 134 135 |
# File 'lib/rubygems/user_interaction.rb', line 133 def outs @outs end |
Instance Method Details
- (Object) alert(statement, question = nil)
Display an informational alert. Will ask question if it is not nil.
275 276 277 278 |
# File 'lib/rubygems/user_interaction.rb', line 275 def alert(statement, question=nil) @outs.puts "INFO: #{statement}" ask(question) if question end |
- (Object) alert_error(statement, question = nil)
Display an error message in a location expected to get error messages. Will ask question if it is not nil.
293 294 295 296 |
# File 'lib/rubygems/user_interaction.rb', line 293 def alert_error(statement, question=nil) @errs.puts "ERROR: #{statement}" ask(question) if question end |
- (Object) alert_warning(statement, question = nil)
Display a warning in a location expected to get error messages. Will ask question if it is not nil.
284 285 286 287 |
# File 'lib/rubygems/user_interaction.rb', line 284 def alert_warning(statement, question=nil) @errs.puts "WARNING: #{statement}" ask(question) if question end |
- (Object) ask(question)
Ask a question. Returns an answer if connected to a tty, nil otherwise.
210 211 212 213 214 215 216 217 218 219 |
# File 'lib/rubygems/user_interaction.rb', line 210 def ask(question) return nil if not @ins.tty? @outs.print(question + " ") @outs.flush result = @ins.gets result.chomp! if result result end |
- (Object) ask_for_password(question)
Ask for a password. Does not echo response to terminal.
224 225 226 227 228 229 230 231 |
# File 'lib/rubygems/user_interaction.rb', line 224 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 |
- (Object) ask_for_password_on_unix
Asks for a password that works on unix
257 258 259 260 261 262 263 |
# File 'lib/rubygems/user_interaction.rb', line 257 def ask_for_password_on_unix system "stty -echo" password = @ins.gets password.chomp! if password system "stty echo" password end |
- (Object) ask_for_password_on_windows
Asks for a password that works on windows. Ripped from the Heroku gem.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/rubygems/user_interaction.rb', line 236 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 |
- (Object) ask_yes_no(question, default = nil)
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.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/rubygems/user_interaction.rb', line 169 def ask_yes_no(question, default=nil) unless @ins.tty? then if default.nil? then raise Gem::OperationNotSupportedError, "Not connected to a tty and no default specified" else return default end end qstr = case default when nil 'yn' when true 'Yn' else 'yN' end result = nil while result.nil? result = ask("#{question} [#{qstr}]") result = case result when /^[Yy].*/ true when /^[Nn].*/ false when /^$/ default else nil end end return result end |
- (Object) choose_from_list(question, list)
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].
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/rubygems/user_interaction.rb', line 146 def choose_from_list(question, list) @outs.puts question list.each_with_index do |item, index| @outs.puts " #{index+1}. #{item}" end @outs.print "> " @outs.flush result = @ins.gets return nil, nil unless result result = result.strip.to_i - 1 return list[result], result end |
- (Object) debug(statement)
Display a debug message on the same location as error messages.
301 302 303 |
# File 'lib/rubygems/user_interaction.rb', line 301 def debug(statement) @errs.puts statement end |
- (Object) download_reporter(*args)
Return a download reporter object chosen from the current verbosity
418 419 420 421 422 423 424 425 |
# File 'lib/rubygems/user_interaction.rb', line 418 def download_reporter(*args) case Gem.configuration.verbose when nil, false SilentDownloadReporter.new(@outs, *args) else VerboseDownloadReporter.new(@outs, *args) end end |
- (Object) progress_reporter(*args)
Return a progress reporter object chosen from the current verbosity.
316 317 318 319 320 321 322 323 324 325 |
# File 'lib/rubygems/user_interaction.rb', line 316 def progress_reporter(*args) case Gem.configuration.verbose when nil, false SilentProgressReporter.new(@outs, *args) when true SimpleProgressReporter.new(@outs, *args) else VerboseProgressReporter.new(@outs, *args) end end |
- (Object) say(statement = "")
Display a statement.
268 269 270 |
# File 'lib/rubygems/user_interaction.rb', line 268 def say(statement="") @outs.puts statement end |
- (Object) terminate_interaction(status = 0)
Terminate the application with exit code status, running any exit handlers that might have been defined.
309 310 311 |
# File 'lib/rubygems/user_interaction.rb', line 309 def terminate_interaction(status = 0) raise Gem::SystemExitException, status end |