Class: DebugLogging::ColorizedString
- Inherits:
-
String
- Object
- String
- DebugLogging::ColorizedString
- Defined in:
- lib/debug_logging/colorized_string.rb
Constant Summary collapse
- FOREGROUND_CODES =
{ black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37, light_black: 90, light_red: 91, light_green: 92, light_yellow: 93, light_blue: 94, light_magenta: 95, light_cyan: 96, light_white: 97, default: 39, }.freeze
- BACKGROUND_CODES =
FOREGROUND_CODES.transform_values { |code| code + 10 }.merge(default: 49).freeze
- FORMAT_CODES =
{ bold: 1, italic: 3, underline: 4, blink: 5, reverse_color: 7, conceal: 8, hide: 8, strikethrough: 9, double_underline: 21, }.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #colorize(color = nil, options = {}) ⇒ Object
- #colorized? ⇒ Boolean
-
#initialize(value, foreground: nil, background: nil, formats: []) ⇒ ColorizedString
constructor
A new instance of ColorizedString.
- #uncolorize ⇒ Object
Constructor Details
#initialize(value, foreground: nil, background: nil, formats: []) ⇒ ColorizedString
Returns a new instance of ColorizedString.
43 44 45 46 47 48 49 |
# File 'lib/debug_logging/colorized_string.rb', line 43 def initialize(value, foreground: nil, background: nil, formats: []) @raw = value.to_s @foreground = foreground @background = background @formats = formats.dup super(render) end |
Class Method Details
.[](value) ⇒ Object
38 39 40 |
# File 'lib/debug_logging/colorized_string.rb', line 38 def [](value) new(value.to_s) end |
Instance Method Details
#colorize(color = nil, options = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/debug_logging/colorized_string.rb', line 51 def colorize(color = nil, = {}) = color if color.is_a?(Hash) foreground = .fetch(:color, nil) || .fetch("color", nil) foreground ||= color unless color.is_a?(Hash) background = .fetch(:background, nil) || .fetch("background", nil) formats = Array([:mode]) + Array([:modes]) self.class.new( @raw, foreground: foreground ? normalized_color(foreground) : @foreground, background: background ? normalized_color(background) : @background, formats: merge_formats(formats), ) end |
#colorized? ⇒ Boolean
66 67 68 |
# File 'lib/debug_logging/colorized_string.rb', line 66 def colorized? @foreground || @background || @formats.any? end |
#uncolorize ⇒ Object
70 71 72 |
# File 'lib/debug_logging/colorized_string.rb', line 70 def uncolorize @raw.dup end |