Module: Ollama::Utils::Width

Includes:
Term::ANSIColor
Included in:
Documents, ANSIMarkdown, ColorizeTexts
Defined in:
lib/ollama/utils/width.rb

Class Method Summary collapse

Class Method Details

.truncate(text, percentage: nil, length: nil, ellipsis: ?…) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ollama/utils/width.rb', line 25

def truncate(text, percentage: nil, length: nil, ellipsis: ?…)
  percentage.nil? ^ length.nil? or
    raise ArgumentError, "either pass percentage or length argument"
  percentage and length ||= width(percentage:)
  if length < 1
    +''
  elsif text.size > length
    text[0, length - 1] + ?…
  else
    text
  end
end

.width(percentage: 100.0) ⇒ Object



8
9
10
# File 'lib/ollama/utils/width.rb', line 8

def width(percentage: 100.0)
  ((Float(percentage) * Tins::Terminal.columns) / 100).floor
end

.wrap(text, percentage: nil, length: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ollama/utils/width.rb', line 12

def wrap(text, percentage: nil, length: nil)
  percentage.nil? ^ length.nil? or
    raise ArgumentError, "either pass percentage or length argument"
  percentage and length ||= width(percentage:)
  text.gsub(/(?<!\n)\n(?!\n)/, ' ').lines.map do |line|
    if length >= 1 && uncolor { line }.length > length
      line.gsub(/(.{1,#{length}})(\s+|$)/, "\\1\n").strip
    else
      line.strip
    end
  end * ?\n
end