UNPKG

@teaui/core

Version:

A high-level terminal UI library for Node

101 lines (100 loc) 3.4 kB
import type { SGRTerminal } from './terminal.js'; import { Style } from './Style.js'; /** * A headless SGRTerminal for testing. Stores characters and parsed Style objects * in a 2D grid, providing readable query methods for assertions: * * expect(term.charAt(0, 0)).toBe('H') * expect(term.styleAt(0, 0).bold).toBe(true) * expect(term.textAt(0, 0, 5)).toBe('Hello') * expect(term.textContent()).toContain('Hello') */ export declare class TestTerminal implements SGRTerminal { #private; cols: number; rows: number; constructor({ cols, rows }: { cols: number; rows: number; }); move(x: number, y: number): void; write(str: string): void; flush(): void; /** * Get the character at (x, y). Returns ' ' for empty cells. */ charAt(x: number, y: number): string; /** * Get the Style at (x, y). */ styleAt(x: number, y: number): Style; /** * Read `length` characters starting at (x, y) on the same row. * Skips empty cells from wide characters. */ textAt(x: number, y: number, length: number): string; /** * Get all text on a row (trimmed of trailing spaces). */ textAtRow(y: number): string; /** * Get all visible text content (rows joined by newlines, trailing spaces trimmed). */ textContent(): string; /** * Find the first position of a substring on the screen. * Returns {x, y} or null if not found. */ find(text: string): { x: number; y: number; } | null; /** * Get the Style of the first character of a found substring. * Useful for asserting styles on specific text. */ styleOf(text: string): Style | null; /** * Get a row as a string, optionally sliced. NOT trimmed. * Wide char continuation cells are skipped. */ getRow(y: number, from?: number, to?: number): string; /** * Extract a rectangular region as a multi-line string (lines joined by \n). * Trailing spaces on each line are preserved. Use for exact grid assertions: * * expect(term.textRect(0, 0, 5, 3)).toBe( * '┌───┐\n' + * '│ │\n' + * '└───┘' * ) */ textRect(x: number, y: number, width: number, height: number): string; /** * Assert that all cells in a range satisfy a style predicate. * Returns true if every cell in the range matches. * * term.stylesMatch(0, 0, 5, style => style.bold === true) */ stylesMatch(x: number, y: number, width: number, predicate: (style: Style) => boolean, height?: number): boolean; /** * Compare full screen content against a template string. Each line * of the expected string is compared against the corresponding row. * Trailing spaces in the template are significant — use '·' for * explicit space if needed. * Trailing empty lines in the template are not required to match. */ contentEquals(expected: string): boolean; /** * Like contentEquals but returns a diff-friendly string for assertion messages. * Use with expect().toBe() for readable failures: * * expect(term.frameContent()).toBe( * '┌───┐\n' + * '│Hi │\n' + * '└───┘' * ) */ frameContent(): string; reset(): void; }