Class: Ollama::Utils::ColorizeTexts

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

Instance Method Summary collapse

Methods included from Width

truncate, width, wrap

Methods included from Math

#convert_to_vector, #cosine_similarity, #norm

Constructor Details

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

Initializes a new instance of Ollama::Utils::ColorizeTexts

Parameters:

  • texts (Array<String>)

    the array of strings to be displayed with colors



11
12
13
14
# File 'lib/ollama/utils/colorize_texts.rb', line 11

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.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ollama/utils/colorize_texts.rb', line 20

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