Class: Documentrix::Utils::ColorizeTexts

Inherits:
Object
  • Object
show all
Includes:
Math, Kramdown::ANSI::Width, Term::ANSIColor
Defined in:
lib/documentrix/utils/colorize_texts.rb

Overview

A utility class for colorizing and formatting text output with ANSI color codes and size information.

The ColorizeTexts class takes a collection of text strings and formats them with dynamically generated ANSI colors for visual distinction. Each text block is wrapped to fit the terminal width and appended with its size in bytes, making it ideal for debugging text-splitting pipelines.

Examples:

colorizer = Documentrix::Utils::ColorizeTexts.new('First chunk', 'Second chunk')
puts colorizer.to_s

Instance Method Summary collapse

Methods included from Math

#convert_to_vector, #cosine_similarity, #norm

Constructor Details

#initialize(*texts) ⇒ Documentrix::Utils::ColorizeTexts

Initializes a new instance of ColorizeTexts.

Parameters:

  • texts (String, Array<String>)

    a variable list of strings or an array of strings to be colorized.



26
27
28
# File 'lib/documentrix/utils/colorize_texts.rb', line 26

def initialize(*texts)
  @texts = texts.flatten
end

Instance Method Details

#to_sString

Returns a formatted string representation of the texts.

Each text block is:

  1. Assigned a color from a trigonometric RGB gradient.
  2. Wrapped to 90% of the terminal width.
  3. Appended with its size in bold text.

Returns:

  • (String)

    the colorized and formatted output string.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/documentrix/utils/colorize_texts.rb', line 38

def to_s
  result = +''
  @texts.each_with_index do |t, i|
    color = colors[(t.hash ^ i.hash) % colors.size]
    wrap(t, percentage: 90).each_line { |l|
      result << on_color(color) { color(text_color(color)) { l } }
    }
    result << "\n##{bold{t.size.to_s}} \n\n"
  end
  result
end