class Pastel::Color
A class responsible for coloring strings.
Constants
- ALIASES
-
All color aliases
- ANSI_COLOR_REGEXP
-
Match all color escape sequences
Attributes
Public Class Methods
Source
# File lib/pastel/color.rb, line 24 def initialize(enabled: nil, eachline: false) @enabled = enabled @eachline = eachline @cache = {} end
Initialize a Terminal Color
@api public
Public Instance Methods
Source
# File lib/pastel/color.rb, line 235 def ==(other) other.is_a?(self.class) && enabled == other.enabled && eachline == other.eachline end
Compare colors for equivalence of attributes
@return [Boolean]
@api public
Source
# File lib/pastel/color.rb, line 206 def alias_color(alias_name, *colors) validate(*colors) if !(alias_name.to_s =~ /^[\w]+$/) raise InvalidAliasNameError, "Invalid alias name `#{alias_name}`" elsif ANSI::ATTRIBUTES[alias_name] raise InvalidAliasNameError, "Cannot alias standard color `#{alias_name}`" end ALIASES[alias_name.to_sym] = colors.map(&ANSI::ATTRIBUTES.method(:[])) colors end
Define a new colors alias
@param [String] alias_name
the colors alias to define
@param [Array] color
the colors the alias will correspond to
@return [Array]
the standard color values of the alias
@api public
Source
# File lib/pastel/color.rb, line 78 def apply_codes(string, ansi_colors) "#{ansi_colors}#{string.gsub(/(\e\[0m)([^\e]+)$/, "\\1#{ansi_colors}\\2")}\e[0m" end
Apply escape codes to the string
@param [String] string
the string to apply escapes to
@param [Strin] ansi_colors
the ansi colors to apply
@return [String]
return the string surrounded by escape codes
@api private
Source
# File lib/pastel/color.rb, line 85 def clear lookup(:clear) end
Reset sequence
@api public
Source
# File lib/pastel/color.rb, line 148 def code(*colors) attribute = [] colors.each do |color| value = ANSI::ATTRIBUTES[color] || ALIASES[color] if value attribute << value else validate(color) end end attribute end
Return raw color code without embeding it into a string.
@return [Array]
ANSI escape codes
@api public
Source
# File lib/pastel/color.rb, line 117 def colored?(string) !ANSI_COLOR_REGEXP.match(string).nil? end
Check if string has color escape codes
@param [String] string
the string to check for color strings
@return [Boolean]
true when string contains color codes, false otherwise
@api public
Source
# File lib/pastel/color.rb, line 54 def decorate(string, *colors) return string if blank?(string) || !enabled || colors.empty? ansi_colors = lookup(*colors.dup.uniq) if eachline string.dup.split(eachline).map! do |line| apply_codes(line, ansi_colors) end.join(eachline) else apply_codes(string.dup, ansi_colors) end end
Apply ANSI
color to the given string.
Wraps eachline with clear escape.
@param [String] string
text to add ANSI strings
@param [Array] colors
the color names
@example
color.decorate "text", :yellow, :on_green, :underline
@return [String]
the colored string
@api public
Source
# File lib/pastel/color.rb, line 33 def disable! @enabled = false end
Disable coloring of this terminal session
@api public
Source
# File lib/pastel/color.rb, line 225 def eql?(other) instance_of?(other.class) && enabled.eql?(other.enabled) && eachline.eql?(other.eachline) end
Compare colors for equality of attributes
@return [Boolean]
@api public
Source
# File lib/pastel/color.rb, line 254 def hash [self.class, enabled, eachline].hash end
Hash for this instance and its attributes
@return [Numeric]
@api public
Source
# File lib/pastel/color.rb, line 245 def inspect "#<#{self.class.name} enabled=#{enabled.inspect} eachline=#{eachline.inspect}>" end
Inspect this instance attributes
@return [String]
@api public
Source
# File lib/pastel/color.rb, line 136 def lookup(*colors) @cache.fetch(colors) do @cache[colors] = "\e[#{code(*colors).join(';')}m" end end
Find the escape code for a given set of color attributes
@example
color.lookup(:red, :on_green) # => "\e[31;42m"
@param [Array] colors
the list of color name(s) to lookup
@return [String]
the ANSI code(s)
@raise [InvalidAttributeNameError]
exception raised for any invalid color name
@api private
Source
# File lib/pastel/color.rb, line 103 def strip(*strings) modified = strings.map { |string| string.dup.gsub(ANSI_COLOR_REGEXP, "") } modified.size == 1 ? modified[0] : modified end
Strip ANSI
color codes from a string.
Only ANSI
color codes are removed, not movement codes or other escapes sequences are stripped.
@param [Array] strings
a string or array of strings to sanitize
@example
strip("foo\e[1mbar\e[0m") # => "foobar"
@return [String]
@api public
Source
# File lib/pastel/color.rb, line 175 def style_names styles.keys end
List all available style names
@return [Array]
@api public
Source
Source
# File lib/pastel/color.rb, line 191 def valid?(*colors) colors.all? { |color| style_names.include?(color.to_sym) } end
Check if provided colors are known colors
@param [Array]
the list of colors to check
@example
valid?(:red) # => true
@return [Boolean]
true if all colors are valid, false otherwise
@api public
Private Instance Methods
Source
# File lib/pastel/color.rb, line 265 def blank?(value) value.nil? || !value.respond_to?(:to_str) || value.to_s == "" end
Check if value contains anything to style
@return [Boolean]
@api private
Source
# File lib/pastel/color.rb, line 270 def validate(*colors) return if valid?(*colors) raise InvalidAttributeNameError, "Bad style or unintialized constant, " \ " valid styles are: #{style_names.join(', ')}." end
@api private