class Ollama::Utils::ColorizeTexts

def colors

Returns:
  • (Array>) - An array of RGB color arrays
def colors
  @colors ||= (0..255).map { |i|
    [
      128 + 128 * sin(PI * i / 32.0),
      128 + 128 * sin(PI * i / 64.0),
      128 + 128 * sin(PI * i / 128.0),
    ].map { _1.clamp(0, 255).round }
  }
end

def initialize(*texts)

Returns:
  • (Ollama::Utils::ColorizeTexts) - an instance of Ollama::Utils::ColorizeTexts

Parameters:
  • texts (Array) -- the array of strings to be displayed with colors
def initialize(*texts)
  texts  = texts.map(&:to_a)
  @texts = Array(texts.flatten)
end

def text_color(color)

Returns:
  • (Array) - An array containing two RGB colors, one for black and

Parameters:
  • color (color) -- The ANSI color attribute
def text_color(color)
  color = Term::ANSIColor::Attribute[color]
  [
    Attribute.nearest_rgb_color('#000'),
    Attribute.nearest_rgb_color('#fff'),
  ].max_by { |t| t.distance_to(color) }
end

def to_s

Returns:
  • (String) - The formatted string.
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