Adding Features to a Terminal Emulator (cursor, and control sequences)

I am attempting to build a terminal emulator as a learning experience. At the moment, I have an egui ui with a pty I am trying to figure out how to go further from this. The two most immediate problems that I can see are (A) drawing the cursor, and (B) dealing with Unicode control sequences (backspace, ctrl-c, etc.). For both of these, however, I have no idea how to go further...any advice for learning Rustacean? The github repo is here.

Terminal emulators can be as complicated as you like!

In theory you're "just" parsing the VT codes coming from the client to update your state, and emitting the VT codes appropriate for the user's keyboard input. (You generally don't directly update your state in response to input, which is an easy mistake to make, echoing that back to you is the client's job if it wants that behavior)

You will need to keep a view buffer with the character and attributes for each cell with a width and height, resizing that if supported, notifying the client when its resized with a SIGWINCH, keeping a cursor position in that buffer, a "current" attribute set (eg colors) and to update the buffer, cursor position or current attributes based on what output you parsed from the client.

In the simplest case, when the client emits character data you write that and the current attributes to the cursor position, then move it to the right. Otherwise it's as appropriate for the control sequence, Wikipedia is decent enough for toys here, eg LF moves the cursor down or scrolls if it's at the bottom, the SGR codes change the current attributes, etc...

If you want to try parsing VT yourself as well, here's a description of how to parse like a DEC VT100 precisely that should still cover pretty much everything in use, though you'll only need the left side (CSI sequences via escape) for the overwhelming majority of common escape sequences: A parser for DEC’s ANSI-compatible video terminals


Mb, sorry about that...I fixed the link.

1 Like