The Matrix code in the terminal

A screensaver in Rust that shows the code of the Matrix in the terminal, with a font built from Rezmason's Matrix project.

Table of Contents

The Matrix films are my favorite films. When I was a bit under the weather recently, I took the chance to watch all of them again. After that I wanted the code from the operators’ screens in my terminal. cmatrix and unimatrix exist for exactly that, but neither looks like the film. What I missed was the real thing. I wanted to get as close as possible to what you see on screen.

While looking for it I came across Rezmason’s Matrix project, a recreation of the code in the browser. It also offers a free font with the characters of the classic code. That was the starting point for matrix-code, a screensaver for the terminal.

Columns of green characters with white cursors in a terminal

Why Rust

It was clear from the start that this would be a screensaver. It was not my first Rust project, and Rust suits a terminal program very well. The result is a single file that contains the font, and the Linux builds are statically linked, so they run on every distribution.

Light over fixed characters

The characters do not fall. Each column holds characters that stay in place, and trails of light move down over them. At the bottom of a trail sits a white cursor, and above it the colors fade to dark green. A cell outside every trail stays empty. Now and then a lit character turns into another one.

const CURSOR_COLOR: u8 = 231;
const TRAIL_COLORS: [u8; 7] = [157, 83, 46, 40, 34, 28, 22];

The numbers come from the 256-color palette, the colors the program expects from a terminal. Each column has a speed of its own, and all trails in a column share it, so none overtakes another. I matched the speed to Rezmason’s project.

The font

A terminal can only draw what a font contains, and ordinary fonts do not have the characters of the code in this form. The program therefore brings its own font and copies it into the user’s font folder. A Python script with fontTools builds it from Rezmason’s font and assigns the glyphs to code points in the Supplementary Private Use Area-B.

FIRST_CHARACTER = 0x100000
mapping = {FIRST_CHARACTER + index: name for index, name in enumerate(names)}

Common terminal fonts leave this area empty, so the terminal falls back to this font without any setting. The program leaves the cell to the right of each character empty, and the script scales the glyphs so that they grow into it. A terminal often only finds a new font after a restart. A first start without install therefore still shows half-width katakana and digits.

The screensaver

zsh provides what a screensaver needs. matrix-code screensaver 300 adds a block with these two lines to .zshrc.

TMOUT=300
TRAPALRM() { if (( TTYIDLE >= TMOUT )); then "/home/me/.local/bin/matrix-code"; fi; }

With TMOUT set, the shell receives an alarm signal when no command is entered at the prompt in time, and TRAPALRM runs. Typing at the prompt without pressing Enter does not stop that signal. The check of TTYIDLE, the seconds since the last key press, covers this case. A key press ends the animation and the prompt is back.

Drawing

Redrawing the whole screen for each frame is too slow for some terminals. The program remembers the last frame and only sends the cells that differ. It wraps them in a synchronized update, so terminals that support it show each frame at once.

Limits

A terminal is a grid of cells without any glow. The program uses the 256-color palette, so a trail fades in steps instead of smoothly. The screensaver needs zsh, because in bash TMOUT ends the shell instead. It only starts at the prompt, not while another program runs. The Linux text console cannot use the font and shows placeholders. The terminal of VS Code needs the font in its settings, and the Reworked terminal engine of JetBrains IDEs shifts the columns, for which there is an open bug report.

Conclusion

Despite these limits it looks incredibly good, and to me it is very close to the film. The glyphs come from Rezmason’s project, which took them from the promotional site of an official Matrix game and publishes them under the MIT License, like the program. The source is on GitHub.