lmformat reference

Exported functions, option interfaces, and error behaviors for @lmctl-ai/lmformat.

Table and grid functions

Function signatureReturn typeDescription
formatTable(rows: TableCell[][], options?: TableOptions)stringMeasure and format a single table into an aligned multi-line string without a trailing newline.
printTable(rows: TableCell[][], options?: PrintTableOptions)voidFormat 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)stringRender a single grid using explicit options.widths or self-measured widths.
escapeCell(value: unknown)stringConvert a value to a single-line string with tabs, newlines, ANSI sequences, and control characters escaped.

TableOptions, RenderGridOptions, and PrintTableOptions

OptionTypeDefaultDescription
headersTableCell[]undefinedOptional header row aligned according to each column's alignment.
alignArray<'left' | 'right' | 'decimal'>['left', ...]Per-column alignment mode.
decimalPlacesnumber[]measuredShared fractional digit slots per column for decimal alignment.
widthsnumber[]measuredExplicit column widths for renderGrid (mutually exclusive with max).
maxArray<number | null | undefined>undefinedPer-column maximum width caps during measurement.
truncatebooleantrueTruncate over-width cells with an ellipsis; false allows overflow.
truncateTrailingbooleanfalseAlso truncate the last populated cell of each row to its column width.
ellipsisstring'…'Truncation suffix ('...' for ASCII, '' for hard cut).
marginnumber | string0Leading space count or prefix string prepended to every rendered line.
stream{ write(text: string): void }process.stdoutDestination writable stream for printTable.

Scalar formatting functions

Function signatureReturn typeDescription
formatRelativeTime(value: TimestampInput, options?: { now?: TimestampInput })stringLargest whole unit relative to now (w, d, h, m, s), e.g. 3d, 40m ago, now, or unknown.
formatCount(value: number | null | undefined)stringCompact 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' })stringUp to two units (2h 5m) or dense compact style (2d02h03m), or unknown.
formatPercent(fraction: number | null | undefined)stringRounded percentage string (0.16 -> 16%) or ?% for non-finite input.
formatBar(fraction: number | null | undefined, options?: { width?: number; fill?: string; empty?: string; unknown?: string })stringBare progress bar of display-width-1 glyphs (default width 10, fill '#', empty ' ', unknown '?').
formatTimer(ms: number | null | undefined)stringStopwatch string HH:MM:SS (or Nd HH:MM:SS past 24 hours), or unknown.
formatClock(value?: TimestampInput)stringLocal wall-clock time HH:MM:SS (defaults to current time), or unknown.
formatLocalTimestamp(value?: TimestampInput)stringLocal date and time YYYY-MM-DD HH:MM:SS (defaults to current time), or unknown.

Validation errors and exit behavior

ConditionBehavior
Control characters, tabs, newlines, or ANSI escapes in a table cellThrows TypeError; sanitize untrusted inputs with escapeCell first.
Both widths and max passed to renderGridThrows TypeError; use max when measuring or widths when rendering pre-measured grids.
Invalid explicit options.now in formatRelativeTimeThrows TypeError.
Wide, zero-width, or multi-character glyph in formatBarThrows TypeError; fill, empty, and unknown must have terminal display width 1.
Non-finite number or unparseable target timestamp in scalar formattersReturns unknown (or ?% in formatPercent) without throwing.