Skip to content

Documentation / @ripl/terminal

@ripl/terminal

Terminal rendering context for Ripl — draw the same 2D graphics and charts to a terminal as braille-character output with ANSI truecolor.

Installation

bash
npm install @ripl/terminal

Overview

@ripl/terminal implements Ripl's Context abstraction without any DOM. It rasterizes elements into a grid of Unicode braille dots (each character cell packs a 2×4 sub-pixel grid) and writes them to a runtime-agnostic TerminalOutput adapter — process.stdout in Node, or an xterm.js instance in the browser. Because every Ripl element renders through the shared Context API, scenes written for Canvas or SVG render unchanged in the terminal.

Optional logicalWidth/logicalHeight options let you author a scene in CSS-pixel coordinates; the context uniformly scales and letterboxes that space into the character grid so it renders proportionally in any terminal size.

Note: the terminal context is a rasterizer — pointer events/hit-testing, gradients, images, and transforms are not supported.

Usage

typescript
import {
    createContext,
} from '@ripl/terminal';

import {
    createCircle,
} from '@ripl/core';

// `output` implements { write, columns, rows, onResize? }
const context = createContext(output, {
    logicalWidth: 800,
    logicalHeight: 600,
});

createCircle({
    stroke: '#38bdf8',
    cx: 400,
    cy: 300,
    radius: 150,
}).render(context);

In Node, use @ripl/node to obtain a stdout-backed output adapter.

Exporting

typescript
const text = context.export().toString(); // plain braille art
const image = await context.export().toImage(); // ImageData (rasterized)
const url = context.export().toURL(); // PNG object URL (browser)

Documentation

Full documentation and interactive demos are available at ripl.run.

License

MIT

Classes

ClassDescription
BrailleRasterizerBraille-dot rasterizer. Each terminal cell encodes a 2×4 grid of sub-pixel dots via Unicode braille patterns (U+2800–U+28FF).
TerminalContextTerminal rendering context that rasterizes Ripl elements into character-based output via a TerminalOutput adapter.
TerminalPathTerminal path implementation that records drawing commands for later rasterization.

Interfaces

InterfaceDescription
RasterizerAbstract rasterizer interface for converting pixel data to terminal characters.
SerializeOptionsOptions controlling how a rasterizer serializes its grid to a string.
TerminalContextOptionsOptions for constructing a terminal rendering context.
TerminalOutputAbstract terminal output interface — runtime-agnostic.
TerminalPathCommandA recorded drawing command with its type and parameters.
VertexA 2D point in raster (pixel) space.

Type Aliases

Type AliasDescription
PixelCallbackCallback invoked for each pixel in a rasterization pass.
TerminalPathCommandTypeTypes of drawing commands recorded by a terminal path.

Variables

VariableDescription
ANSI_RESETANSI SGR reset sequence.
BRAILLE_CELL_HEIGHTEach braille cell is 2 pixels wide and 4 pixels tall.
BRAILLE_CELL_WIDTHEach braille cell is 2 pixels wide and 4 pixels tall.

Functions

FunctionDescription
colorToAnsiBgConverts a CSS color string to an ANSI truecolor background escape sequence. Returns empty string for invalid/transparent colors.
colorToAnsiFgConverts a CSS color string to an ANSI truecolor foreground escape sequence. Returns empty string for invalid/transparent colors.
createContextCreates a terminal rendering context bound to the given output adapter.
fillPolygonFills the interior of one or more closed contours using the even-odd rule. Each contour is a polyline (implicitly closed); interiors are determined per scanline from edge crossings, so concave shapes, circular segments, and annular sectors (holes) fill correctly.
flattenArcSamples an arc from startAngle to endAngle into a polyline of points (inclusive of both endpoints).
flattenCubicBezierSamples a cubic bezier curve into a polyline of points (inclusive of both endpoints).
flattenEllipseSamples a full ellipse outline into a closed polyline of points.
flattenQuadBezierSamples a quadratic bezier curve into a polyline of points (inclusive of both endpoints).
rasterizeArcRasterizes an arc from startAngle to endAngle at (cx,cy) with given radius by subdivision into line segments.
rasterizeCircleRasterizes a circle outline at (cx,cy) with the given radius using the midpoint algorithm.
rasterizeCubicBezierRasterizes a cubic bezier curve by adaptive subdivision into line segments.
rasterizeEllipseRasterizes an ellipse outline at (cx,cy) with the given radii using the midpoint algorithm.
rasterizeLineRasterizes a line segment from (x0,y0) to (x1,y1) using Bresenham's algorithm.
rasterizeQuadBezierRasterizes a quadratic bezier curve by adaptive subdivision into line segments.
rasterizeRectRasterizes a rectangle outline.