/** * The bounded-surface kernel: the terminal mechanics every live-region * inspector shares, kept apart from the capability it presents. * * An overlay owns feature state — what it is looking at, what a key means — and * this module owns what makes that fit a terminal: the fixed chrome rows, the * content width and capacity, the physical-row check against Screen's wrapping, * the one-row backstop for a terminal too small to frame, an owned temporary * notice, and the close handshake with `TuiSlots`. * * Before this module each of the seventeen bounded surfaces re-derived all of * it, so a frame-geometry change had to be made in seventeen files and the * notice lifetime had drifted to four values with no owner. Nothing here knows * a domain: a surface reads a value and returns rows. * * This is internal presentation vocabulary, not a plugin SDK. It is deliberately * not exported from the package entry, and a surface whose layout is not one of * the shapes here stays free to keep its own `render` and use the helpers. * @module dshline/surface */ import type { Key } from '@dshline/renderer'; import type { TuiOverlay } from './slots.ts'; /** Rows outside a framed body: the leading blank and the two frame borders. */ export declare const SURFACE_FIXED_ROWS = 3; /** Narrowest terminal whose framed surface keeps one physical row per fact. */ export declare const SURFACE_MIN_COLUMNS: number; /** * Count the physical rows `Screen` draws for candidate logical lines. * * A logical row wider than the terminal is wrapped into several physical rows, * and the live region must never exceed the screen — a row scrolled off cannot * be climbed back to and erased. Every surface that budgets against height needs * this same count, which is why it lives here rather than once per surface. * @param lines - candidate logical lines. * @param columns - the terminal's width. * @returns one entry per physical row. */ export declare function physicalRows(lines: readonly string[], columns: number): string[]; /** * The one-row backstop for a terminal too small to frame. * * Every candidate is a WHOLE truthful phrase. A cut phrase says something the * reader cannot act on — `esc cl` names neither the state nor the way out — so * the ladder tries the most specific summary first and falls back to the bare * escape. An empty result is the honest answer when not even `esc` fits. * @param phrases - summary phrases, most specific first; one phrase is common. * @param columns - the terminal's width. * @returns at most one painted row. */ export declare function compactRows(phrases: string | readonly string[], columns: number): string[]; /** One active temporary outcome. */ export interface SurfaceNoticeReading { /** Untrusted message text; escaped when drawn. */ readonly text: string; /** Whether the outcome failed, which colours it and lets it win the geometry fallback. */ readonly failed: boolean; } /** * Make one untrusted notice message safe to draw in a single physical row. * * `escapeControls` neutralizes terminal controls but deliberately preserves a * line feed, because multi-line layout text is legitimate. A notice is * contracted to ONE row, so a message carrying a newline would become physical * rows the live region never budgeted: `Screen` wraps them after the budget is * fixed, and a region taller than the screen leaves rows in scrollback that can * never be erased. Collapsing the breaks before styling keeps the one-row * promise without weakening the control-safety the escaping provides. * @param text - untrusted message text. * @returns escaped text with no line breaks. */ export declare function noticeText(text: string): string; /** * Draw one notice reading as a bounded row. * * Escaping happens before styling: a message can quote a path or a provider * error, so it is untrusted text that must not add rows or operate the terminal. * @param reading - the active reading, or undefined when there is none. * @param columns - display columns available. * @returns the painted row, or undefined while no notice is active or when the * terminal has no columns to put it in. */ export declare function noticeRow(reading: SurfaceNoticeReading | undefined, columns: number): string | undefined; /** * A temporary outcome owned by exactly one surface. * * The lifetime is the surface's own choice rather than a shared constant: a * refused compaction and a discovery failure are not the same event, and the * copies of `NOTICE_MS` this replaced had already drifted to four values. * * {@link SurfaceNotice.read} retires an expired notice, but that is now backed * by one unref'd timer per notice: a notice with a declared lifetime asks its * owner for a repaint the moment the lifetime ends. Before this, an idle * terminal kept an expired notice on screen until some unrelated paint — a tool * result, a spinner beat — happened to arrive, which is exactly when a notice * is least useful. * * The timer is an INVALIDATION mechanism only. It never touches the notice: * {@link SurfaceNotice.read} is still the sole state authority, so the deadline * stays the only truth and the next render derives it. Showing again cancels * the prior deadline, so two timers are never live, and an older callback that * somehow fires after a replacement cannot clear the newer notice because it * asks for a repaint and nothing more — `read` grades whatever deadline is * current when that repaint runs. */ export declare class SurfaceNotice { private readonly lifetimeMs; private readonly options; private state; private timer; private disposed; /** * @param lifetimeMs - how long a shown notice stays readable. * @param options - the clock that grades the deadline, and the repaint request * the expiry makes. A surface with an injected clock passes it so the timer * and the deadline share one timeline; tests may construct with neither. */ constructor(lifetimeMs: number, options?: { readonly now?: () => number; readonly invalidate?: () => void; }); /** * Replace any current notice with a new one, restarting its lifetime. * @param text - untrusted message text. * @param failed - whether the outcome failed. */ show(text: string, failed?: boolean): void; /** * Read the active notice, retiring it once its lifetime has passed. * @param now - current wall-clock instant; defaults to the injected clock. * @returns the active reading, or undefined once retired. */ read(now?: number): SurfaceNoticeReading | undefined; /** * One bounded row for the active notice. * @param columns - display columns available. * @returns the painted row, or undefined while no notice is active. */ row(columns: number): string | undefined; /** * Cancel the pending expiry and forget the notice. Idempotent, and a later * {@link SurfaceNotice.show} is refused. */ dispose(): void; /** The clock this notice's deadline is graded on. */ private now; /** * Arm the one expiry timer for the current notice. * * Called after every `show`, so a replacement never leaves two deadlines * live. The callback only invalidates — it never retires the notice and never * touches its state. A Node timer can fire a hair before the deadline it was * measured against, and a caller-injected clock can lag Node's; repainting * then would render a notice `read` still calls current, and with no timer * left there would be no second chance — the stale row this mechanism exists * to clear. So the callback waits out the true remainder instead. That is a * one-shot retry at the deadline, not a poll: each wait is the exact * remaining time, which shrinks to zero as the clock advances. */ private arm; /** Cancel the pending expiry, if any. */ private cancel; } /** Inputs one framed bounded surface needs from its presenter. */ export interface BoundedSurfaceSpec { /** The presenter's own state, read fresh on every paint. */ readonly reading: () => S; /** Right-hand frame label for the current reading. */ readonly title: (reading: S) => string; /** * Body rows for a reading. * @param reading - current state. * @param width - display columns inside the frame. * @param capacity - body rows the current geometry can show, excluding the * frame's fixed rows and an active notice. */ readonly body: (reading: S, width: number, capacity: number) => readonly string[]; /** Whole-phrase backstop summaries, most specific first. */ readonly compact: (reading: S) => string | readonly string[]; /** Footer help; defaults to `esc close`. */ readonly footer?: (reading: S) => string; /** * Optional temporary outcome, given its own reserved row while active. * * The presenter constructs it with the repaint request {@link SurfaceNotice} * makes when the lifetime ends, and disposes it when the surface comes down. */ readonly notice?: SurfaceNotice; /** * Feature keys, after the shared close handling. The surface owns the * keyboard while it is mounted, so a key it does not close on is delivered * here whether or not the presenter acts on it. * @param key - the decoded keystroke. * @param reading - the current reading. */ readonly onKey?: (key: Key, reading: S) => void; /** Remove this temporary surface. */ readonly close: () => void; /** Release presenter-owned resources such as a heartbeat. */ readonly dispose?: () => void; /** Rows outside the body; defaults to {@link SURFACE_FIXED_ROWS}. */ readonly fixedRows?: number; /** Minimum terminal width; defaults to {@link SURFACE_MIN_COLUMNS}. */ readonly minColumns?: number; } /** * Frame one bounded body, or report that it does not fit. * * The frame wraps whatever it is given, so the physical candidate — not the * logical row count — decides. A body that does not fit returns `undefined` and * the caller substitutes its geometry backstop rather than leaking a row into * scrollback. * * The title and footer are PLAIN, untrusted text and the kernel escapes them * before `paint`; passing already-styled text here would lose its colour. Body * rows are the presenter's own terminal rows and must already be safe. * @param options - terminal geometry, frame title, body rows, and optional footer. * @returns the framed rows, or undefined when they do not fit. */ export declare function frameBounded(options: { readonly columns: number; readonly rows: number; readonly title: string; readonly body: readonly string[]; readonly footer?: string; }): string[] | undefined; /** * Create one framed bounded surface over a presenter's reading. * * The surface owns the close guard, the geometry, the shared escape keys, and * the notice row; the presenter owns what the rows say and what its own keys * mean. Escape and ctrl-c always close, and nothing else quits. * @param spec - the presenter's reading, rows, keys, and close control. * @returns a live-region overlay that never writes the transcript. */ export declare function createBoundedSurface(spec: BoundedSurfaceSpec): TuiOverlay; /** * Open one overlay and return its dismisser. * * A surface closes itself by calling the callback it was handed, but the * dismisser only exists after `pushOverlay` returns, and `TuiSlots` calls the * overlay's `mounted()` before that. A close requested from `create` or from * `mounted()` therefore arrives before there is anything to call, so it is * remembered and applied the moment mounting returns. Closing is idempotent: * the first request removes the overlay once, and later ones are no-ops. * @param slots - the live-region registry that owns the overlay stack. * @param create - builds the overlay, given the callback that closes it. * @returns a function that removes the overlay; safe to call more than once. */ export declare function openSurface(slots: { pushOverlay(overlay: TuiOverlay): () => void; }, create: (close: () => void) => TuiOverlay): () => void; //# sourceMappingURL=surface.d.ts.map