Module: MakeMenu::Console::ColorString

Defined in:
lib/make_menu/console/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) ⇒ Object

Align the string, ignoring color code characters which would otherwise mess up String#center, etc.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/make_menu/console/color_string.rb', line 110

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

  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

#align_block(alignment = :left) ⇒ Object

Align each line of a string



128
129
130
131
132
# File 'lib/make_menu/console/color_string.rb', line 128

def align_block(alignment = :left)
  split("\n")
    .map { |line| line.align(alignment) }
    .join("\n")
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



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

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

#decolorObject

Remove color codes from the string



105
106
107
# File 'lib/make_menu/console/color_string.rb', line 105

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

#highlight(char, fore_color, back_color) ⇒ Object

Changes all occurrences of ‘char` to `fore_color` and all other characters to `back_color`



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/make_menu/console/color_string.rb', line 76

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