Class: Gem::StreamUI
- Inherits:
-
Object
- Object
- Gem::StreamUI
- Defined in:
- lib/rubygems/user_interaction.rb
Overview
Gem::StreamUI implements a simple stream based user interface.
Defined Under Namespace
Classes: SilentDownloadReporter, SilentProgressReporter, SimpleProgressReporter, VerboseDownloadReporter, VerboseProgressReporter
Instance Attribute Summary collapse
-
#errs ⇒ Object
readonly
Returns the value of attribute errs.
-
#ins ⇒ Object
readonly
Returns the value of attribute ins.
-
#outs ⇒ Object
readonly
Returns the value of attribute outs.
Instance Method Summary collapse
-
#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 in a location expected to get error messages.
-
#ask(question) ⇒ Object
Ask a question.
-
#ask_for_password(question) ⇒ Object
Ask for a password.
-
#ask_for_password_on_unix ⇒ Object
Asks for a password that works on unix.
-
#ask_for_password_on_windows ⇒ Object
Asks for a password that works on windows.
-
#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.
-
#debug(statement) ⇒ Object
Display a debug message on the same location as error messages.
-
#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
A new instance of StreamUI.
-
#progress_reporter(*args) ⇒ Object
Return a progress reporter object chosen from the current verbosity.
-
#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
Constructor Details
#initialize(in_stream, out_stream, err_stream = STDERR, usetty = true) ⇒ StreamUI
Returns a new instance of StreamUI.
131 132 133 134 135 136 |
# File 'lib/rubygems/user_interaction.rb', line 131 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)
Returns the value of attribute errs.
129 130 131 |
# File 'lib/rubygems/user_interaction.rb', line 129 def errs @errs end |
#ins ⇒ Object (readonly)
Returns the value of attribute ins.
129 130 131 |
# File 'lib/rubygems/user_interaction.rb', line 129 def ins @ins end |
#outs ⇒ Object (readonly)
Returns the value of attribute outs.
129 130 131 |
# File 'lib/rubygems/user_interaction.rb', line 129 def outs @outs end |
Instance Method Details
#alert(statement, question = nil) ⇒ Object
Display an informational alert. Will ask question
if it is not nil.
307 308 309 310 |
# File 'lib/rubygems/user_interaction.rb', line 307 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.
325 326 327 328 |
# File 'lib/rubygems/user_interaction.rb', line 325 def alert_error(statement, question=nil) @errs.puts "ERROR: #{statement}" ask(question) if question end |
#alert_warning(statement, question = nil) ⇒ Object
Display a warning in a location expected to get error messages. Will ask question
if it is not nil.
316 317 318 319 |
# File 'lib/rubygems/user_interaction.rb', line 316 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.
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/rubygems/user_interaction.rb', line 220 def ask(question) return nil if not tty? @outs.print(question + " ") @outs.flush result = @ins.gets result.chomp! if result result end |
#ask_for_password(question) ⇒ Object
Ask for a password. Does not echo response to terminal.
235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/rubygems/user_interaction.rb', line 235 def ask_for_password(question) return nil if not tty? require 'io/console' @outs.print(question + " ") @outs.flush password = @ins.noecho {@ins.gets} password.chomp! if password password end |
#ask_for_password_on_unix ⇒ Object
Asks for a password that works on unix
286 287 288 289 290 291 292 293 294 |
# File 'lib/rubygems/user_interaction.rb', line 286 def ask_for_password_on_unix return nil if not tty? system "stty -echo" password = @ins.gets password.chomp! if password system "stty echo" password end |
#ask_for_password_on_windows ⇒ Object
Asks for a password that works on windows. Ripped from the Heroku gem.
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/rubygems/user_interaction.rb', line 263 def ask_for_password_on_windows return nil if not tty? 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 |
#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.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/rubygems/user_interaction.rb', line 184 def ask_yes_no(question, default=nil) unless tty? then if default.nil? then 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 else nil end end return result end |
#backtrace(exception) ⇒ Object
Prints a formatted backtrace to the errors stream if backtraces are enabled.
150 151 152 153 154 |
# File 'lib/rubygems/user_interaction.rb', line 150 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].
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/rubygems/user_interaction.rb', line 161 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 |
#debug(statement) ⇒ Object
Display a debug message on the same location as error messages.
333 334 335 |
# File 'lib/rubygems/user_interaction.rb', line 333 def debug(statement) @errs.puts statement end |
#download_reporter(*args) ⇒ Object
Return a download reporter object chosen from the current verbosity
454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/rubygems/user_interaction.rb', line 454 def download_reporter(*args) if self.kind_of?(Gem::SilentUI) return SilentDownloadReporter.new(@outs, *args) end case Gem.configuration.verbose when nil, false SilentDownloadReporter.new(@outs, *args) else VerboseDownloadReporter.new(@outs, *args) end end |
#progress_reporter(*args) ⇒ Object
Return a progress reporter object chosen from the current verbosity.
348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/rubygems/user_interaction.rb', line 348 def progress_reporter(*args) if self.kind_of?(Gem::SilentUI) return SilentProgressReporter.new(@outs, *args) end case Gem.configuration.verbose when nil, false SilentProgressReporter.new(@outs, *args) when true SimpleProgressReporter.new(@outs, *args) else VerboseProgressReporter.new(@outs, *args) end end |
#say(statement = "") ⇒ Object
Display a statement.
300 301 302 |
# File 'lib/rubygems/user_interaction.rb', line 300 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.
341 342 343 |
# File 'lib/rubygems/user_interaction.rb', line 341 def terminate_interaction(status = 0) raise Gem::SystemExitException, status end |
#tty? ⇒ Boolean
138 139 140 141 142 143 144 |
# File 'lib/rubygems/user_interaction.rb', line 138 def tty? if RUBY_VERSION < '1.9.3' and RUBY_PLATFORM =~ /mingw|mswin/ then @usetty else @usetty && @ins.tty? end end |