grid
Utility module for creating and manipulating character grids.
import { grid } from 'graticule'Functions
create()
grid.create(width: number, height: number, fill?: string): CharGridCreate an empty grid. Default fill is ' ' (space).
write()
grid.write(grid: CharGrid, x: number, y: number, text: string): voidWrite a string at position (x, y). Out-of-bounds characters are silently clipped.
writeLines()
grid.writeLines(grid: CharGrid, x: number, y: number, lines: string[]): voidWrite multiple lines starting at (x, y).
overlay()
grid.overlay(target: CharGrid, source: CharGrid, x: number, y: number): voidOverlay source onto target at position (x, y). Characters from source overwrite target.
fillRect()
grid.fillRect(grid: CharGrid, x: number, y: number, w: number, h: number, char?: string): voidFill a rectangular region with a character (default: space).
toString()
grid.toString(grid: CharGrid): stringConvert grid to a single string (rows joined by newlines).
clone()
grid.clone(grid: CharGrid): CharGridDeep-copy a grid.
dimensions()
grid.dimensions(grid: CharGrid): { width: number; height: number }Get the width and height of a grid.
wordWrap()
grid.wordWrap(text: string, width: number): string[]Word-wrap text to fit within width characters. Returns an array of lines.
truncate()
grid.truncate(text: string, width: number): stringTruncate text with ... if it exceeds width.
center()
grid.center(text: string, width: number): stringCenter-pad text within width characters.
padLeft()
grid.padLeft(text: string, width: number): stringLeft-pad text to width characters.
padRight()
grid.padRight(text: string, width: number): stringRight-pad text to width characters.
collectOverlays()
grid.collectOverlays(
component: Component,
offsetX: number,
offsetY: number,
): { vectors, rects, texts, colors, clicks }Collect vector overlays from a component (and its children, if it's a layout container) with coordinate offsets applied. Used after rendering a component tree that contains LineGraph, BarGraph, or other components with overlays.
const layout = new Row([new LineGraph(series), sidebar])
grid.overlay(g, layout.render(w, h), x, y)
const overlays = grid.collectOverlays(layout, x, y)
return { grid: g, ...overlays }