Class: Gem::StreamUI
- Inherits:
-
Object
- Object
- Gem::StreamUI
- Extended by:
- Deprecate
- Defined in:
- lib/rubygems/user_interaction.rb
Overview
Gem::StreamUI implements a simple stream based user interface.
Defined Under Namespace
Classes: SilentDownloadReporter, SilentProgressReporter, SimpleProgressReporter, ThreadedDownloadReporter, VerboseProgressReporter
Instance Attribute Summary collapse
-
#errs ⇒ Object
readonly
The error stream.
-
#ins ⇒ Object
readonly
The input stream.
-
#outs ⇒ Object
readonly
The output stream.
Instance Method Summary collapse
- #_gets_noecho ⇒ Object
-
#alert(statement, question = nil) ⇒ Object
Display an informational alert.
-
#alert_error(statement, question = nil) ⇒ Object
Display an error message in a location expected to get error messages.
-
#alert_warning(statement, question = nil) ⇒ Object
Display a warning on stderr.
-
#ask(question) ⇒ Object
Ask a question.
-
#ask_for_password(question) ⇒ Object
Ask for a password.
-
#ask_yes_no(question, default = nil) ⇒ Object
Ask a question.
-
#backtrace(exception) ⇒ Object
Prints a formatted backtrace to the errors stream if backtraces are enabled.
-
#choose_from_list(question, list) ⇒ Object
Choose from a list of options.
- #close ⇒ Object
-
#download_reporter(*args) ⇒ Object
Return a download reporter object chosen from the current verbosity.
-
#initialize(in_stream, out_stream, err_stream = $stderr, usetty = true) ⇒ StreamUI
constructor
Creates a new StreamUI wrapping
in_stream
for user input,out_stream
for standard output,err_stream
for error output. -
#progress_reporter(*args) ⇒ Object
Return a progress reporter object chosen from the current verbosity.
- #require_io_console ⇒ Object
-
#say(statement = "") ⇒ Object
Display a statement.
-
#terminate_interaction(status = 0) ⇒ Object
Terminate the application with exit code
status
, running any exit handlers that might have been defined. -
#tty? ⇒ Boolean
Returns true if TTY methods should be used on this StreamUI.
Methods included from Deprecate
deprecate, next_rubygems_major_version, rubygems_deprecate, rubygems_deprecate_command, skip, skip=, skip_during
Constructor Details
#initialize(in_stream, out_stream, err_stream = $stderr, usetty = true) ⇒ StreamUI
Creates a new StreamUI wrapping in_stream
for user input, out_stream
for standard output, err_stream
for error output. If usetty
is true then special operations (like asking for passwords) will use the TTY commands to disable character echo.
196 197 198 199 200 201 |
# File 'lib/rubygems/user_interaction.rb', line 196 def initialize(in_stream, out_stream, err_stream=$stderr, usetty=true) @ins = in_stream @outs = out_stream @errs = err_stream @usetty = usetty end |
Instance Attribute Details
#errs ⇒ Object (readonly)
The error stream
188 189 190 |
# File 'lib/rubygems/user_interaction.rb', line 188 def errs @errs end |
#ins ⇒ Object (readonly)
The input stream
178 179 180 |
# File 'lib/rubygems/user_interaction.rb', line 178 def ins @ins end |
#outs ⇒ Object (readonly)
The output stream
183 184 185 |
# File 'lib/rubygems/user_interaction.rb', line 183 def outs @outs end |
Instance Method Details
#_gets_noecho ⇒ Object
320 321 322 323 |
# File 'lib/rubygems/user_interaction.rb', line 320 def _gets_noecho require_io_console @ins.noecho { @ins.gets } end |
#alert(statement, question = nil) ⇒ Object
Display an informational alert. Will ask question
if it is not nil.
335 336 337 338 |
# File 'lib/rubygems/user_interaction.rb', line 335 def alert(statement, question=nil) @outs.puts "INFO: #{statement}" ask(question) if question end |
#alert_error(statement, question = nil) ⇒ Object
Display an error message in a location expected to get error messages. Will ask question
if it is not nil.
352 353 354 355 |
# File 'lib/rubygems/user_interaction.rb', line 352 def alert_error(statement, question=nil) @errs.puts "ERROR: #{statement}" ask(question) if question end |
#alert_warning(statement, question = nil) ⇒ Object
Display a warning on stderr. Will ask question
if it is not nil.
343 344 345 346 |
# File 'lib/rubygems/user_interaction.rb', line 343 def alert_warning(statement, question=nil) @errs.puts "WARNING: #{statement}" ask(question) if question end |
#ask(question) ⇒ Object
Ask a question. Returns an answer if connected to a tty, nil otherwise.
284 285 286 287 288 289 290 291 292 293 |
# File 'lib/rubygems/user_interaction.rb', line 284 def ask(question) return nil unless tty? @outs.print(question + " ") @outs.flush result = @ins.gets result&.chomp! result end |
#ask_for_password(question) ⇒ Object
Ask for a password. Does not echo response to terminal.
298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/rubygems/user_interaction.rb', line 298 def ask_for_password(question) return nil unless tty? @outs.print(question, " ") @outs.flush password = _gets_noecho @outs.puts password&.chomp! password end |
#ask_yes_no(question, default = nil) ⇒ Object
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.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/rubygems/user_interaction.rb', line 249 def ask_yes_no(question, default=nil) unless tty? if default.nil? raise Gem::OperationNotSupportedError, "Not connected to a tty and no default specified" else return default end end default_answer = case default when nil "yn" when true "Yn" else "yN" end result = nil while result.nil? do result = case ask "#{question} [#{default_answer}]" when /^y/i then true when /^n/i then false when /^$/ then default end end result end |
#backtrace(exception) ⇒ Object
Prints a formatted backtrace to the errors stream if backtraces are enabled.
214 215 216 217 218 |
# File 'lib/rubygems/user_interaction.rb', line 214 def backtrace(exception) return unless Gem.configuration.backtrace @errs.puts "\t#{exception.backtrace.join "\n\t"}" end |
#choose_from_list(question, list) ⇒ Object
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].
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/rubygems/user_interaction.rb', line 225 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 nil, nil unless (0...list.size) === result [list[result], result] end |
#close ⇒ Object
366 367 |
# File 'lib/rubygems/user_interaction.rb', line 366 def close end |
#download_reporter(*args) ⇒ Object
Return a download reporter object chosen from the current verbosity
499 500 501 502 503 504 505 |
# File 'lib/rubygems/user_interaction.rb', line 499 def download_reporter(*args) if [nil, false].include?(Gem.configuration.verbose) || !@outs.tty? SilentDownloadReporter.new(@outs, *args) else ThreadedDownloadReporter.new(@outs, *args) end end |
#progress_reporter(*args) ⇒ Object
Return a progress reporter object chosen from the current verbosity.
372 373 374 375 376 377 378 379 380 381 |
# File 'lib/rubygems/user_interaction.rb', line 372 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 |
#require_io_console ⇒ Object
310 311 312 313 314 315 316 317 318 |
# File 'lib/rubygems/user_interaction.rb', line 310 def require_io_console @require_io_console ||= begin begin require "io/console" rescue LoadError end true end end |
#say(statement = "") ⇒ Object
Display a statement.
328 329 330 |
# File 'lib/rubygems/user_interaction.rb', line 328 def say(statement="") @outs.puts statement end |
#terminate_interaction(status = 0) ⇒ Object
Terminate the application with exit code status
, running any exit handlers that might have been defined.
361 362 363 364 |
# File 'lib/rubygems/user_interaction.rb', line 361 def terminate_interaction(status = 0) close raise Gem::SystemExitException, status end |
#tty? ⇒ Boolean
Returns true if TTY methods should be used on this StreamUI.
206 207 208 |
# File 'lib/rubygems/user_interaction.rb', line 206 def tty? @usetty && @ins.tty? end |