I’m pondering creating a WPF or Silverlight app that acts just like a terminal window. Except, since it is in WPF/Silverlight, it will be able to ‘enhance’ the terminal experience with effects, images, etc.
I’m trying to figure out the best way to emulate a terminal. I know how to handle the VT100 emulation as far as parsing, etc. But how to display it? I considered using a RichTextBox and essentially converting the VT100 escape codes into RTF.
The problem I see with that is performance. The terminal may be getting only a few characters at a time, and to be able to load them into the textbox as-we-go I would constantly be creating TextRanges and using Load() to load the RTF. Also, in order for each loading ‘session’ to be complete, it would have to be fully describing RTF. For example, if the current color is Red, each load into the TextBox would need the RTF codes to make the text red, or I assume the RTB won’t load it as red.
This seems very redundant — the resulting RTF document built by the emulation will be extremely messy. Also, movement of the caret doesn’t seem like it would be ideally handled by the RTB. I need something custom, methinks, but that scares me!
Hoping to hear bright ideas or pointers to existing solutions. Perhaps there is a way to embed an actual terminal and overlay stuff on top of it. The only thing I’ve found is an old WinForms control.
UPDATE: See how the proposed solution fails due to perf in my answer below. 🙁
VT100 Terminal Emulation in Windows WPF or Silverlight
If you try to implement this with RichTextBox and RTF you will quickly run into many limitations and find yourself spending much more time working around differences than if you implemented the functionality yourself.
In fact it is quite easy to implement VT100 terminal emulation using WPF. I know because just now I implemented an almost-complete VT100 emulator in an hour or so. To be precise, I implmented everything except:
The most interesting parts were:
Here is the XAML:
And here is the code:
Note that if you don’t like the visual styling just update the TerminalCell DataTemplate. For example, the cursor could be a blinking rectangle instead of a solid one.
This code was fun to write. Hopefully it will be useful to you. It probably has a bug or two (or three) since I never actually executed it, but I expect those will be easily cleared up. I’d welcome an edit to this answer if you fix something.