The current parser is a bit adhoc and doesn't handle invalid escape sequences well.
Here is a document that explains how to implement a proper parser using a state machine: https://vt100.net/emu/dec_ansi_parser
Here is a TypeScript implementation I found: https://github.com/xtermjs/xterm.js/blob/522c8bf3afcc296f7586c21b097025fe3626dc32/src/common/parser/EscapeSequenceParser.ts#L201
The current parser is a bit adhoc and doesn't handle invalid escape sequences well.
Here is a document that explains how to implement a proper parser using a state machine: https://vt100.net/emu/dec_ansi_parser
Here is a TypeScript implementation I found: https://github.com/xtermjs/xterm.js/blob/522c8bf3afcc296f7586c21b097025fe3626dc32/src/common/parser/EscapeSequenceParser.ts#L201