Class: Vagrant::UI::Prefixed

Inherits:
Interface show all
Defined in:
lib/vagrant/ui.rb

Overview

Prefixed wraps an existing UI and adds a prefix to it.

Constant Summary collapse

OUTPUT_PREFIX =

The prefix for output messages.

"==> "

Instance Attribute Summary

Attributes inherited from Interface

#stderr, #stdin, #stdout

Instance Method Summary collapse

Methods inherited from Interface

#color?

Constructor Details

#initialize(ui, prefix) ⇒ Prefixed

Returns a new instance of Prefixed.


292
293
294
295
296
297
# File 'lib/vagrant/ui.rb', line 292

def initialize(ui, prefix)
  super()

  @prefix = prefix
  @ui     = ui
end

Instance Method Details

#format_message(type, message, **opts) ⇒ Object


347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/vagrant/ui.rb', line 347

def format_message(type, message, **opts)
  opts = self.opts.merge(opts)

  prefix = ""
  if !opts.key?(:prefix) || opts[:prefix]
    prefix = OUTPUT_PREFIX
    prefix = " " * OUTPUT_PREFIX.length if \
      type == :detail || type == :ask || opts[:prefix_spaces]
  end

  message = Util::CredentialScrubber.desensitize(message)

  # Fast-path if there is no prefix
  return message if prefix.empty?

  target = @prefix
  target = opts[:target] if opts.key?(:target)
  target = "#{target}:" if target != ""

  lines = [message]
  if message != ""
    lines = [].tap do |l|
      message.scan(/(.*?)(\n|$)/).each do |m|
        l << m.first if m.first != "" || (m.first == "" && m.last == "\n")
      end
    end
    lines << "" if message.end_with?("\n")
  end

  # Otherwise, make sure to prefix every line properly
  lines.map do |line|
    "#{prefix}#{target} #{line}"
  end.join("\n")
end

#initialize_copy(original) ⇒ Object


299
300
301
302
# File 'lib/vagrant/ui.rb', line 299

def initialize_copy(original)
  super
  @ui = original.instance_variable_get(:@ui).dup
end

#machine(type, *data) ⇒ Object

For machine-readable output, set the prefix in the options hash and continue it on.


332
333
334
335
336
337
338
# File 'lib/vagrant/ui.rb', line 332

def machine(type, *data)
  opts = {}
  opts = data.pop if data.last.is_a?(Hash)
  opts[:target] = @prefix
  data << opts
  @ui.machine(type, *data)
end

#optsHash

Return the parent's opts.

Returns:

  • (Hash)

343
344
345
# File 'lib/vagrant/ui.rb', line 343

def opts
  @ui.opts
end

#rewritingObject


382
383
384
385
386
# File 'lib/vagrant/ui.rb', line 382

def rewriting
  @ui.rewriting do |ui|
    yield ui
  end
end