Skip to main content
๐Ÿง™โ€โ™‚๏ธ Kris Kowal ๐Ÿ˜ ๐Ÿ™ ๐Ÿง› ๐Ÿ“Œ ๐Ÿ“ฌ Book Store

Terminal Rivers

Updated November 4, 2026: I rewrote this article from my 2019 sketch to rivers that run, with a demo and the code as a ZIP. The original 2019 article is kept as it was published.

My mind tends to gravitate down to watersheds. Three times, in 2019 and 2020, I wrote my patrons about drawing one: a notebook sketch for showing rivers in a terminal, the tables of runes that turn water levels into line art, and, after a walk around a lake, a way to draw contour lines on tiles that never have to talk to each other. The first two are here, carried forward, seven years on, to rivers that run.

A design sketch #

A notebook open to two pages of pen sketches in blue ink, with a sliver of a thumb at the left edge. The left page has six small diagrams of crossing lines, each a corner of a grid where a vertical and a horizontal channel meet beside a square cell, with arrows showing water moving between them. They are labeled watershed, flowing rivers and runoff flooding, and below them is written: evaporation and condensation, percolation, precipitation. The right page is headed background rendering cells over a brace. Below it, a square of four cells, hatched in two directions, is crossed by a long vertical and a long horizontal line; a narrow channel of scribbled water runs along the middle row and down one column. A larger square of dashed cells is offset behind it. A brace under it is labeled simulation cell, and below that: foreground render text.
Figure 1. The sketch: simulation cells, hatched, laid over the terminal's render cells, dashed, offset by half a cell.

This weekendโ€™s design exercise was rendering a cellular watershed automaton in a terminal. The objective is an art piece that shows rivers with line art and plains between them with the background color. To do this, the simulation cells have their corners in the center of every other render cell. The simulation effectively treats every other row and column as a potential ridge or river course. The simulation would need erosion rules that take into account the difference in volume of each kind of cell.

I look forward to carving out some space to implement this!

Line art from water levels #

This afternoonโ€™s project is to take a watershed automaton and render its rivers as Unicode line art in a terminal. In the sketch above, I designed a way to render rivers over topography as two-cell terminal art, using two-by-two cells from a cellular automaton to model fields and rivers in alternating columns and rows.

In Hilbert World, I shared how to generate an animation from a cellular automaton that models a watershed: how to flow water down to the basins on a topography.

To scratch an itch, Iโ€™d like to recreate this art piece in a terminal. Every pair of terminal cells will correspond to an offset 2ร—2 grid of the cellular automaton: a corner, vertical and horizontal rivers, and a field. We will render the background color of each cell by blending the covered field cells. We will render the text as line art. To do this, we need to convert an image representing water levels into the corresponding Unicode lines.

โ•ด โ•ถ โ”€ โ•ท โ” โ”Œ โ”ฌ โ•ต โ”˜ โ”” โ”ด โ”‚ โ”ค โ”œ โ”ผ โ•ธ โ•พ โ”‘ โ”ญ โ”™ โ”ต โ”ฅ โ”ฝ โ•บ โ•ผ โ” โ”ฎ โ”• โ”ถ โ” โ”พ โ” โ”ฏ โ”ท โ”ฟ โ•ป โ”’ โ”Ž โ”ฐ โ•ฝ โ”ง โ”Ÿ โ• โ”“ โ”ฑ โ”ช โ•… โ” โ”ฒ โ”ข โ•† โ”ณ โ•ˆ โ•น โ”š โ”– โ”ธ โ•ฟ โ”ฆ โ”ž โ•€ โ”› โ”น โ”ฉ โ•ƒ โ”— โ”บ โ”ก โ•„ โ”ป โ•‡ โ”ƒ โ”จ โ”  โ•‚ โ”ซ โ•‰ โ”ฃ โ•Š โ•‹

Each rune captures whether there is a river heading north, south, east, or west. Interestingly, Unicode has variations for light and heavy lines. We can use this!

For each edge, we have three possible values: absent, light, or heavy. We can map these to 0โ€“25%, 25โ€“75%, and 75โ€“100% bands for water levels on those edges. So, we need to convert the water levels for rivers on the input image into two bits, sometimes called 25ยข for being 25% of a byte. Aside, donโ€™t believe everything a stranger tells you.

So we will have one byte of input for every render cell, of the form NnSsEeWw. So we need to map each of the 256 possible inputs to one of the corresponding 80 glyphs. Weโ€™ll just index one array to the offset into another, and presto: runes.

So, step one, I manually created a table that describes each of the Unicode line art glyphs in terms of how it renders its edges. A dash is an absent edge, a lowercase letter a light one, and a capital a heavy one, in the order north, south, east, west.

--------  
-------w โ•ด
-----e-- โ•ถ
-----e-w โ”€
---s---- โ•ท
---s---w โ”
---s-e-- โ”Œ
---s-e-w โ”ฌ

Those are the first eight of its 81 lines; the whole table is with the program and its output.

Then I took a turn that I wasnโ€™t expecting. Generating the corresponding tables, to map the 256 possible inputs to these possible 80 outputs just felt like a good fit for Perl, a language I havenโ€™t used in anger for a decade. For those just tuning in, Perl was How The Web Was Won. I wonโ€™t be using Perl at runtime, but itโ€™s a fine tool for generating Go.

Perl is something of an atrocity, but there are ways to make it readable.

Go is a different kind of atrocity. Go deliberately omits macros and generics popular among more learnรจd languages, so itโ€™s the kind of language where you find yourself writing code generators a lot.

The meat of my code generator is pretty opaque, but itโ€™s the weekend and Iโ€™m hacking. This is a pretty representative sample of what Hell looked like in the early 90โ€™s, but doing this kind of work in a high-magic language like Perl really gives the profession that arcane, high-fantasy feeling we all crave. All the more apropos that weโ€™re making a table of runes.

So now I have some Go code to feed the terminal. Onward!

For your leisure reading, here are the input, program and output in full, from the gist. Run as perl runes.pl < runes.txt, the program still writes that runes.go byte for byte, a table of all 256 inputs and the 81 runes, counting the blank. In May 2019 the tables moved into the Bร˜RK shopโ€™s Go repository as the package rrr, โ€œrunes for rivers and ridgesโ€. They are in the Terminal Rivers repository now, in rrr/ in its snapshot, unchanged, and they draw the rivers below.

Rivers that run #

Seven years later, I carved out that space. The rivers now run, in a few small Go programs around those same tables.

I took a simpler course than the sketch. Instead of the offset two-by-two grid, there is one cell of the automaton for each cell of the terminal. A river is the line from the middle of a cell to the edge it drains through, so the runes draw the whole drainage tree, with its forks in the middles of the cells. Each cellโ€™s rune shows two things: its own outflow, on its downhill edge, and the outflow of every neighbor that drains into it, on the edge where that water arrives. Two neighbors draw the same flow on the edge they share, so the lines always join. The fields between the rivers do not get cells of their own; the ground shows through as the background color, green through tan to snow.

The landscape grows from a seed: a few octaves of value noise, stretched twice as wide as it is tall, since a terminal cell is about twice as tall as it is wide. Raw noise has pits, hollows with no way out, where water would pool forever. So, before any rain falls, a priority flood fills them, rising from the edges of the map like water poured in from outside, until every cell has a way downhill and off the map.

Then it rains. Every tick, each cell gets one cellโ€™s worth of rain, keeps half of the water it holds, and passes the other half to its lowest neighbor. Once the rain settles, the water leaving a cell is all the rain that falls upstream of it, so the flow of a cell is the size of its catchment. Six cells of catchment make a stream, a light line, and thirty make a river, a heavy one. Absent, light and heavy: the three values for each edge that the runes were made for.

A landscape in a grid of small cells, colored by elevation: olive green lowlands, tan and brown slopes and pale snow on the peaks. Dark blue lakes lie in the valleys, each cell marked with a pale wavy line. Thin pale blue lines, all right angles, run down from the hills as streams and join into heavier lines; each stops at a lakeโ€™s shore and leaves again from its outlet.
Figure 2. Rivers in runes, 80 by 24 cells, after 120 ticks of rain: light lines for streams and heavy ones for rivers, over the groundโ€™s elevation, with lakes drawn as lakes.
The same frame, as plain runes
      โ•ถโ”€โ”€โ”€โ”  โ”Œโ”˜   โ”—โ”“    โ”Œโ”€โ”€โ”€โ”            โ•ถโ”ฌโ”€โ”€โ”š      โ”โ”โ”นโ•ด     โ”‚โ”Œโ”˜    โ”โ”โ•ƒโ”€โ•ด โ”žโ”€โ•ด   
          โ””โ”€โ‰ˆโ”˜     โ”—โ‰ˆโ‰ˆ  โ”‚   โ””โ”€โ•ด           โ”‚     โ•ถโ”€โ”€โ”€โ”š    โ”Œโ”€โ”€โ”€โ”˜โ”‚  โ•ถโ”ฒโ”โ”› โ•ต   โ””โ”    
  โ•ถโ”€โ”€โ”€โ”€โ”€โ”€โ”      โ‰ˆ  โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ”โ”ตโ”€โ”€โ”€โ•ด             โ”‚              โ•ต  โ•ถโ”€โ”˜   โ”ƒ     โ‰ˆ  โ””โ”€โ”  
       โ•ถโ”€โ”ดโ”     โ”‚   โ‰ˆโ‰ˆโ””โ”€โ•ด                โ‰ˆโ•ต                     โ‰ˆโ”โ”›          โ•ต  
      โ•ถโ”€โ”€โ”€โ”ถโ”ณโ”โ”โ”โ”โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆ                        โ‰ˆ               โ”โ‰ˆโ‰ˆโ‰ˆ              
     โ”Œโ”€โ”€โ•ผโ”โ”โ”› โ”Œโ”€โ”€โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ””โ•ด             โ•ท       โ‰ˆโ•ด                โ•ฟโ”‚                
     โ”‚       โ”‚  โ”‚                   โ”‚       โ”‚โ”Œโ•ด      โ•ถโ‰ˆโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜โ”‚   โ‰ˆโ‰ˆ           
     โ•ต       โ•ต  โ”‚                   โ”‚ โ•ถโ”€โ”โ”โ•พโ”€โ”ดโ”˜                โ”Œโ”˜  โ•ถโ”€โ‰ˆโ‰ˆโ‰ˆโ”        
                โ‰ˆโ‰ˆ       โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆ โ•ทโ•ทโ”‚   โ‰ˆโ” โ”€โ”€โ•ด               โ”Œโ”€โ”˜        โ””โ•ผโ”โ”โ”โ‰ˆโ‰ˆ  
โ•ด              โ‰ˆโ‰ˆโ‰ˆ     โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ”ทโ”ทโ‰ˆโ”โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ•ด                 โ•ต               โ‰ˆโ‰ˆโ”“ 
                       โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ”ƒ      โ”‚        โ•ท                            โ”ƒ 
                       โ””โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ”€โ”€โ‰ˆโ•ด โ””โ”€โ”€โ•ด     โ”‚              โ•ถโ”€โ”€โ”€โ”€โ•ผโ”โ‰ˆโ”ฏโ‰ˆโ”ฏโ”ฏโ”โ”โ”ปโ”ฏ
                        โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ”ฉ                โ”‚          โ•ท          โ”‚โ”‚ โ•ตโ•ต  โ”Œโ”˜
                         โ”ƒ   โ‰ˆโ‰ˆโ‰ˆโ‰ˆ              โ•ท โ”‚          โ”‚          โ”‚โ”‚     โ”‚ 
    โ•ท   โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆ            โ‰ˆ   โ•ตโ‰ˆโ‰ˆโ‰ˆโ”€โ•ด            โ”‚ โ”‚   โ•ท      โ”‚          โ”‚โ”‚    โ•ถโ”˜ 
    โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆ  โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ”โ”โ‰ˆโ‰ˆ                    โ”‚ โ”‚   โ”‚ โ•ท    โ”‚  โ•ท      โ•ถโ”˜โ•ต       
โ”โ”โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆ    โ•ต             โ‰ˆโ‰ˆโ‰ˆ โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆ                
        โ‰ˆโ”‚โ‰ˆโ‰ˆโ‰ˆ โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆ           โ‰ˆ     โ”‚โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ”Œโ•ด              
         โ‰ˆโ‰ˆ     โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆ                  โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆ               
โ”€โ”€โ”€โ•ด  โ”Œโ”€โ”€โ”˜   โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆ                      โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆ โ””โ”€โ”ฌโ•ด            
      โ•ต      โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆ             โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ”€โ”€โ”€โ”€โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆ          โ””โ•ด            
              โ”‚     โ””โ”                     โ‰ˆโ‰ˆโ‰ˆโ‰ˆโ‰ˆโ”›                     โ‰ˆ    โ•ถโ”€โ”€โ” 
              โ•ต   โ‰ˆ  โ•ต   โ”Œโ•ด  โ•ท              โ‰ˆโ‰ˆโ‰ˆ                        โ‰ˆ      โ””โ”ฎ
                         โ”‚   โ”œโ”€โ”€โ•ด   โ•ท      โ”โ”›                          โ”‚       โ•ต

A watershed that tilts #

The demo also has the watershed as a JavaScript module that draws on a canvas: rivers over contour lines, in the same runes. Drag it, or use the sliders, and the land tilts: the lakes fill again, the water finds new ways down, and the rivers reroute. Tilt is how far: it lifts one side of the map above the other, from level to 60% of the landโ€™s whole range of height. Raised side is which side goes up, as a compass bearing, 0ยฐ north and 90ยฐ east, and the water runs off toward the side across from it. It is tested to draw exactly what the Go program draws.

A watershed drawn in box-drawing runes on a grid of colored cells: blue rivers, light and heavy, run between green lowlands, olive and tan hills and pale peaks, over thin contour lines in the ground's own color, darker, and blue lakes marked with wavy lines, where no river runs.
Figure 3. A watershed in the runes of 2019: rivers over contour lines, and lakes as lakes. Tilt it, by dragging or with the sliders, and the lakes refill, the rivers reroute and the contours slide.

The demo has working versions of every drawing here, from the seed up, and much more about how each part works than fits in an article. Download it as terminal-rivers-5799e64.zip (3.0 MB, a snapshot of commit 5799e64), and read docs/runes.md, docs/rivers.md and docs/figure.md inside it on the runes, the rivers and the figure.

Contours that tile #

After the walk around a lake in 2020, I sketched a way to draw contour lines on tiles that never have to talk to each other. That works now too, and it has grown into an article of its own, Marching Topography.

I thought I might yet revisit rendering a watershed with random, drifting tectonic plates. I ended up doing this in Go as well, with my World in a Bottle, an endeavor that keeps paying dividends. I imagined this might make a nice foundation for a Roguelike game, where one must carefully follow a gradual contour between cliff walls and slippery slopes. One might call it: โ€œA short trip, a long fallโ€. More likely, I will find myriad uses for procedurally generated settings. Or rather, a variety of clockwork worlds.


First published on Patreon in two posts, January 28 and February 2, 2019, and rewritten in November 2026. The 2019 version, with a third post of August 31, 2020, is kept as it was published.