lmformat guide
Two-phase grid measurement, decimal alignment, width capping, and scalar formatting conventions in @lmctl-ai/lmformat.
Overview
lmformat is designed around predictable, zero-dependency CLI output: columns size themselves to the widest cell by default, two spaces separate adjacent columns, trailing spaces are never emitted at the end of a line, and every glyph or width cap is an explicit caller option rather than a heuristic.
Measuring and rendering shared grids
While formatTable and printTable measure and render a single table in one step, CLI status displays often group rows under separate section headings while keeping columns aligned down the entire screen. Splitting measurement from rendering solves this cleanly:
- Pass an array of section row arrays (or { rows, headers } objects) to measureColumns(grids, options) to compute one shared widths array across all sections.
- Render each section independently with renderGrid(rows, { widths, margin: 2 }), placing section headings or blank lines between sections as needed.
const { measureColumns, renderGrid } = require('@lmctl-ai/lmformat');
const running = [['math.lmctl:Lead', 'exec', 'running 16h ago']];
const idle = [
['triage.lmctl:Triage', 'chat', 'ran 24s, idle 5m ago'],
['or.lmctl:Lead', 'chat', 'ran 5s, idle 6m ago'],
];
const widths = measureColumns([running, idle]);
console.log('Running:');
console.log(renderGrid(running, { widths, margin: 2 }));
console.log('Idle:');
console.log(renderGrid(idle, { widths, margin: 2 }));
Decimal alignment across sections
Setting align to decimal aligns floating-point numbers and dollar amounts ($10, $0.25, -$1.25, comma-grouped thousands) on the decimal point without rounding or adding trailing zeros. Missing fractional digits are reserved with spaces, and non-numeric cells or headers fall back to standard right alignment. When sharing decimal alignment across multiple sections, compute const decimalPlaces = measureDecimalPlaces(sections) first and pass both align and decimalPlaces to measureColumns and renderGrid.
Manual width caps and truncation
To prevent an outlier cell from stretching a table, pass max: [26, null, 20] to measureColumns, renderGrid, formatTable, or printTable. Any non-trailing cell wider than its column width is truncated to fit with a trailing ellipsis (default …, customizable via ellipsis: '...' or '' for a hard cut). Trailing cells are left open-ended by default; pass truncateTrailing: true to bound the last populated column as well, or truncate: false to disable truncation entirely.
Terminal display width and cell sanitization
Column measurement uses terminal display widths: East Asian wide and fullwidth characters and most emoji count as width 2, while combining marks and zero-width characters count as width 0. Because multi-line cells or terminal escape codes corrupt tabular alignment, raw control characters throw an error; use escapeCell(untrusted) to escape control bytes safely before formatting.
Scalar formatters for CLI telemetry
lmformat includes eight scalar formatters tailored for CLI status tables:
- formatCount(n) formats finite numbers using K/M/B/T suffixes (960K, 15.8M), promoting units after symmetric rounding so 999999 becomes 1.0M.
- formatDuration(ms, { maxUnit, style }) formats milliseconds in up to two units (2h 5m, or 2d02h03m with style: 'compact', or capped at days with maxUnit: 'd').
- formatRelativeTime(ts, { now }) returns the largest whole unit relative to now (3d, 40m ago, now, or unknown).
- formatPercent(fraction) and formatBar(fraction, { width, fill, empty, unknown }) render quota usage percentages and fixed-width progress bars.
- formatTimer(ms), formatClock(ts), and formatLocalTimestamp(ts) format digital stopwatches (00:36:12 or 5d 18:24:33) and local wall-clock timestamps.