Class: AsciinemaWin::AnsiParser

Inherits:
Object
  • Object
show all
Defined in:
lib/asciinema_win/ansi_parser.rb

Overview

ANSI escape sequence parser for rendering colored output

Parses ANSI SGR (Select Graphic Rendition) codes and converts terminal output into structured data for SVG/HTML rendering.

Defined Under Namespace

Classes: ParsedLine, StyleState, StyledChar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width:, height:) ⇒ AnsiParser

Initialize parser with terminal dimensions

Parameters:

  • width (Integer)

    Terminal width in characters

  • height (Integer)

    Terminal height in lines



123
124
125
126
127
128
129
130
131
# File 'lib/asciinema_win/ansi_parser.rb', line 123

def initialize(width:, height:)
  @width = width
  @height = height
  @lines = []
  @current_line = []
  @cursor_x = 0
  @cursor_y = 0
  @state = StyleState.new
end

Instance Attribute Details

#heightInteger (readonly)

Returns Terminal height.

Returns:

  • (Integer)

    Terminal height



117
118
119
# File 'lib/asciinema_win/ansi_parser.rb', line 117

def height
  @height
end

#linesArray<ParsedLine> (readonly)

Returns Parsed lines.

Returns:



111
112
113
# File 'lib/asciinema_win/ansi_parser.rb', line 111

def lines
  @lines
end

#widthInteger (readonly)

Returns Terminal width.

Returns:

  • (Integer)

    Terminal width



114
115
116
# File 'lib/asciinema_win/ansi_parser.rb', line 114

def width
  @width
end

Instance Method Details

#parse(content) ⇒ Array<ParsedLine>

Parse ANSI content and build styled character grid

Parameters:

  • content (String)

    Raw ANSI content

Returns:



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/asciinema_win/ansi_parser.rb', line 137

def parse(content)
  # Initialize empty grid
  @height.times do |y|
    @lines[y] = ParsedLine.new(
      chars: Array.new(@width) { @state.to_styled_char(" ") },
      line_number: y
    )
  end

  pos = 0
  while pos < content.length
    if content[pos] == "\e" && content[pos + 1] == "["
      # ANSI escape sequence
      end_pos = find_sequence_end(content, pos + 2)
      if end_pos
        sequence = content[pos + 2...end_pos]
        command = content[end_pos]
        process_escape(sequence, command)
        pos = end_pos + 1
      else
        pos += 1
      end
    elsif content[pos] == "\r"
      @cursor_x = 0
      pos += 1
    elsif content[pos] == "\n"
      @cursor_x = 0
      @cursor_y += 1
      scroll_if_needed
      pos += 1
    elsif content[pos] == "\t"
      # Tab - move to next 8-column boundary
      spaces = 8 - (@cursor_x % 8)
      spaces.times { write_char(" ") }
      pos += 1
    elsif content[pos] == "\b"
      # Backspace
      @cursor_x = [@cursor_x - 1, 0].max
      pos += 1
    elsif content[pos].ord >= 32 || content[pos].ord == 0
      write_char(content[pos])
      pos += 1
    else
      pos += 1
    end
  end

  @lines
end