cathode
cathode copied to clipboard
Implement a control sequence parser
This should basically be a state machine that processes UTF-8 text and invokes handler methods when successfully processing sequences. This will be at the heart of all of our input processing and terminal state tracking.
Can draw inspiration from Windows Terminal: https://github.com/microsoft/terminal/tree/main/src/terminal/parser
https://vt100.net/emu/dec_ansi_parser
I'm not sure how to handle the classic ESC ambiguity in input parsing. You can only use brittle heuristics (e.g. input buffer size or timing) to attempt to distinguish an Escape key press from a longer input sequence that happens to start with the ESC character. This is necessarily going to have a sweeping effect on the parser API shape...
When I implemented ambiguous ESC key detection in my own tooling, I did a small survey of established tools and libraries to see how others handled it, and the timeout method was the most common.
The tcell
term library (which I use as a gold standard) implemented it that way here:
https://github.com/gdamore/tcell/blob/master/tscreen.go#L1624