Module: Corefines::String::Color

Defined in:
lib/corefines/string.rb

Constant Summary collapse

COLOR_CODES =
{
  black:   0, light_black:   60,
  red:     1, light_red:     61,
  green:   2, light_green:   62,
  yellow:  3, light_yellow:  63,
  blue:    4, light_blue:    64,
  magenta: 5, light_magenta: 65,
  cyan:    6, light_cyan:    66,
  white:   7, light_white:   67,
  default: 9
}
MODE_CODES =
{
  default:   0,  # Turn off all attributes.
  bold:      1,  # Set bold mode.
  underline: 4,  # Set underline mode.
  blink:     5,  # Set blink mode.
  swap:      7,  # Exchange foreground and background colors.
  hide:      8   # Hide text (foreground color would be the same as background).
}

Instance Method Summary collapse

Instance Method Details

#color(text_color) ⇒ String #color(opts) ⇒ String

Returns a copy of this string colored for command line output using ANSI escape codes.

Examples:

"Roses are red".color(:red) # => "\e[0;31;49mRoses are red\e[0m"
"Roses are red".color(text: :red, mode: :bold) # => "\e[1;31;49mRoses are red\e[0m"
"Violets are blue".color(background: 'blue') # => "\e[0;39;44mViolets are blue\e[0m"
"Sugar is sweet".color(text: 7) # => "\e[0;37;49mSugar is sweet\e[0m"

Overloads:

  • #color(opts) ⇒ String

    Options Hash (opts):

    • :mode (#to_sym, Integer)

      text attributes (see MODE_CODES).

    • :text,:fg (#to_sym, Integer)

      text (foreground) color (see COLOR_CODES).

    • :background,:bg (#to_sym, Integer)

      background color (see COLOR_CODES).

See Also:


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/corefines/string.rb', line 93

module Color

  COLOR_CODES = {
    black:   0, light_black:   60,
    red:     1, light_red:     61,
    green:   2, light_green:   62,
    yellow:  3, light_yellow:  63,
    blue:    4, light_blue:    64,
    magenta: 5, light_magenta: 65,
    cyan:    6, light_cyan:    66,
    white:   7, light_white:   67,
    default: 9
  }

  MODE_CODES = {
    default:   0,  # Turn off all attributes.
    bold:      1,  # Set bold mode.
    underline: 4,  # Set underline mode.
    blink:     5,  # Set blink mode.
    swap:      7,  # Exchange foreground and background colors.
    hide:      8   # Hide text (foreground color would be the same as background).
  }

  refine ::String do
    def color(opts)
      opts = {text: opts} unless opts.is_a? ::Hash
      opts[:fg] ||= opts[:text] || opts[:color]
      opts[:bg] ||= opts[:background]

      scan(ESCAPE_SEQUENCE).reduce('') do |str, match|
        mode     = Color.mode_code(opts[:mode])    || match[0] || 0
        fg_color = Color.color_code(opts[:fg], 30) || match[1] || 39
        bg_color = Color.color_code(opts[:bg], 40) || match[2] || 49
        text     = match[3] || match[4]

        str << "\033[#{mode};#{fg_color};#{bg_color}m#{text}\033[0m"
      end
    end
  end

  private

  def self.color_code(color, offset)
    return color + offset if color.is_a? ::Integer
    COLOR_CODES[color.to_sym] + offset if color && COLOR_CODES[color.to_sym]
  end

  def self.mode_code(mode)
    return mode if mode.is_a? ::Integer
    MODE_CODES[mode.to_sym] if mode
  end
end