Class: LogBench::App::Renderer::Ansi

Inherits:
Object
  • Object
show all
Includes:
Curses
Defined in:
lib/log_bench/app/renderer/ansi.rb

Instance Method Summary collapse

Constructor Details

#initialize(screen) ⇒ Ansi

Returns a new instance of Ansi.



9
10
11
# File 'lib/log_bench/app/renderer/ansi.rb', line 9

def initialize(screen)
  self.screen = screen
end

Instance Method Details

#has_ansi_codes?(text) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/log_bench/app/renderer/ansi.rb', line 13

def has_ansi_codes?(text)
  text.match?(/\e\[[0-9;]*m/)
end

#parse_and_render(text, win) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/log_bench/app/renderer/ansi.rb', line 17

def parse_and_render(text, win)
  parts = text.split(/(\e\[[0-9;]*m)/)
  current_color = nil

  parts.each do |part|
    if part =~ /\e\[([0-9;]*)m/
      # ANSI escape code
      codes = $1.split(";").map(&:to_i)
      current_color = ansi_to_curses_color(codes)
    elsif current_color && !part.empty?
      # Text content
      win.attron(current_color) { win.addstr(part) }
    elsif !part.empty?
      win.addstr(part)
    end
  end
end

#wrap_ansi_text(text, max_width) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/log_bench/app/renderer/ansi.rb', line 35

def wrap_ansi_text(text, max_width)
  clean_text = text.gsub(/\e\[[0-9;]*m/, "")

  if clean_text.length <= max_width
    [text]
  else
    chunks = []
    remaining = text
    active_colors = []

    # Extract initial color state
    text.scan(/\e\[[0-9;]*m/) do |ansi_code|
      if /\e\[0m/.match?(ansi_code)
        active_colors.clear
      else
        active_colors << ansi_code
      end
    end

    while remaining.length > 0
      clean_remaining = remaining.gsub(/\e\[[0-9;]*m/, "")

      if clean_remaining.length <= max_width
        # Last chunk
        chunks << if active_colors.any? && !remaining.start_with?(*active_colors)
          active_colors.join("") + remaining
        else
          remaining
        end
        break
      else
        # Find break point and preserve color state
        break_point = max_width
        original_pos = 0
        clean_pos = 0
        chunk_colors = active_colors.dup

        remaining.each_char.with_index do |char, idx|
          if /^\e\[[0-9;]*m/.match?(remaining[idx..])
            # Found ANSI sequence
            ansi_match = remaining[idx..].match(/^(\e\[[0-9;]*m)/)
            ansi_code = ansi_match[1]

            if /\e\[0m/.match?(ansi_code)
              chunk_colors.clear
              active_colors.clear
            else
              chunk_colors << ansi_code unless chunk_colors.include?(ansi_code)
              active_colors << ansi_code unless active_colors.include?(ansi_code)
            end

            original_pos += ansi_code.length
            idx + ansi_code.length - 1
          else
            clean_pos += 1
            original_pos += 1

            if clean_pos >= break_point
              break
            end
          end
        end

        chunk_text = remaining[0...original_pos]
        chunks << if active_colors.any? && !chunk_text.start_with?(*active_colors)
          active_colors.join("") + chunk_text
        else
          chunk_text
        end

        remaining = remaining[original_pos..]
      end
    end

    chunks
  end
end

#wrap_plain_text(text, max_width) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/log_bench/app/renderer/ansi.rb', line 113

def wrap_plain_text(text, max_width)
  # Simple text wrapping for plain text
  if text.length <= max_width
    [text]
  else
    chunks = []
    remaining = text

    while remaining.length > 0
      if remaining.length <= max_width
        chunks << remaining
        break
      else
        # Find a good break point (try to break on spaces)
        break_point = max_width
        if remaining[0...max_width].include?(" ")
          # Find the last space within the limit
          break_point = remaining[0...max_width].rindex(" ") || max_width
        end

        chunks << remaining[0...break_point]
        remaining = remaining[break_point..].lstrip
      end
    end

    chunks
  end
end