Module: Ollama::Utils::Width

Extended by:
Term::ANSIColor
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



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

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:)
  ellipsis_length = ellipsis.size
  if length < ellipsis_length
    +''
  elsif text.size >= length + ellipsis_length
    text[0, length - ellipsis_length] + ellipsis
  else
    text
  end
end

.width(percentage: 100.0) ⇒ Object



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

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

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



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

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