💾 Archived View for gemini.ctrl-c.club › ~nttp › writing › ansi-escapes.md captured on 2024-08-18 at 20:36:24.
-=-=-=-=-=-=-
# Fun with ANSI escape codes 2024-05-15 I love text user interfaces. Even outside of tilde servers, they have a purpose. TUI apps are light, simple by necessity, and look just plain cool. They're also a lot easier to make than anything with a GUI, and there are at least two popular ways. While the [ncurses](https://invisible-island.net/ncurses/) library still has a place in the modern world, [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) may be better in some situations:
local reset = "\x1b[0m"
local bold = "\x1b[1m"
local reverse = "\x1b[7m"
print(bold .. reverse .. "Hello, world!" .. reset)
Rule #1: all these codes begin with ASCII character 27 (Escape), followed by an open square bracket, one or more numeric arguments, and a one-letter command. Actually it's more complicated, but we'll stick to the basics. Entering special characters as hex numbers seems to work in Python and JS too; in C you might have to use octal, i.e. "\033". Rule #2: there are lots and lots of codes – check out Wikipedia for more – but you can go far with just the basics. For example, clearing the screen and moving the cursor:
local cls = "\x1b[2J"
local function move(y, x)
io.write("\x1b[" .. y .. ";" .. x .. "H")
end
io.write(cls) move(3, 6) print "Hello again!"
But my favorite thing about terminals is that they can have colors. You don't even need a lot of them:
local black = 0
local yellow = 3
local blue = 4
local white = 7
local default = 9
The way they work is, you add 30 to get a code for the foreground color, and 40 for the background:
local function foreground(color)
io.write("\x1b[" .. (30 + color) .. "m")
end
local function background(color)
io.write("\x1b[" .. (40 + color) .. "m")
end
foreground(yellow) background(blue) print(bold .. "I'm pretty!" .. reset)
Note how using bold text makes the foreground color brighter, effectively giving you sixteen of them in total. Modern terminal emulators also support 256-color or even 24-bit modes. There are many more attributes, alternate character sets and advanced features. Two things you can't do just with escape codes are: