Module: PaintbrushSupport::Colors

Defined in:
lib/paintbrush_support/colors.rb

Overview

Provides methods that are temporarily injected into block context. Each method returns an escaped string including the current stack size with start and end escape codes, allowing the string to be reconstituted afterwards with nested strings restoring the previous color once they have terminated.

Constant Summary collapse

COLOR_CODES =
{
  black: '30',
  red: '31',
  green: '32',
  yellow: '33',
  blue: '34',
  purple: '35',
  cyan: '36',
  white: '37',
  default: '39',
  black_b: '90',
  red_b: '91',
  green_b: '92',
  yellow_b: '93',
  blue_b: '94',
  purple_b: '95',
  cyan_b: '96',
  white_b: '97'
}.freeze
HEX_CODE_REGEXP =
/(?:hex_[a-fA-F0-9]{3}){1,2}/.freeze

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/paintbrush_support/colors.rb', line 41

def method_missing(method_name, *args)
  return super unless method_name.match?(HEX_CODE_REGEXP)
  return string unless Configuration.colorize?

  return unless Configuration.colorize?

  ColorElement.new(
    stack: @__stack,
    code: HexColorCode.new(hex_code: method_name).escape_sequence,
    string: args.first
  ).to_s
end

Instance Method Details

#respond_to_missing?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/paintbrush_support/colors.rb', line 54

def respond_to_missing?(*)
  return super unless method_name.match?(HEX_CODE_REGEXP)

  true
end