yog/render/ascii

ASCII art rendering for grids and mazes.

Renders grid structures as text using simple ASCII characters (+, -, |) or Unicode box-drawing characters (┌, ─, ┼, │). Perfect for terminal output and following along with “Mazes for Programmers” book examples.

Quick Start

import yog/render/ascii
import yog/builder/grid

 let map = [
   [">", ">", "."],
   [".", "V", ">"],
   [".", ".", "."],
 ]
 let maze =
   grid.from_2d_list(
     map,
     model.Directed,
     can_move: grid.including([">", "<", "V", "^"]),
   )

io.println(ascii.grid_to_string(maze))

Output

+---+---+---+
|       |   |
+---+   +---+
|   |       |
+---+---+---+
|   |   |   |
+---+---+---+

Unicode Rendering

For a more polished look, use grid_to_string_unicode:

io.println(ascii.grid_to_string_unicode(maze))
// Output:
// ┌───┬───┬───┐
// │   │   │   │
// ├───┼   ├───┤
// │   │   │   │
// └───┴───┴───┘

Displaying Cell Contents

You can show values inside cells using occupants:

let occupants = dict.from_list([
  #(0, "S"),  // Start
  #(8, "G"),  // Goal
])
io.println(ascii.grid_to_string_with_occupants(maze, occupants))

Values

pub fn grid_to_string(grid: grid.Grid(n, e)) -> String

Converts a grid to ASCII art using simple characters (+, -, |).

Each cell is represented as a 3-character wide space. Walls are drawn where edges don’t exist between adjacent cells.

Example

let maze = // ... create grid
io.println(ascii.grid_to_string(maze))
pub fn grid_to_string_unicode(grid: grid.Grid(n, e)) -> String

Converts a grid to Unicode box-drawing art.

Uses Unicode characters like ┌, ─, ┬, ┼, │ for a more polished look. Each intersection is drawn correctly based on surrounding walls.

Example

io.println(ascii.grid_to_string_unicode(maze))
// Output:
// ┌───┬───┬───┐
// │   │   │   │
// ├───┼   ├───┤
// │   │   │   │
// └───┴───┴───┘
pub fn grid_to_string_unicode_with_occupants(
  grid: grid.Grid(n, e),
  occupants: dict.Dict(Int, String),
) -> String

Converts a grid to Unicode box-drawing art with cell contents.

Example

let occupants = dict.from_list([#(0, "S"), #(8, "G")])
io.println(ascii.grid_to_string_unicode_with_occupants(maze, occupants))
pub fn grid_to_string_with_occupants(
  grid: grid.Grid(n, e),
  occupants: dict.Dict(Int, String),
) -> String

Converts a grid to ASCII art with cell contents displayed.

The occupants dictionary maps node IDs to single-character strings that will be displayed inside each cell.

Example

let occupants = dict.from_list([#(0, "S"), #(8, "G")])
io.println(ascii.grid_to_string_with_occupants(maze, occupants))
pub fn has_edge(
  graph: model.Graph(n, e),
  from: Int,
  to: Int,
) -> Bool

Checks if an edge exists from one node to another.

pub fn has_passage(
  graph: model.Graph(n, e),
  from: Int,
  to: Int,
) -> Bool

Checks if there’s a passage (edge) between two cells.

A passage exists if there’s an edge in either direction (since mazes can be directed or undirected).

pub fn toroidal_to_string(
  toroidal_grid: toroidal.ToroidalGrid(n, e),
) -> String

Converts a toroidal grid to ASCII art.

Adds arrow hints around the border to indicate wrap-around connections.

pub fn toroidal_to_string_unicode(
  toroidal_grid: toroidal.ToroidalGrid(n, e),
) -> String

Converts a toroidal grid to Unicode box-drawing art.

pub fn toroidal_to_string_unicode_with_occupants(
  toroidal_grid: toroidal.ToroidalGrid(n, e),
  occupants: dict.Dict(Int, String),
) -> String

Converts a toroidal grid to Unicode art with cell contents.

pub fn toroidal_to_string_with_occupants(
  toroidal_grid: toroidal.ToroidalGrid(n, e),
  occupants: dict.Dict(Int, String),
) -> String

Converts a toroidal grid to ASCII art with cell contents.

Search Document