Class: AsciinemaWin::AnsiParser
- Inherits:
-
Object
- Object
- AsciinemaWin::AnsiParser
- 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
-
#height ⇒ Integer
readonly
Terminal height.
-
#lines ⇒ Array<ParsedLine>
readonly
Parsed lines.
-
#width ⇒ Integer
readonly
Terminal width.
Instance Method Summary collapse
-
#initialize(width:, height:) ⇒ AnsiParser
constructor
Initialize parser with terminal dimensions.
-
#parse(content) ⇒ Array<ParsedLine>
Parse ANSI content and build styled character grid.
Constructor Details
#initialize(width:, height:) ⇒ AnsiParser
Initialize parser with terminal dimensions
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
#height ⇒ Integer (readonly)
Returns Terminal height.
117 118 119 |
# File 'lib/asciinema_win/ansi_parser.rb', line 117 def height @height end |
#lines ⇒ Array<ParsedLine> (readonly)
Returns Parsed lines.
111 112 113 |
# File 'lib/asciinema_win/ansi_parser.rb', line 111 def lines @lines end |
#width ⇒ Integer (readonly)
Returns 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
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 |