Module: Kolor

Included in:
ColorizedString, String
Defined in:
lib/kolor.rb,
lib/kolor/cli.rb,
lib/kolor/extra.rb,
lib/kolor/internal/enum.rb,
lib/kolor/internal/config.rb,
lib/kolor/internal/logger.rb,
lib/kolor/internal/version.rb

Overview

Kolor::Enum provides a declarative, type-safe registry for named values. Each entry is unique by both name and value. Values can be of any type, but you may optionally declare a type constraint using ‘type`.

Enum entries are registered via ‘.entry(name, value)` and accessed via `.name` or `[]`. Each entry becomes a singleton method of the class and returns an instance of the enum.

Examples:

Define an enum

class MyEnum < Kolor::Enum
  type Integer
  entry :low, 1
  entry :high, 2
end

Access values

MyEnum[:low].value        # => 1
MyEnum.high.to_sym        # => :high
MyEnum.low == MyEnum[:low] # => true

Defined Under Namespace

Modules: Config, Extra, Logger Classes: CLI, ColorizedString, Enum

Constant Summary collapse

ANSI_REGEX =

Regex to match ANSI escape codes

/\e\[[\d;]*m/.freeze
VERSION =
'1.0.0'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.enabledBoolean (readonly) Also known as: enabled?

Check if colorization is enabled

Returns:

  • (Boolean)


58
59
60
# File 'lib/kolor.rb', line 58

def enabled
  @enabled
end

Class Method Details

.background_code(color_name) ⇒ String

Generates ANSI escape code for a background color

Parameters:

  • color_name (Symbol)

    name of the background color

Returns:

  • (String)

    ANSI escape code or empty string if disabled or unknown



110
111
112
113
114
115
# File 'lib/kolor.rb', line 110

def background_code(color_name)
  return '' unless @enabled

  color = Kolor::Enum::Background[color_name]
  color ? "\e[#{color.value}m" : ''
end

.clear_codeString

Clears all ANSI formatting

Returns:

  • (String)

    ANSI clear code or empty string if disabled



120
121
122
# File 'lib/kolor.rb', line 120

def clear_code
  @enabled ? "\e[0m" : ''
end

.colorsArray<Symbol>

Returns list of available colors

Returns:

  • (Array<Symbol>)

    sorted list of color names



63
64
65
# File 'lib/kolor.rb', line 63

def colors
  @colors ||= Kolor::Enum::Foreground.keys.sort
end

.disable!Boolean

Disables colorization (useful for CI/CD, logging to files, etc.)

Returns:

  • (Boolean)

    false



75
76
77
# File 'lib/kolor.rb', line 75

def disable!
  @enabled = false
end

.enable!Boolean

Enables colorization (default state)

Returns:

  • (Boolean)

    true



69
70
71
# File 'lib/kolor.rb', line 69

def enable!
  @enabled = true
end

.foreground_code(color_name) ⇒ String

Generates ANSI escape code for a foreground color

Parameters:

  • color_name (Symbol)

    name of the foreground color

Returns:

  • (String)

    ANSI escape code or empty string if disabled or unknown



100
101
102
103
104
105
# File 'lib/kolor.rb', line 100

def foreground_code(color_name)
  return '' unless @enabled

  color = Kolor::Enum::Foreground[color_name]
  color ? "\e[#{color.value}m" : ''
end

.strip(string) ⇒ String

Strips all ANSI escape codes from a string

Parameters:

  • string (String)

    string with ANSI codes

Returns:

  • (String)

    clean string without ANSI codes



82
83
84
85
# File 'lib/kolor.rb', line 82

def strip(string)
  result = string.to_s
  result.gsub(ANSI_REGEX, '')
end

.style_code(style_name) ⇒ String

Generates ANSI escape code for a style enum

Parameters:

  • style_name (Symbol)

    symbolic name of the style (e.g. :bold, :underline)

Returns:

  • (String)

    ANSI escape code (e.g. “e[1m”) or empty string if disabled or unknown



90
91
92
93
94
95
# File 'lib/kolor.rb', line 90

def style_code(style_name)
  return '' unless @enabled

  style = Kolor::Enum::Style[style_name]
  style ? "\e[#{style.value}m" : ''
end

Instance Method Details

#to_eolString

Clears the line to the end (useful for dynamic terminal output)

Returns:

  • (String)

    string with clear-to-end-of-line code



190
191
192
193
194
195
196
# File 'lib/kolor.rb', line 190

def to_eol
  return to_s unless Kolor.enabled?

  str = to_s
  modified = str.sub(/^(\e\[[\d;]*m)/, "\\1\e[0K")
  modified == str ? "\e[0K#{str}" : modified
end

#uncolorizeString Also known as: decolorize

Clears all ANSI formatting from the string

Returns:

  • (String)

    plain string without ANSI codes



200
201
202
# File 'lib/kolor.rb', line 200

def uncolorize
  Kolor.strip(to_s)
end