fence: a tiny chess board viewer
Ever since I saw Unicode had chess symbols, I wanted to make this. fence is a tiny program that prints a chess board to your terminal.
# About
Fence is a small (under 15 kilobytes) program that takes a string in Forsyth-Edwards Notation (FEN) and displays it in the terminal. It’s written in rust.
Download: https://codeberg.org/tiffany/fence/releases/tag/v1.0.0 Source code: https://codeberg.org/tiffany/fence
# Screenshot
# Just here for a copy-pasteable chess board?
a b c d e f g h 8 ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜ 7 ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ 6 5 4 3 2 ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙ 1 ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜ ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
The top Google search for this page is unicode chess board, so it feels important to include this.
# Binary size
When I got this program working for the first time, I noticed that the binary was over 350 kilobytes in size. This bothered me, because it’s such a small and simple program, it shouldn’t cost this much disk space.
After a bit of fiddling I ended up deciding to make the program
#![no_std] and remove dependencies on all crates except libc. That
got it down to only 8 kilobytes. It went up a little bit since then, as
I added more features, and is now sitting around 13 kilobytes.
This means that I couldn’t use any nice things like format!() or the
nu_ansi_term crate. Everything was from scratch, like I was writing C.
That was a kind of fun little challenge in itself. I never knew anything
about ANSI escape codes before this, I’d always just relied on some
other software to handle them for me.
This does mean the program contains several unsafe {} blocks (9 to be
exact), but that’s fine. It also means I’m kind of throwing away the
easy cross platform support you get by default in rust. That’s less
fine. I’ve only tested fence on linux.
# The Unicode bit
https://unicode.link/blocks/chess-symbols
There is a block in Unicode called "Chess Symbols" that contains all 6 chess pieces, in both colors. It also has Xiangqi pieces, plus chess pieces rotated to various angles.
Fence doesn’t actually use these by default. They look ugly in my
terminal. I used NerdFont symbols instead, as it apparently also has
chess piece icons. Unicode symbols can be used with the -u flag
though.
In 2-wide mode (the -2 flag) fence also uses two half block characters
(U+258C Left Half Block & U+2590 Right Half Block) in
order to visually center the pieces.
# Double-height lines
Apparently there is an obscure set of VT100 escape codes that can make double-width lines, and double-width double-height lines. I considered using these, but not a lot of terminals support them, my primary terminal (kitty) included. There are a few though, like Konsole and xterm, that support them. I might support this in a future update, but it’s not as interesting to me since it doesn’t work in my main terminal.
printf '\e#3big text\n\e#4big text\n' printf '\e#6wide text\n'
There’s also ways of displaying straight up images inside of terminals, such as sixel, kitty image protocol, and a few others. That’s way out of scope for this project though.