Announcing `termbuffer` - immediate mode terminal apps


termbuffer (crate) - Immediate mode terminal apps.

I made a library on top of termion that allows you to draw in immediate mode and will issue the minimum number of characters* to update the terminal.

Immediate mode means that you draw the scene from scratch every time. It’s easier to work with because you don’t have to remember anything about the previous frame, but it can be slower than retained mode (where state is shared between frames). This library keeps a copy of the previous frame, so it can calculate what it has to do to update the screen for the next frame.

I’ve currently only tested it on alacritty, where I can easily get 60fps with a simple game. I’m interested in how it works with other terminals.

The basic strategy is

  1. Create an App.
  2. Per frame:
    1. When you want to update the screen, call App::draw. This will give you a guard-like object where you can do your drawing.
    2. Redraw your whole image (you can get the size of the screen from the guard)
    3. drop the guard - this will trigger an update.
    4. (optional) process input

See the readme for a more complete example.

This library would be useful for any time you want to make a command line app, and don’t want to have to worry about minimizing the work of the terminal.