module Kitchen::Color

Utility methods to help ouput colorized text in a terminal. The implementation is a compressed mashup of code from the Thor and Foreman projects.

@author Fletcher Nichol <fnichol@nichol.ca>

Constants

ANSI
COLORS

Public Class Methods

colorize(str, name) click to toggle source

Returns a colorized ansi escaped string with the given color.

@param str [String] a string to colorize @param name [Symbol] a valid color representation, taken from

Kitchen::Color::ANSI

@return [String] an ansi escaped string if the color is valid and an

unescaped string otherwise
# File lib/kitchen/color.rb, line 58
def self.colorize(str, name)
  color = escape(name)
  color.empty? ? str : "#{color}#{str}#{escape(:reset)}"
end
escape(name) click to toggle source

Returns an ansi escaped string representing a color control sequence.

@param name [Symbol] a valid color representation, taken from

Kitchen::Color::ANSI

@return [String] an ansi escaped string if the color is valid and an

empty string otherwise
# File lib/kitchen/color.rb, line 44
def self.escape(name)
  return "" if name.nil?
  return "" unless ANSI[name]

  "\e[#{ANSI[name]}m"
end