Class: Documentrix::Utils::ColorizeTexts
- Inherits:
-
Object
- Object
- Documentrix::Utils::ColorizeTexts
- 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.
Instance Method Summary collapse
-
#initialize(*texts) ⇒ Documentrix::Utils::ColorizeTexts
constructor
Initializes a new instance of ColorizeTexts.
-
#to_s ⇒ String
Returns a formatted string representation of the texts.
Methods included from Math
#convert_to_vector, #cosine_similarity, #norm
Constructor Details
#initialize(*texts) ⇒ Documentrix::Utils::ColorizeTexts
Initializes a new instance of ColorizeTexts.
26 27 28 |
# File 'lib/documentrix/utils/colorize_texts.rb', line 26 def initialize(*texts) @texts = texts.flatten end |
Instance Method Details
#to_s ⇒ String
Returns a formatted string representation of the texts.
Each text block is:
- Assigned a color from a trigonometric RGB gradient.
- Wrapped to 90% of the terminal width.
- Appended with its size in bold text.
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 |