paratext
API reference

paratext

Every export of paratext, with its signature and doc comment: _default, ansiEscapesFor, beep, beginSynchronizedOutput, clearScreen, clearTerminal and 54 more, plus 7 types.

paratext — everything around your terminal output that is not the output.

import _default from 'paratext';
import { ansiEscapesFor, beep, beginSynchronizedOutput, … } from 'paratext';

Functions

ansiEscapesFor

The surface bound to a runtime you supply.

function ansiEscapesFor(runtime: Runtime): AnsiEscapes;
ParameterType
runtimeRuntime

Returns AnsiEscapes

check

Everything wrong with a plugin document — the check rule 7 asks for, usable before registering and by a command that validates a file.

function check(candidate: object): string[];
ParameterType
candidateobject

Returns string[]

emit

Emit name for fields: the sequence when the terminal understands it, the static projection when it does not, and for an unknown name whatever text the caller supplied — never a throw, because output is not the place to discover a typo at three in the morning.

function emit(runtime: Runtime, name: string, fields?: Fields): string;
ParameterType
runtimeRuntime
namestring
fields (optional)Fields

Returns string

fieldsUsed

Every field a template reads, so check can say what a capability needs.

function fieldsUsed(template: string): string[];
ParameterType
templatestring

Returns string[]

register

Add a capability, or replace one by name — replacing is deliberate, so a caller whose terminal we mis-detect can correct the guess without patching the package.

function register(capability: Capability): void;
ParameterType
capabilityCapability

Returns void

registerBuiltins

Registered through the public call, so the built-ins prove the extension surface works.

function registerBuiltins(): void;

Returns void

render

Render a template against a caller's fields.

function render(template: string, fields: Fields): string;
ParameterType
templatestring
fieldsFields

Returns string

supports

Whether this runtime is believed to understand capability.

function supports(runtime: Runtime, capability: {
    readonly when: Support;
}): boolean;
ParameterType
runtimeRuntime
capability{ readonly when: Support; }

Returns boolean

Classes

CapabilityError

Thrown rather than returned: a malformed capability is a programming error at start-up.

class CapabilityError extends Error {
    readonly code: CapabilityErrorCode;
    constructor(code: CapabilityErrorCode, message: string);
}

Constants

default

The default export, declared as _default.

The default export, carrying what this package implements: the CSI half and the four OSC members. iTerm and ConEmu stay off it — a key present with an undefined value would read as a claim made and not kept. Frozen, because the incumbent's own suite asserts that a named export and the member are the same object and a caller reassigning one would make that quietly untrue.

const _default: Readonly<{
    beep: "\u0007";
    image: (data: Uint8Array | string, options?: ImageOptions) => string;
    link: (text: string, url: string) => string;
    setCwd: (cwd?: string) => string;
    cursorTo: (x: number, y?: number) => string;
    cursorMove: (x: number, y?: number) => string;
    cursorUp: (count?: number) => string;
    cursorDown: (count?: number) => string;
    cursorForward: (count?: number) => string;
    cursorBackward: (count?: number) => string;
    cursorLeft: "\u001B[G";
    cursorSavePosition: "\u001B7" | "\u001B[s";
    cursorRestorePosition: "\u001B8" | "\u001B[u";
    cursorGetPosition: "\u001B[6n";
    cursorNextLine: "\u001B[E";
    cursorPrevLine: "\u001B[F";
    cursorHide: "\u001B[?25l";
    cursorShow: "\u001B[?25h";
    eraseEndLine: "\u001B[K";
    eraseStartLine: "\u001B[1K";
    eraseLine: "\u001B[2K";
    eraseDown: "\u001B[J";
    eraseUp: "\u001B[1J";
    eraseScreen: "\u001B[2J";
    scrollUp: "\u001B[S";
    scrollDown: "\u001B[T";
    eraseLines: (count: number) => string;
    clearScreen: "\u001Bc";
    clearViewport: "\u001B[2J\u001B[H";
    clearTerminal: "\u001B[2J\u001B[3J\u001B[H";
    enterAlternativeScreen: "\u001B[?1049h";
    exitAlternativeScreen: "\u001B[?1049l";
    beginSynchronizedOutput: "\u001B[?2026h";
    endSynchronizedOutput: "\u001B[?2026l";
    synchronizedOutput: (text: string) => string;
}>;

beep

The bell, byte-identical to ansi-escapes' beep.

const beep = "\u0007";

bell

BEL — the oldest one, and the only member of this layer that is not an OSC sequence.

const bell: Capability;

builtins

Every capability this package ships, in one list a reader can check against the registry.

const builtins: readonly Capability[];

capabilities

Every registered name, sorted — so --json and a check command can enumerate them.

const capabilities: () => string[];

capability

One capability by name, for callers that want to inspect before they emit.

const capability: (name: string) => Capability | undefined;

clipboard

OSC 52 — put text on the user's clipboard through the terminal, which is the only way that works over ssh. clipboardy shells out to pbcopy and cannot.

const clipboard: Capability;

ConEmu

const ConEmu: NotImplemented;

cwd

OSC 50 and 9;9 — tell the emulator where we are, so a new tab opens here.

const cwd: Capability;

DEPRECATED

The prefix on a line check returns that does not refuse the document.

const DEPRECATED = "deprecated: ";

image

OSC 1337 — image(data, options), projecting to options.caption.

const image: (data: Uint8Array | string, options?: ImageOptions) => string;

isDeprecation

Whether a line check produced is a warning rather than a refusal.

const isDeprecation: (line: string) => boolean;

iTerm

const iTerm: NotImplemented;

OSC 8 — link(text, url), projecting to text (url) where hyperlinks are not understood.

const link: (text: string, url: string) => string;

notify

OSC 9 — a desktop notification, without node-notifier's native binaries.

const notify: Capability;

processRuntime

What a real process looks like. Callers that have not got one pass their own.

const processRuntime: () => Runtime;

refusals

The lines that refuse the document — what register throws on, and what a CLI exits on.

const refusals: (lines: readonly string[]) => string[];

reset

Registration is global, so tests and hosts need a way back.

const reset: () => void;

setCwd

OSC 50 + OSC 9;9 — setCwd(cwd), defaulting to the runtime's own, projecting to nothing. Read per call: the working directory moves.

const setCwd: (cwd?: string) => string;

title

OSC 0 — the window and tab title.

const title: Capability;

Interfaces

AnsiEscapes

The members of ansi-escapes paratext implements, bound to one runtime.

interface AnsiEscapes {
    /** The bell, as a string — the incumbent's `beep`. */
    beep: string;
    /** OSC 8. Projects to `text (url)`. */
    link: (text: string, url: string) => string;
    /** OSC 1337. Projects to `options.caption`. */
    image: (data: Uint8Array | string, options?: ImageOptions) => string;
    /** OSC 50 + OSC 9;9, the two halves upstream concatenates. Projects to nothing. */
    setCwd: (cwd?: string) => string;
}

Capability

interface Capability {
    /** How callers name it: `link`, `image`, `clipboard`, or anything a third party invents. */
    readonly name: string;
    /** The OSC code, or `BEL`. Documentation for a reader, and a key for `check`. */
    readonly osc: number | 'BEL';
    readonly when: Support;
    /** The bytes, as a template, for a terminal that does understand. */
    readonly encode: string;
    /**
     * What to print when it does not — PRINCIPLES rule 6, and the reason this package exists.
     * An image becomes its caption, a notification a printed line, a hyperlink `text (url)`.
     * A capability without one is refused at `register`, because a sequence a terminal cannot
     * read is not a feature, it is `]1337;File=inline=1;…` across a user's screen. An empty
     * string is a legitimate projection — a window title has nothing to say in a log — but it
     * has to be written down rather than left out.
     */
    readonly fallback: string;
}

ImageOptions

ansi-escapes' image() options, plus the field a projection needs.

interface ImageOptions {
    /** Cells, pixels (`20px`) or percent (`50%`) — passed through as the incumbent passes it. */
    width?: number | string;
    height?: number | string;
    /** `false` writes `preserveAspectRatio=0`, exactly as upstream does. */
    preserveAspectRatio?: boolean;
    /**
     * What a terminal that cannot draw the image prints instead. paratext's addition, and not
     * optional in spirit: an image with no caption projects to nothing, which is silence where
     * the incumbent would have written bytes nobody can read.
     */
    caption?: string;
}

Runtime

The slice of a runtime this package needs — the same seam roundel/policy.ts declares, for the same reason: nothing here reads process directly, so a test is a two-line literal and a host can lie about the terminal on purpose.

interface Runtime {
    env: Record<string, string | undefined>;
    /**
     * `stderr` is optional and falls back to `stdout` when absent.
     */
    isTTY: {
        stdout: boolean;
        stderr?: boolean;
    };
    /**
     * The working directory, for the one capability that has to name it.
     */
    cwd?: string;
    /**
     * The command line and the OS, for `paratext/terminal-link` alone: `supports-hyperlinks`
     * reads `--no-hyperlink` / `--color` flags and refuses win32 outside Windows Terminal, and
     * a drop-in that ignored them would link where the incumbent did not. Optional for the
     * reason `cwd` is.
     */
    argv?: readonly string[];
    platform?: string;
    /**
     * The output mode a host that composes has already decided — roundel's `outputMode(rt)`:
     * `tty`, `pipe`, `ci`, `accessible` or `json`. Anything but `tty` is a static projection,
     * so every capability renders its `fallback`, the rule flagstaff's `hoist()` follows.
     */
    mode?: string;
}

Support

When a terminal is believed to understand a sequence. Every clause is optional and all of them must hold; termProgram and envAny are ORs within themselves.

interface Support {
    /** Refuse a pipe. Almost always true: a file that receives OSC gets control bytes in it. */
    readonly tty?: boolean;
    /** Any one of these `TERM_PROGRAM` values. */
    readonly termProgram?: readonly string[];
    /** Any one of these environment variables merely being set, as VTE announces itself. */
    readonly envAny?: readonly string[];
    /** An exact `TERM`, for the terminals that identify that way. */
    readonly term?: string;
}

Types

Fields

What a caller hands a capability. Flat and string-valued, and therefore describable.

type Fields = Readonly<Record<string, string | undefined>>;

NotImplemented

The type of a name that is declared and not implemented.

type NotImplemented = undefined;

Re-exported

Documented on the page of the entry point that declares them.

ExportKindDocumented in
beginSynchronizedOutputconstparatext/csi
clearScreenconstparatext/csi
clearTerminalconstparatext/csi
clearViewportconstparatext/csi
cursorBackwardconstparatext/csi
cursorDownconstparatext/csi
cursorForwardconstparatext/csi
cursorGetPositionconstparatext/csi
cursorHideconstparatext/csi
cursorLeftconstparatext/csi
cursorMoveconstparatext/csi
cursorNextLineconstparatext/csi
cursorPrevLineconstparatext/csi
cursorRestorePositionconstparatext/csi
cursorSavePositionconstparatext/csi
cursorShowconstparatext/csi
cursorToconstparatext/csi
cursorUpconstparatext/csi
endSynchronizedOutputconstparatext/csi
enterAlternativeScreenconstparatext/csi
eraseDownconstparatext/csi
eraseEndLineconstparatext/csi
eraseLineconstparatext/csi
eraseLinesconstparatext/csi
eraseScreenconstparatext/csi
eraseStartLineconstparatext/csi
eraseUpconstparatext/csi
exitAlternativeScreenconstparatext/csi
scrollDownconstparatext/csi
scrollUpconstparatext/csi
synchronizedOutputconstparatext/csi

On this page