Tips for Building a Text-Based UI in Node

Over the past week or so, I’ve been working on building Straturgery, a text-based game, playable over telnet, written in node.js.

The game, being both text-based and only accesable over telnet has provided some interesting challenges. I can’t use ncurses (since telnet is text-only), and have to build my windows and interfaces by hand. Here are a few things I’ve learned.

Escape Codes

It’s a bit esoteric, but you find yourself using terminal escape codes often. It’s also the only way to do colors. Most of the documented VT-100 escape codes work and are outputted with the escape special character \x1b[. You can test for this character when charCodeAt() equals 27.

A very helpful escape is the clear screen code, \x1b[2J. Escapes are also used for printing unicode characters. For example, you can print one of the many helpful characters from Code Page 347, such as , with the escape \u2588. Escapes can also be used to position the cursor to make drawing the screen easier (more on this later).


Escape codes are also used for colors. Just about all terminals support 256 colors (though very few support blinking). To enter the 256 color pallete, use the escape \x1b[38;5 followed by the color number and m. x1b[38;5; is for the foreground and x1b[48;5; is for the background. More details are here.

You can do pretty cool stuff with just 256 colors. For example, to make a red gradient:

var gradient = "";
for(var i = 5; i > 0 ; i--) {
  var x = (i * 36) + 16;
  gradient += "\x1B[48;5;" + x + "m \x1B[0m";

To see the amazing stuff you can do with just colors and unicode characters, check out these explosions!

Hopefully anyone else who’s building any type of node-based telnet application.