Class: WhyChain::Explainer::Colorizer

Inherits:
Object
  • Object
show all
Defined in:
lib/why_chain/explainer/colorizer.rb

Overview

Handles ANSI color output and source location formatting.

Constant Summary collapse

ANSI_CODES =
{
  title: "1;36",
  owner: "32",
  kind: "35",
  label: "2;37",
  location: "2;37",
  native: "33",
  arrow: "36"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(color_mode) ⇒ Colorizer

Returns a new instance of Colorizer.



17
18
19
# File 'lib/why_chain/explainer/colorizer.rb', line 17

def initialize(color_mode)
  @use_color = resolve_color(color_mode)
end

Instance Method Details

#colorize(text, tone) ⇒ Object



21
22
23
24
25
26
# File 'lib/why_chain/explainer/colorizer.rb', line 21

def colorize(text, tone)
  return text unless @use_color

  code = ANSI_CODES.fetch(tone, "0")
  "\e[#{code}m#{text}\e[0m"
end

#format_source_location(source_location, color: true) ⇒ Object



28
29
30
31
32
33
# File 'lib/why_chain/explainer/colorizer.rb', line 28

def format_source_location(source_location, color: true)
  formatted = source_location ? source_location.join(":") : "<native>"
  return formatted unless color

  source_location ? colorize(formatted, :location) : colorize(formatted, :native)
end