Class: Vli::UI::Colored

Inherits:
Basic show all
Defined in:
lib/vli/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

{
  :clear  => "\e[0m",
  :red    => "\e[31m",
  :green  => "\e[32m",
  :yellow => "\e[33m"
}
COLOR_MAP =

Mapping between type of message and the color to output

{
  :warn    => COLORS[:yellow],
  :error   => COLORS[:red],
  :success => COLORS[:green]
}

Instance Attribute Summary

Attributes inherited from Interface

#resource

Instance Method Summary collapse

Methods inherited from Basic

#ask, #clear_line, #report_progress, #say

Methods included from Vli::Util::SafePuts

#safe_puts

Methods inherited from Interface

#initialize

Constructor Details

This class inherits a constructor from Vli::UI::Interface

Instance Method Details

#format_message(type, message, opts = nil) ⇒ Object

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



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/vli/ui.rb', line 144

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

  # Colorize the message if there is a color for this type of message,
  # either specified by the options or via the default color map.
  if opts.has_key?(:color)
    color   = COLORS[opts[:color]]
    message = "#{color}#{message}#{COLORS[:clear]}"
  else
    message = "#{COLOR_MAP[type]}#{message}#{COLORS[:clear]}" if COLOR_MAP[type]
  end

  message
end