class Vagrant::UI::Colored

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.

Constants

COLORS

Terminal colors

Public Instance Methods

format_message(type, message, **opts) click to toggle source

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

Calls superclass method Vagrant::UI::Basic#format_message
# File lib/vagrant/ui.rb, line 319
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
  "\0033[#{colorseq}m#{message}\0033[0m"
end