Skip to content

Instantly share code, notes, and snippets.

@guidoschmidt
Created January 11, 2024 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guidoschmidt/09fdb6cd12aac7e99bce0bbc9871a833 to your computer and use it in GitHub Desktop.
Save guidoschmidt/09fdb6cd12aac7e99bce0bbc9871a833 to your computer and use it in GitHub Desktop.
Animate 2D text buffers right in the terminal
fn printDefault(c: u8) void {
std.debug.print("{c}", .{ c });
}
fn animate(buffer: *[]u8, rows: u32, cols: u32, comptime print_fn: fn(u8) void) void {
for(0..rows) |dy| {
for(0..cols) |dx| {
const idx = dy * cols + dx;
const pipe_tile = buffer.*[idx];
std.debug.print("\x1B[{d};{d}H", .{ dy, dx });
print_fn(pipe_tile);
}
}
std.time.sleep(1000 * 1000 * 16);
}
@guidoschmidt
Copy link
Author

Allows animating text buffers as 2D maps right in the terminal. Use like this:

var row_count = 128;
var col_count = 128;
// ... allocate and fill text buffer to your liking
var text_buffer: []u8 = undefined;

while (true) {
  animate(&text_buffer, row_count, col_count, printDefault);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment