Skip to content

@jsvision/core / serialize

Function: serialize()

serialize(current, previous, options): string

Defined in: render/serialize.ts:76

Build the ANSI string that turns previous into current (a damage diff). When previous is null, or its dimensions differ from current (e.g. after a resize), the baseline is invalid and every cell is repainted.

Parameters

current

ScreenBuffer

The frame to display.

previous

ScreenBuffer | null

The last displayed frame, or null for a full first paint.

options

RenderOptions

Terminal capabilities plus an optional custom style encoder.

Returns

string

One coalesced ANSI string, empty when nothing changed.

Example

ts
import { ScreenBuffer, serialize, resolveCapabilities } from '@jsvision/core';
const caps = resolveCapabilities().profile;

let previous: ScreenBuffer | null = null;
const frame = new ScreenBuffer(40, 10, { fg: 'white', bg: 'black' });
frame.text(1, 1, 'Ready.', { fg: 'brightGreen', bg: 'black' });

// First paint: previous is null, so the whole frame is emitted.
process.stdout.write(serialize(frame, previous, { caps }));
previous = frame.clone();

// Next frame: only the cells you changed since the snapshot are re-emitted.
frame.text(1, 1, 'Done. ', { fg: 'brightGreen', bg: 'black' });
process.stdout.write(serialize(frame, previous, { caps }));