Module: TerminalColors::Apply

Defined in:
lib/terminal_colors/apply.rb

Class Method Summary collapse

Class Method Details

.call(string, fg: nil, bg: nil, bold: nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/terminal_colors/apply.rb', line 3

def self.call string, fg: nil, bg: nil, bold: nil
  output = String.new

  styled = fg || bg || bold

  if styled
    fg = Palette.fetch fg if fg
    bg = Palette.fetch bg if bg
    bold_octet = bold ? '1' : '0'

    output << "\e[#{bold_octet}"
    output << ";" if fg or bg
    output << "3#{fg}" if fg
    output << ";" if fg and bg
    output << "4#{bg}" if bg
    output << "m"
  end

  output << string

  if styled
    output << "\e[0m"
  end

  output
end