Class: Gem::StreamUI

Inherits:
Object
  • Object
show all
Defined in:
lib/build-tool/GUI.rb

Overview

StreamUI implements a simple stream based user interface.

Direct Known Subclasses

ConsoleUI

Defined Under Namespace

Classes: SilentProgressReporter, SimpleProgressReporter, VerboseProgressReporter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(in_stream, out_stream, err_stream = STDERR) ⇒ StreamUI

Returns a new instance of StreamUI.



112
113
114
115
116
# File 'lib/build-tool/GUI.rb', line 112

def initialize(in_stream, out_stream, err_stream=STDERR)
  @ins = in_stream
  @outs = out_stream
  @errs = err_stream
end

Instance Attribute Details

#errsObject (readonly)

Returns the value of attribute errs.



110
111
112
# File 'lib/build-tool/GUI.rb', line 110

def errs
  @errs
end

#insObject (readonly)

Returns the value of attribute ins.



110
111
112
# File 'lib/build-tool/GUI.rb', line 110

def ins
  @ins
end

#outsObject (readonly)

Returns the value of attribute outs.



110
111
112
# File 'lib/build-tool/GUI.rb', line 110

def outs
  @outs
end

Instance Method Details

#alert(statement, question = nil) ⇒ Object

Display an informational alert. Will ask question if it is not nil.



208
209
210
211
# File 'lib/build-tool/GUI.rb', line 208

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.



226
227
228
229
# File 'lib/build-tool/GUI.rb', line 226

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.



217
218
219
220
# File 'lib/build-tool/GUI.rb', line 217

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.



187
188
189
190
191
192
193
194
195
196
# File 'lib/build-tool/GUI.rb', line 187

def ask(question)
  return nil if not @ins.tty?

  @outs.print(question + "  ")
  @outs.flush

  result = @ins.gets
  result.chomp! if result
  result
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.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/build-tool/GUI.rb', line 146

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

#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].



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/build-tool/GUI.rb', line 123

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

#progress_reporter(*args) ⇒ Object

Return a progress reporter object chosen from the current verbosity.



242
243
244
245
246
247
248
249
250
251
# File 'lib/build-tool/GUI.rb', line 242

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

#say(statement = "") ⇒ Object

Display a statement.



201
202
203
# File 'lib/build-tool/GUI.rb', line 201

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.

Raises:

  • (Gem::SystemExitException)


235
236
237
# File 'lib/build-tool/GUI.rb', line 235

def terminate_interaction(status = 0)
  raise Gem::SystemExitException, status
end