Module: Crayon::StringBuilder

Included in:
Crayon
Defined in:
lib/crayon/string_builder.rb

Instance Method Summary collapse

Instance Method Details

#background_stringObject



20
21
22
# File 'lib/crayon/string_builder.rb', line 20

def background_string
  handle_color(4, @background)
end

#foreground_stringObject



16
17
18
# File 'lib/crayon/string_builder.rb', line 16

def foreground_string
  handle_color(3, @foreground)
end

#formatting_stringObject



24
25
26
27
# File 'lib/crayon/string_builder.rb', line 24

def formatting_string
  return "" if @formatting.empty?
  @formatting.map{|format| "\e[#{FORMATS[format]}m"}.join("")
end

#handle_color(lead, color) ⇒ Object



29
30
31
32
# File 'lib/crayon/string_builder.rb', line 29

def handle_color(lead, color)
  return "" unless color
  "\e[#{lead}#{COLORS[color]}m"
end

#has_color?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/crayon/string_builder.rb', line 12

def has_color?
  @foreground || @background || @formatting.any?
end

#prepare_string(string) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/crayon/string_builder.rb', line 3

def prepare_string(string)
  [ foreground_string,
    background_string,
    formatting_string,
    string,
    (TERMINATION_STRING if has_color?)
  ].compact.join("")
end