Sha256: a5b6634e1a912dc0492d65e362384e76bb777e6319722b9bd3e6bdfa403c0b66

Contents?: true

Size: 973 Bytes

Versions: 5

Compression:

Stored size: 973 Bytes

Contents

class Ollama::Utils::ColorizeTexts
  include Math
  include Term::ANSIColor
  include Ollama::Utils::Width

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

  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

  private

  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 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
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ollama-ruby-0.3.2 lib/ollama/utils/colorize_texts.rb
ollama-ruby-0.3.1 lib/ollama/utils/colorize_texts.rb
ollama-ruby-0.3.0 lib/ollama/utils/colorize_texts.rb
ollama-ruby-0.2.0 lib/ollama/utils/colorize_texts.rb
ollama-ruby-0.1.0 lib/ollama/utils/colorize_texts.rb