Build the Gibson in Your Terminal Build the Gibson in Your Terminal

Build the Gibson in Your Terminal

Hackers (1995) is one of my favorite films, and its Gibson, the supercomputer that looks like a city of glass towers, still fascinates me. In 2024 I built it as a CSS demo.

Recently I started to write visualizers for cliamp, a music player for the terminal in the spirit of Winamp. The first one was Wormhole, a wireframe tunnel. I was happy with the result and wanted to build something harder. That’s when my CSS demo came to mind, and the Gibson became my second plugin.

I posted a clip of it on X, and it got much more attention than I expected. That’s why I wrote this article. I show you how to build a small version of the plugin in six steps. It uses the same techniques and most of the same values. The result is a single Lua file with fewer than 300 lines, and you can run it after each step.

I only show the interesting parts of the code. The line numbers are those of the complete files, and the title of each snippet links to its place in the repo. Every step also runs on this page, without sound, if you press Live above its picture.

Demo & Source Code

If you just want to try it out (you need cliamp for it, see step 1):

cliamp plugins install konstantindenerz/cliamp-plugin-hack-the-gibson

From Perspex to CSS to Braille

The city in the film was built for real. Director Iain Softley told Collider that the towers were made of Perspex (acrylic glass), filmed with motion control and lit from underneath. In another interview he says they shot it “almost like animation” and changed the text on the cels.

In my CSS demo each tower consists of six HTML elements, one for each face:

I position the faces with translateZ and rotateY, set a perspective, and the browser takes care of the projection and the transparency. In a terminal I don’t get any of this. There is only a grid of cells, and each cell has a character, a foreground color and a background color. So I had to write the perspective myself and find a replacement for the glass.

Step 1: Ten Numbers per Frame

To follow along you need cliamp and a terminal with 24-bit color. Ghostty draws the Braille characters itself. If your terminal doesn’t, your font has to contain them. I use macOS, so for other platforms please take a look at the README of cliamp.

brew install bjarneo/cliamp/cliamp
git clone https://github.com/konstantindenerz/article-cliamp-visualizer
cd article-cliamp-visualizer

You need v2.5.0 or newer for the kicks in step 6. cliamp --version shows what you have.

A visualizer plugin is a Lua file with a render function. cliamp runs it in gopher-lua, a Lua 5.1 VM. While music plays, it calls render about 20 times per second and displays the string it returns:

1-hello.lua:3
 3local p = plugin.register({
 4  name = "Mini Gibson",
 5  type = "visualizer",
 6  version = "0.1.0",
 7  description = "A small flight through the Gibson",
 8})
 9
10function p:render(bands, frame, rows, cols)
11  local lines = {}
12  for row = 1, rows do
13    local band = bands[(row - 1) % 10 + 1]
14    lines[row] = string.rep("#", math.floor(band * cols))
15  end
16  return table.concat(lines, "\n")
17end

The parameter bands contains ten values between 0 and 1, from 20 Hz up to 20 kHz. cliamp has already converted them to a dB scale and smoothed them. A plugin never sees the samples themselves. frame is a counter, rows and cols are the size of the panel in cells.

If you come from JavaScript or Python, these will look odd:

You don’t have to install Lua. It’s part of cliamp.

Now copy the file and trust it:

mkdir -p ~/.config/cliamp/plugins
cp 1-hello.lua ~/.config/cliamp/plugins/mini-gibson.lua
cliamp plugins trust mini-gibson

trust shows the hash of the file and asks you once. If it worked, it prints nothing more. cliamp runs a plugin only if you approved exactly this content, and it loads plugins only when it starts. So after every edit you have to call trust again and restart the player. The following steps work the same way. Each one is a complete file that replaces mini-gibson.lua, and -y skips the question:

cp 2-braille.lua ~/.config/cliamp/plugins/mini-gibson.lua && cliamp plugins trust -y mini-gibson

To see what a step adds, run git diff --no-index 4-city.lua 5-glass.lua.

Start cliamp with some music, for example with cliamp ~/Music, and press Space to play it. Press V for full screen and then v until the line at the bottom says Mode:Mini Gibson. The normal view doesn’t show the name of a visualizer. If Mini Gibson is not in the list, quit cliamp and look at the terminal. A missing trust is reported there when cliamp starts, in a line that begins with lua plugins:. A syntax error already shows up when you call trust. Errors that happen later in render are written to ~/.config/cliamp/plugins.log, and so is everything you print. Of these errors cliamp logs only the first one until you restart it.

Every row is one band, the lowest at the top, and after ten rows the pattern starts again:

1-hello.lua
#########################################
###################################
########################
################
################

##
###
####
####
#########################################
###################################
########################
################

Step 2: Eight Pixels per Cell

Unicode contains all 256 Braille patterns, from U+2800 to U+28FF. A pattern has two columns with four dots each, and every dot stands for one bit. The sum of the bits is added to U+2800. Try it out:

1 · 8
2 · 16
4 · 32
64 · 128

The values run down the left column (1, 2, 4) and then down the right one (8, 16, 32). These are the six dots of the original Braille. The bottom row was added later and got the next two values, 64 and 128. DOT lists them row by row. That’s why they look mixed up in the code below.

Lua 5.1 has no functions for Unicode. cliamp adds a utf8.char for exactly this case, but I didn’t want a function call per cell, so I create the three UTF-8 bytes of all patterns once. The first byte is always the same, the two dots of the bottom row end up in the second byte and the other six in the third. floor is math.floor. The files keep such functions in locals at the top:

2-braille.lua:14
14-- Bit of each dot in a cell, row by row.
15local DOT = { 1, 8, 2, 16, 4, 32, 64, 128 }
16
17-- The 256 Braille characters in UTF-8, by their dots.
18local GLYPH = {}
19for mask = 0, 255 do
20  GLYPH[mask] = string.char(0xE2, 0xA0 + floor(mask / 64), 0x80 + mask % 64)
21end

The canvas dots is an array with one number per cell. rows and cols are the size of the panel, w and h its size in dots, so two per column and four per row. To set a dot I look up its cell and add the bit, unless it’s already there. Lua 5.1 has no bit operators either, so I check this with a modulo:

2-braille.lua:34
34local function set(x, y)
35  if x < 0 or x >= w or y < 0 or y >= h then
36    return
37  end
38  local c = floor(y / 4) * cols + floor(x / 2) + 1
39  local bit = DOT[y % 4 * 2 + x % 2 + 1]
40  if dots[c] % (2 * bit) < bit then
41    dots[c] = dots[c] + bit
42  end
43end

With dots[c] = 5 and bit = 4, 5 % 8 is 5 and not less than 4, so the dot is already set.

A cell is about twice as high as it is wide, so the dots are almost square. In full screen my terminal has 140 × 40 cells, which gives me 280 × 160 dots. The function encode turns the numbers into characters, out holds one row at a time:

2-braille.lua:45
45local function encode()
46  local lines = {}
47  for row = 0, rows - 1 do
48    for col = 1, cols do
49      out[col] = GLYPH[dots[row * cols + col]]
50    end
51    lines[row + 1] = table.concat(out, "", 1, cols)
52  end
53  return table.concat(lines, "\n")
54end

As a first test, 2-braille.lua draws a sine wave with a different amplitude for each band, so it jumps where two bands meet:

2-braille.lua
A sine wave of Braille dots A sine wave of Braille dots
A sine wave of Braille dots

From the next step on you need the full screen. The normal panel is only seven rows high.

Step 3: A Camera

In the world x points to the right, y up and z along the street. A tower is one unit wide. For the perspective I divide x and y by the distance z. Something that is twice as far away is drawn half as far from the center. Here is the camera from the side:

eye screen point focal · (y − EYE) / z EYE y − EYE focal z

project does this for x and y:

3-camera.lua:58
58local NEAR = 0.12 -- nothing closer than this is drawn
59local EYE = 0.9 -- height of the camera
60local SPEED = 0.15 -- distance per frame
61
62local ez, focal -- camera position along the street, focal length in dots
63
64-- From the world to the screen. z is the distance from the camera.
65local function project(x, y, z)
66  return w / 2 + focal * x / z, h / 2 - focal * (y - EYE) / z
67end

w / 2 and h / 2 are the center of the panel, and the minus after h / 2 is there because y grows downwards on the screen. focal is the zoom, and render sets it to 1.4 times the height of the panel, but between 30% and 60% of its width. My camera only flies straight ahead, so I subtract its position ez from z and don’t need a rotation or a matrix in this version.

Everything is drawn with lines in 3D, and there are two cases I had to take care of:

line draws one line between two points in the world and handles both cases. On the screen it runs from a to b, and every point of it is a + t · (b − a), so t is 0 at a and 1 at b. t0 and t1 are the ends of the part we can see:

3-camera.lua:80
 80local function line(x0, y0, z0, x1, y1, z1)
 81  z0, z1 = z0 - ez, z1 - ez
 82  if z0 > z1 then
 83    x0, y0, z0, x1, y1, z1 = x1, y1, z1, x0, y0, z0
 84  end
 85  if z1 < NEAR then
 86    return -- behind the camera
 87  elseif z0 < NEAR then
 88    local t = (NEAR - z0) / (z1 - z0)
 89    x0, y0, z0 = x0 + (x1 - x0) * t, y0 + (y1 - y0) * t, NEAR
 90  end
 91  local ax, ay = project(x0, y0, z0)
 92  local bx, by = project(x1, y1, z1)
 93  local dx, dy = bx - ax, by - ay
 94  local t0, t1 = clip(-dx, ax, 0, 1) -- left
 95  t0, t1 = clip(dx, w - 1 - ax, t0, t1) -- right
 96  t0, t1 = clip(-dy, ay, t0, t1) -- top
 97  t0, t1 = clip(dy, h - 1 - ay, t0, t1) -- bottom
 98  if t0 > t1 then
 99    return -- outside of the panel
100  end
101  ax, ay, dx, dy = ax + dx * t0, ay + dy * t0, dx * (t1 - t0), dy * (t1 - t0)
102  local steps = floor(max(abs(dx), abs(dy))) + 1
103  for i = 0, steps do
104    set(floor(ax + dx * i / steps), floor(ay + dy * i / steps))
105  end
106end

The swap at the top puts the nearer point first, so only this one has to be cut. The loop at the end goes along the line in steps of at most one dot.

clip takes care of one edge, and every call can only shorten what the one before has left:

3-camera.lua:69
69-- Narrows the part t0..t1 of a line to one side of an edge. d is how far the
70-- line moves towards the edge, q how far away the edge is at its start.
71local function clip(d, q, t0, t1)
72  if d < 0 then
73    return max(t0, q / d), t1
74  elseif d > 0 then
75    return t0, min(t1, q / d)
76  end
77  return t0, q < 0 and -1 or t1
78end
right edge: t1 = q / d panel a, t = 0 b, t = 1 t0 t1 q d
Drag a and b

The left edge works the same in the other direction. There d is -dx, and q is ax, the way from the left edge to a. A line that runs parallel to an edge has a d of 0. If it’s outside, clip returns a t1 of -1, which is below every t0, and line gives up.

The position of the camera is frame * SPEED. cliamp doesn’t increase the frame counter while the player is paused, so the flight stops together with the music. This is how 3-camera.lua looks, with a grid on the ground and one tower:

3-camera.lua
A grid on the ground and the outline of a single tower A grid on the ground and the outline of a single tower
A grid on the ground and the outline of a single tower

Step 4: An Endless City

The city should never end, and I didn’t want to create towers in advance or clean them up afterwards. That’s why render visits only 56 lots, seven rows with eight lots each, beginning with the row the camera is in:

4-city.lua:163
163function p:render(_, frame, new_rows, new_cols)
164  clear(new_rows, new_cols)
165  ez = frame * SPEED
166  focal = max(0.3 * w, min(0.6 * w, 1.4 * h))
167  ground()
168  local here = floor(ez / BLOCK)
169  for j = here, here + 6 do
170    for i = -4, 3 do
171      tower(i, j)
172    end
173  end
174  return encode()
175end

clear empties the canvas and keeps the new size. That’s why the parameters are called new_rows and new_cols. The first one is _ because this step doesn’t use the bands. BLOCK is the distance from one tower to the next. When the camera reaches the next row, here is increased by one. The row behind us isn’t visited anymore, and the loop reaches one row farther. Each lot has an index across the street (i) and one along the street (j). Here are the lots from above:

street i = −4 −3 −2 −1 0 1 2 3 j = here + 6 j = here empty camera BLOCK

I calculate everything else from a hash of i and j. You may know the formula from shaders. Its numbers are arbitrary. They only have to mix well. The sine is multiplied by a large number and only the fraction is kept, so two neighbors get values that have nothing to do with each other:

4-city.lua:127
127-- Stable pseudo-random number in [0, 1) for item i. slot selects the stream.
128local function hash(i, slot)
129  local x = sin(i * 12.9898 + slot * 78.233) * 43758.5453
130  return x - floor(x)
131end

And this is how a lot becomes a tower:

4-city.lua:147
147-- The tower of lot (i, j). Lots 0 and -1 are next to the street.
148local function tower(i, j)
149  local x, z = (i + 0.5) * BLOCK, j * BLOCK
150  local dist = z - ez
151  if dist < -HALF or dist > VIEW then
152    return
153  end
154  local id = i * 127.1 + j * 311.7
155  if hash(id, 1) < 0.1 then
156    return -- an empty lot
157  end
158  box(x, z, TALL * (0.7 + 0.9 * hash(id, 2)))
159end

tower skips a lot when the whole tower is behind us (HALF is half its width) or farther away than VIEW. id is one number for the lot, made from i and j. A lot always gets the same values, so a tower doesn’t change while we fly past it, although it isn’t stored anywhere. The first hash (slot 1) leaves one lot in ten empty. The second one sets the height, between 0.7 and 1.6 times TALL, which is 2.5.

The ground works in a similar way. The lines along the street are attached to the camera, and only the lines across the street have a fixed position in the world, which is enough to see the movement.

A box has twelve edges, and I draw seven of them: four for the front and three for the side that faces the street. With so few dots I couldn’t tell the other five apart anyway. The result is 4-city.lua. If you watch the end of the street, you will notice that new towers suddenly pop up:

4-city.lua
The outlines of many towers along a street

Step 5: Fog and Glass

To hide the towers that pop up at the end of the street, I added fog. It also makes it easier to see which tower is in front. A color is an escape sequence in the string that render returns. It starts with \27, the Escape character in decimal. There are four shades of the tower color, and the farther away a tower is, the closer its shade is to the background of the terminal:

5-glass.lua:17
17-- Escape sequence for a color, mixed from the background. kind is 38 for the
18-- foreground and 48 for the background of a cell.
19local function shade(kind, color, share)
20  local c = {}
21  for i = 1, 3 do
22    c[i] = floor(BACK[i] + (color[i] - BACK[i]) * share + 0.5)
23  end
24  return string.format("\27[%d;2;%d;%d;%dm", kind, c[1], c[2], c[3])
25end

All shades are mixed from BACK. The files have my background, #0f1320 as { 15, 19, 32 }, so put yours there before you run this step. Otherwise the fog fades into the wrong color and thin glass is darker than the cells without glass. Ghostty shows its color with ghostty +show-config | grep '^background ='. If that prints nothing, you have the default, #282c34. Other terminals have it in their theme or settings.

The farthest shade still has 22% of the tower color, so a new tower appears faintly instead of out of nowhere. If several towers share a cell, the nearest shade wins. For this set stores a tier per cell, 1 for the ground and 2 to 5 for the towers from far to near, and keeps the highest. That’s the tier on every line in the diff.

For the glass I use the only thing that is left, the background color of the cells. Every wall adds a value to the haze of the cells it covers. The walls are upright and the camera is level, so their sides stay vertical on the screen. Every column of a wall has exactly one top and one bottom, and I can fill it column by column. pane does this for one wall. box calls it for the front and the side of a tower, and amount is the tint, which gets weaker with the fog. a and b are the two ends of the wall on the screen, each with an x, a top and a foot:

a b top foot col · 2 + 1 − ax bx − ax
5-glass.lua:166
166  for col = max(0, floor(ax / 2 + 0.5)), min(cols - 1, floor(bx / 2 - 0.5)) do
167    local t = (col * 2 + 1 - ax) / (bx - ax)
168    local top, foot = atop + (btop - atop) * t, afoot + (bfoot - afoot) * t
169    for row = max(0, floor(top / 4 + 0.5)), min(rows - 1, floor(foot / 4 - 0.5)) do
170      local c = row * cols + col + 1
171      haze[c] = haze[c] + amount
172    end
173  end

col * 2 + 1 is the center of a column in dots. A cell gets the tint if its center is inside the wall. That’s what the 0.5 is for. A sum per cell is all I have to store, and since every pane has the same tint, I don’t have to sort anything by depth. Towers don’t have to hide each other either, because they are transparent.

In the end the sum is capped at 1 and becomes one of 16 shades between the background and half of PANE, the color of the glass. A cell without glass keeps the default background of the terminal:

5-glass.lua:35
35-- The glass, from none to dense.
36local LEVELS = 16
37local GLASS = { [0] = "\27[49m" }
38for level = 1, LEVELS do
39  GLASS[level] = shade(48, PANE, 0.5 * level / LEVELS)
40end

encode writes an escape sequence only if the color changes between two cells. The eight dots of a cell share one color, so if a bright tower and the grid on the ground share a cell, the dots of the grid become bright as well. This is 5-glass.lua:

5-glass.lua
Glass towers with bright edges Glass towers with bright edges
Glass towers with bright edges

The glass has only the resolution of the cells. That’s why it doesn’t match the edges everywhere.

Step 6: Listen to the Kick

The level of the low bands is not enough to detect a kick (the bass drum), because a bass line can keep it high the whole time. So I compare the three lowest bands (20 to 400 Hz) with the previous frame and sum up only the increase. This is a spectral flux, limited to the low bands:

6-beat.lua:187
187-- A kick is a sudden rise of the three lowest bands.
188local function listen(bands, frame)
189  if frame == heard then
190    return
191  end
192  heard = frame
193  local rise = 0
194  for i = 1, 10 do
195    if i <= 3 and level[i] then
196      rise = rise + max(0, bands[i] - level[i])
197    end
198    level[i] = bands[i]
199  end
200  thump = thump * 0.67
201  since = since + 1
202  if rise > RISE and since >= GAP then
203    thump, since = 1, 0
204  end
205end

The check at the top is there because cliamp also calls render after a key press, with the same frame as before. Without the check thump would fade with every key you press.

RISE is 0.08. cliamp maps 50 dB to the values from 0 to 1, so this is a rise of 4 dB, summed over the three bands. A quiet track needs the same rise as a loud one, as long as its bands stay above 0. cliamp lowers them with its own volume, so turn down your speakers and not the player. Because of the smoothing a kick can rise for several frames, so the detection pauses for GAP frames after each hit. since counts them.

Here is the detection on four seconds of a generated house track. Drag RISE and GAP and watch the threshold and the pause:

GAP counts frames, like SPEED and the fading of thump. At 20 frames per second it is 250 ms.

A snare with a lot of body lifts the same three bands. In a rock track the city also flashes on the snare.

thump is 1 on a kick and loses a third with every frame. I use it to make the towers a bit taller and the glass denser, to light up the nearest edges and to zoom in by 6%. Each tower also follows one of the ten bands, picked by a third hash. That’s why listen keeps all ten in level:

6-beat.lua:260
260  local band = level[1 + floor(hash(id, 3) * 10)]
261  local tall = TALL * (0.7 + 0.9 * hash(id, 2)) * (1 + 0.25 * band + 0.1 * thump)
6-beat.lua
The finished Mini Gibson, with sound

That’s the complete plugin (6-beat.lua). On my Mac (M4 Max) a frame with 140 × 40 cells takes about 6 ms in gopher-lua. If a frame takes longer than 50 ms, cliamp shows the previous one again.

If the flashes don’t match the kicks, check cliamp --version. Up to v2.4.1 cliamp handed the audio to a plugin in blocks, on my Mac 125 to 250 ms before I heard it. The analysis never saw some of the kicks, and the others flashed early. I fixed this in cliamp, and v2.5.0 has it. Since then cliamp passes the sound that plays, and a flash follows its kick after about 40 ms. Hack the Gibson closes this gap by predicting the beat.

What the Real One Does

Hack the Gibson has 1,728 lines, including the themes and options. If you want to read the code, I would start here:

Your Turn

If you want to fly through something else, you only have to change tower and box. Wormhole and Hack the Gibson are listed in the community plugins of cliamp, and you can add your own visualizer there. And if you read the source of Hack the Gibson, take a look at the end of the file. Hack the planet!