Module: MakeMenu::ColorString

Defined in:
lib/make_menu/color_string.rb

Overview

Monkeypatch for ‘String`, adds methods to change console text colo(u)r

Constant Summary collapse

COLORS =
{
  white: 0,
  normal: 0,
  bold: 1,
  dark: 2,
  underline: 4,
  blink: 5,
  invert: 7,

  black: 30,
  red: 31,
  green: 32,
  yellow: 33,
  blue: 34,
  magenta: 35,
  cyan: 36,
  grey: 37,

  black_bg: 40,
  red_bg: 41,
  green_bg: 42,
  yellow_bg: 43,
  blue_bg: 44,
  magenta_bg: 45,
  cyan_bg: 46,
  grey_bg: 47,

  dark_grey: 90,
  light_red: 91,
  light_green: 92,
  light_yellow: 93,
  light_blue: 94,
  light_magenta: 95,
  light_cyan: 96,
  light_grey: 97,

  dark_grey_bg: 100,
  light_red_bg: 101,
  light_green_bg: 102,
  light_yellow_bg: 103,
  light_blue_bg: 104,
  light_magenta_bg: 105,
  light_cyan_bg: 106,
  light_grey_bg: 107
}.freeze

Instance Method Summary collapse

Instance Method Details

#align(alignment = :left, width: nil, char: ' ', pad_right: false) ⇒ String

Align the string, ignoring color code characters which would otherwise mess up String#center, etc. rubocop:disable Metrics/MethodLength

Parameters:

  • alignment (Symbol) (defaults to: :left)

    :left, :center, or :right

  • width (Integer) (defaults to: nil)

    The number of characters to spread the string over (default to terminal width)

  • char (String) (defaults to: ' ')

    The character to use for padding

  • pad_right (Boolean) (defaults to: false)

    Set true to include trailing spaces when aligning to :center

Returns:

  • (String)

    The padded string



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/make_menu/color_string.rb', line 123

def align(alignment = :left, width: nil, char: ' ', pad_right: false)
  width = ::TTY::Screen.cols unless width

  case alignment
  when :left
    right_pad = width - decolor.length
    "#{self}#{char * right_pad}"
  when :center
    left_pad = [(width - decolor.length) / 2, 0].max
    right_pad = width - left_pad - decolor.length
    "#{char * left_pad}#{self}#{pad_right ? char * right_pad : ''}"
  when :right
    left_pad = width - decolor.length
    "#{char * left_pad}#{self}"
  end
end

#color(color_code) ⇒ String

Apply specified color code to the String

Parameters:

  • color_code (Array, Symbol, Integer)

    Can be a key in the COLORS array, an integer ANSI code for text color, or an array of either to be applied in order

Returns:

  • (String)

    String enclosed by formatting characters



62
63
64
65
66
67
68
69
70
71
# File 'lib/make_menu/color_string.rb', line 62

def color(color_code)
  case color_code
  when Array
    color_code.inject(self) { |string, code| string.color(code) }
  when Symbol
    color(COLORS[color_code])
  else
    "\e[#{color_code}m#{self}\e[0m"
  end
end

#decolorString

Remove color codes from the string

Returns:

  • (String)

    The modified string



112
113
114
# File 'lib/make_menu/color_string.rb', line 112

def decolor
  gsub(/\e\[\d+m/, '')
end

#highlight(char, fore_color, back_color) ⇒ String

Changes all occurrences of a specified character to one color, and all other characters to another rubocop:disable Metrics/MethodLength

Examples:

“==$$==”.highlight(‘$’, :light_yellow, :red)

Parameters:

  • char (String)

    Character to highlight

  • fore_color (Symbol)

    Key of color to use for highlighted character

  • back_color (Symbol)

    Key of color to use for other characters

Returns:

  • (String)

    Highlighted text



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/make_menu/color_string.rb', line 81

def highlight(char, fore_color, back_color)
  inside_highlight = false
  output = ''
  buffer = ''
  each_char do |c|
    if c == char
      unless inside_highlight
        output += buffer.color(COLORS[back_color.to_sym])
        buffer = ''
        inside_highlight = true
      end
    elsif inside_highlight
      output += buffer.color(COLORS[fore_color.to_sym]).bold
      buffer = ''
      inside_highlight = false
    end
    buffer += c
  end

  output += if inside_highlight
              buffer.color(COLORS[fore_color.to_sym]).bold
            else
              buffer.color(COLORS[back_color.to_sym])
            end

  output
end