Class: SwarmCLI::UI::Formatters::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_cli/ui/formatters/text.rb

Overview

Text manipulation utilities for clean display

Class Method Summary collapse

Class Method Details

.indent(text, level: 0, char: " ") ⇒ Object

Indent all lines in text



58
59
60
61
62
63
# File 'lib/swarm_cli/ui/formatters/text.rb', line 58

def indent(text, level: 0, char: "  ")
  return "" if text.nil? || text.empty?

  prefix = char * level
  text.split("\n").map { |line| "#{prefix}#{line}" }.join("\n")
end

.strip_system_reminders(text) ⇒ Object

Strip tags from content



10
11
12
13
14
# File 'lib/swarm_cli/ui/formatters/text.rb', line 10

def strip_system_reminders(text)
  return "" if text.nil?

  text.gsub(%r{<system-reminder>.*?</system-reminder>}m, "").strip
end

.truncate(text, chars: nil, lines: nil) ⇒ Object

Truncate text to specified character/line limits Returns [display_text, truncation_message]



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/swarm_cli/ui/formatters/text.rb', line 18

def truncate(text, chars: nil, lines: nil)
  return [text, nil] if text.nil? || text.empty?

  text_lines = text.split("\n")
  truncated = false
  truncation_parts = []

  # Apply line limit
  if lines && text_lines.length > lines
    text_lines = text_lines.first(lines)
    hidden_lines = text.split("\n").length - lines
    truncation_parts << "#{hidden_lines} more lines"
    truncated = true
  end

  result_text = text_lines.join("\n")

  # Apply character limit
  if chars && result_text.length > chars
    result_text = result_text[0...chars]
    hidden_chars = text.length - chars
    truncation_parts << "#{hidden_chars} more chars"
    truncated = true
  end

  truncation_msg = truncated ? "... (#{truncation_parts.join(", ")})" : nil

  [result_text, truncation_msg]
end

.wrap(text, width:) ⇒ Object

Wrap text to specified width



49
50
51
52
53
54
55
# File 'lib/swarm_cli/ui/formatters/text.rb', line 49

def wrap(text, width:)
  return "" if text.nil? || text.empty?

  text.split("\n").flat_map do |line|
    wrap_line(line, width)
  end.join("\n")
end