Class: Vagrant::UI::Colored

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

Overview

This is a UI implementation that outputs color for various types of messages. This should only be used with a TTY that supports color, but is up to the user of the class to verify this is the case.

Constant Summary collapse

COLORS =

Terminal colors

{
  red:     31,
  green:   32,
  yellow:  33,
  blue:    34,
  magenta: 35,
  cyan:    36,
  white:   37,
}

Instance Attribute Summary

Attributes inherited from Interface

#opts, #stderr, #stdin, #stdout

Instance Method Summary collapse

Methods inherited from Basic

#ask, #clear_line, #initialize, #report_progress, #say

Methods included from Vagrant::Util::SafePuts

#safe_puts

Methods inherited from Interface

inherited, #initialize, #initialize_copy, #machine, #rewriting, #to_proto

Constructor Details

This class inherits a constructor from Vagrant::UI::Basic

Instance Method Details

#color?true

Returns:

  • (true)


422
423
424
# File 'lib/vagrant/ui.rb', line 422

def color?
  return true
end

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

This is called by say to format the message for output.



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/vagrant/ui.rb', line 427

def format_message(type, message, **opts)
  # Get the format of the message before adding color.
  message = super

  opts = @opts.merge(opts)

  # Special case some colors for certain message types
  opts[:color] = :red if type == :error
  opts[:color] = :green if type == :success
  opts[:color] = :yellow if type == :warn

  # If it is a detail, it is not bold. Every other message type
  # is bolded.
  bold  = !!opts[:bold]
  colorseq = "#{bold ? 1 : 0 }"
  if opts[:color] && opts[:color] != :default
    color = COLORS[opts[:color]]
    colorseq += ";#{color}"
  end

  # Color the message and make sure to reset the color at the end
  "\033[#{colorseq}m#{message}\033[0m"
end