Primitives
Five types make up the foundation. Everything else in Textagram builds on them.
Cell
A cell holds one character. Empty cells are spaces.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Cell {
pub glyph: char,
}impl Default for Cell {
fn default() -> Self {
Self { glyph: ' ' }
}
}Cursor
The cursor tracks position on the grid as (col, row). Column is x, row is y.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Cursor {
col: u16,
row: u16,
} pub fn new() -> Self { pub fn position(&self) -> (u16, u16) {Grid
The grid owns the canvas dimensions and the cursor. Movement clamps to bounds — the cursor never leaves the grid.
#[derive(Debug)]
pub struct Grid {
width: u16,
height: u16,
cursor: Cursor,
} pub fn new(width: u16, height: u16) -> Self { pub fn move_cursor_by(&mut self, dx: i16, dy: i16) -> bool {The clamping helper keeps arithmetic clean at the edges:
fn clamp_to_bounds(current: u16, delta: i16, limit: u16) -> u16 {Direction and DirectionSet
Four cardinal directions. Each direction maps to a bit in a bitmask, so sets of directions pack into a single byte.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub enum Direction {
North,
East,
South,
West,
} pub const fn bit(self) -> u8 { pub const fn opposite(self) -> Self {#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct DirectionSet {
pub mask: u8,
} pub fn contains(&self, direction: Direction) -> bool {Connectors use DirectionSet to decide which Unicode glyph to draw at each cell — a cell with North and East connections becomes ╰, one with all four becomes ┼.
LayerBuffer
The shared 2D storage. All three runtime layers — shapes, connectors, text — wrap a LayerBuffer.
#[derive(Clone, Debug)]
pub struct LayerBuffer {
width: u16,
height: u16,
cells: Vec<char>,
} pub fn get_cell(&self, col: u16, row: u16) -> Option<char> { pub fn set_cell(&mut self, col: u16, row: u16, glyph: char) -> Option<char> {set_cell returns Some(previous) only when the glyph actually changed. This drives dirty-tracking: if nothing changed, nothing redraws.