Class: Documentrix::Utils::ColorizeTexts

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

Instance Method Summary collapse

Methods included from Math

#convert_to_vector, #cosine_similarity, #norm

Constructor Details

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

Initializes a new instance of Documentrix::::ColorizeTexts

Parameters:

  • texts (Array<String>)

    the array of strings to be displayed with colors



14
15
16
17
# File 'lib/documentrix/utils/colorize_texts.rb', line 14

def initialize(*texts)
  texts  = texts.map(&:to_a)
  @texts = Array(texts.flatten)
end

Instance Method Details

#to_sString

Returns a string representation of the object, including all texts content, colored differently and their sizes.

Returns:

  • (String)

    The formatted string.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/documentrix/utils/colorize_texts.rb', line 23

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