lmformat reference
Exported functions, option interfaces, and error behaviors for @lmctl-ai/lmformat.
Table and grid functions
| Function signature | Return type | Description |
|---|---|---|
| formatTable(rows: TableCell[][], options?: TableOptions) | string | Measure and format a single table into an aligned multi-line string without a trailing newline. |
| printTable(rows: TableCell[][], options?: PrintTableOptions) | void | Format a table and write it with a final newline to process.stdout or options.stream (writes nothing if empty). |
| measureColumns(grids: Array<Grid | TableCell[][]>, options?: MeasureOptions) | number[] | Return the maximum display width of each column across one or more grids, subject to options.max caps. |
| measureDecimalPlaces(grids: Array<Grid | TableCell[][]>) | number[] | Return the maximum fractional digit count per column across data rows (excluding headers). |
| renderGrid(rows: TableCell[][], options?: RenderGridOptions) | string | Render a single grid using explicit options.widths or self-measured widths. |
| escapeCell(value: unknown) | string | Convert a value to a single-line string with tabs, newlines, ANSI sequences, and control characters escaped. |
TableOptions, RenderGridOptions, and PrintTableOptions
| Option | Type | Default | Description |
|---|---|---|---|
| headers | TableCell[] | undefined | Optional header row aligned according to each column's alignment. |
| align | Array<'left' | 'right' | 'decimal'> | ['left', ...] | Per-column alignment mode. |
| decimalPlaces | number[] | measured | Shared fractional digit slots per column for decimal alignment. |
| widths | number[] | measured | Explicit column widths for renderGrid (mutually exclusive with max). |
| max | Array<number | null | undefined> | undefined | Per-column maximum width caps during measurement. |
| truncate | boolean | true | Truncate over-width cells with an ellipsis; false allows overflow. |
| truncateTrailing | boolean | false | Also truncate the last populated cell of each row to its column width. |
| ellipsis | string | '…' | Truncation suffix ('...' for ASCII, '' for hard cut). |
| margin | number | string | 0 | Leading space count or prefix string prepended to every rendered line. |
| stream | { write(text: string): void } | process.stdout | Destination writable stream for printTable. |
Scalar formatting functions
| Function signature | Return type | Description |
|---|---|---|
| formatRelativeTime(value: TimestampInput, options?: { now?: TimestampInput }) | string | Largest whole unit relative to now (w, d, h, m, s), e.g. 3d, 40m ago, now, or unknown. |
| formatCount(value: number | null | undefined) | string | Compact count (960K, 15.8M, 1.0M) or unknown for non-finite input. |
| formatDuration(ms: number | null | undefined, options?: { maxUnit?: 'w'|'d'|'h'|'m'|'s'; style?: 'spaced'|'compact' }) | string | Up to two units (2h 5m) or dense compact style (2d02h03m), or unknown. |
| formatPercent(fraction: number | null | undefined) | string | Rounded percentage string (0.16 -> 16%) or ?% for non-finite input. |
| formatBar(fraction: number | null | undefined, options?: { width?: number; fill?: string; empty?: string; unknown?: string }) | string | Bare progress bar of display-width-1 glyphs (default width 10, fill '#', empty ' ', unknown '?'). |
| formatTimer(ms: number | null | undefined) | string | Stopwatch string HH:MM:SS (or Nd HH:MM:SS past 24 hours), or unknown. |
| formatClock(value?: TimestampInput) | string | Local wall-clock time HH:MM:SS (defaults to current time), or unknown. |
| formatLocalTimestamp(value?: TimestampInput) | string | Local date and time YYYY-MM-DD HH:MM:SS (defaults to current time), or unknown. |
Validation errors and exit behavior
| Condition | Behavior |
|---|---|
| Control characters, tabs, newlines, or ANSI escapes in a table cell | Throws TypeError; sanitize untrusted inputs with escapeCell first. |
| Both widths and max passed to renderGrid | Throws TypeError; use max when measuring or widths when rendering pre-measured grids. |
| Invalid explicit options.now in formatRelativeTime | Throws TypeError. |
| Wide, zero-width, or multi-character glyph in formatBar | Throws TypeError; fill, empty, and unknown must have terminal display width 1. |
| Non-finite number or unparseable target timestamp in scalar formatters | Returns unknown (or ?% in formatPercent) without throwing. |