<commit_before>require "forwardable"
require_relative "../ansi_palette"

module AnsiPalette
  START_ESCAPE = "\e[" # "\033["
  END_ESCAPE   = "m"
  RESET_COLOR  = 0

  COLOR_HASH = {
    :black   => { :foreground => 30, :background => 40 },
    :red     => { :foreground => 31, :background => 41 },
    :green   => { :foreground => 32, :background => 42 },
    :yellow  => { :foreground => 33, :background => 43 },
    :blue    => { :foreground => 34, :background => 44 },
    :magenta => { :foreground => 35, :background => 45 },
    :cyan    => { :foreground => 36, :background => 46 },
    :white   => { :foreground => 37, :background => 47 },
  }

  EFFECT_HASH = {
    :reset          => 0,
    :bold           => 1,
    :underline      => 4,
    :blink          => 5,
    :inverse_colors => 7
  }

  COLOR_HASH.each_pair.each do |color, color_codes|
    define_method color.to_s.capitalize do |string|
      AnsiPalette::ColoredString.new(string: string, color: color)
    end

    define_method "Bg" + color.to_s.capitalize do |string|
      AnsiPalette::ColoredString.new(string: string, background: color)
    end

    const_set("#{color.upcase}_FG", color_codes.fetch(:foreground))
    const_set("#{color.upcase}_BG", color_codes.fetch(:background))
  end

  class ColoredString < String
    extend ::Forwardable

    def initialize(string:,
                   color: nil,
                   foreground: nil,
                   background: nil,
                   modifier: nil,
                   bold: false,
                   blink: false
                  )

      @string     = string
      @color      = color
      @modifier   = modifier
      @background = background
      @foreground = foreground
      @bold       = bold
      @blink      = blink
    end

    EFFECT_HASH.keys.each do |modifier_method|
      attr_accessor modifier_method
    end

    attr_accessor :modifier

    def colored_string
      set_modifiers +
        set_foreground_color +
        set_background_color +
        string +
        reset_color
    end

    def blink?; blink; end

    def bold?; bold; end

    alias_method :to_s, :colored_string
    alias_method :to_str, :to_s

    def_delegators :string, :length<commit_msg>Adds documentation around meta programmed methods<commit_after>require "forwardable"
require_relative "../ansi_palette"

module AnsiPalette
  START_ESCAPE = "\e[" # "\033["
  END_ESCAPE   = "m"
  RESET_COLOR  = 0

  COLOR_HASH = {
    :black   => { :foreground => 30, :background => 40 },
    :red     => { :foreground => 31, :background => 41 },
    :green   => { :foreground => 32, :background => 42 },
    :yellow  => { :foreground => 33, :background => 43 },
    :blue    => { :foreground => 34, :background => 44 },
    :magenta => { :foreground => 35, :background => 45 },
    :cyan    => { :foreground => 36, :background => 46 },
    :white   => { :foreground => 37, :background => 47 },
  }

  EFFECT_HASH = {
    :reset          => 0,
    :bold           => 1,
    :underline      => 4,
    :blink          => 5,
    :inverse_colors => 7
  }

  COLOR_HASH.each_pair.each do |color, color_codes|

    # defines the following methods:
    #   Black, Red, Green, Yellow, Blue, Magenta, Cyan, White
    #
    # which all return a ColoredString instance
    define_method color.to_s.capitalize do |string|
      AnsiPalette::ColoredString.new(string: string, color: color)
    end

    # defines the following methods:
    #   BlackBg, RedBg, GreenBg, YellowBg, BlueBg, MagentaBg, CyanBg, WhiteBg
    #
    # which all return a ColoredString instance
    define_method "Bg" + color.to_s.capitalize do |string|
      AnsiPalette::ColoredString.new(string: string, background: color)
    end

    const_set("#{color.upcase}_FG", color_codes.fetch(:foreground))
    const_set("#{color.upcase}_BG", color_codes.fetch(:background))
  end

  class ColoredString < String
    extend ::Forwardable

    def initialize(string:,
                   color: nil,
                   foreground: nil,
                   background: nil,
                   modifier: nil,
                   bold: false,
                   blink: false)

      @string     = string
      @color      = color
      @modifier   = modifier
      @background = background
      @foreground = foreground
      @bold       = bold
      @blink      = blink
    end

    # Defines the following methods:
    #   reset=, bold=, underline=, blink=, inverse_colors=
    EFFECT_HASH.keys.each do |modifier_method|
      attr_accessor modifier_method
    end

    attr_accessor :modifier

    def colored_string
      set_modifiers +
        set_foreground_color +
        set_background_color +
        string +
        reset_color
    end

    def blink?; blink; end

    def bold?; bold; end

    alias_method :to_s, :colored_string
    alias_method :to_str, :to_s

    def_delegators :string, :length
{'fraction_non_alphanumeric': 0.11661211129296235, 'fraction_numerical': 0.016775777414075287, 'mean_word_length': 2.2143326758711375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '503891', 'n_tokens_mistral': 1652, 'n_tokens_neox': 1502, 'n_words': 369}