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.
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
-
.enabled ⇒ Boolean
(also: enabled?)
readonly
Check if colorization is enabled.
Class Method Summary collapse
-
.background_code(color_name) ⇒ String
Generates ANSI escape code for a background color.
-
.clear_code ⇒ String
Clears all ANSI formatting.
-
.colors ⇒ Array<Symbol>
Returns list of available colors.
-
.disable! ⇒ Boolean
Disables colorization (useful for CI/CD, logging to files, etc.).
-
.enable! ⇒ Boolean
Enables colorization (default state).
-
.foreground_code(color_name) ⇒ String
Generates ANSI escape code for a foreground color.
-
.strip(string) ⇒ String
Strips all ANSI escape codes from a string.
-
.style_code(style_name) ⇒ String
Generates ANSI escape code for a style enum.
Instance Method Summary collapse
-
#to_eol ⇒ String
Clears the line to the end (useful for dynamic terminal output).
-
#uncolorize ⇒ String
(also: #decolorize)
Clears all ANSI formatting from the string.
Class Attribute Details
.enabled ⇒ Boolean (readonly) Also known as: enabled?
Check if colorization is enabled
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
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_code ⇒ String
Clears all ANSI formatting
120 121 122 |
# File 'lib/kolor.rb', line 120 def clear_code @enabled ? "\e[0m" : '' end |
.colors ⇒ Array<Symbol>
Returns list of available colors
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.)
75 76 77 |
# File 'lib/kolor.rb', line 75 def disable! @enabled = false end |
.enable! ⇒ Boolean
Enables colorization (default state)
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
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
82 83 84 85 |
# File 'lib/kolor.rb', line 82 def strip(string) result = string.to_s result.gsub(ANSI_REGEX, '') end |