@swell/cli
Version:
Swell's command line interface/utility
62 lines (61 loc) • 2.01 kB
JavaScript
/**
* Render a two-column list: paste-back key + optional compact meta string.
*
* - Rows with meta render as ` <key-padded> <meta>`.
* - Rows without meta render as ` <key>` with no trailing whitespace.
* - Key column width is computed globally across all rows so meta columns
* align vertically.
* - Rows with a `group` are collected into sections; sections are separated
* by a blank line and a `── <label>` divider. Sections sort by `order`
* (lower first), ties preserve insertion order. A single group collapses
* to a flat listing.
*/
export function renderKeyMetaTable(rows) {
if (rows.length === 0) {
return [];
}
const keyWidth = Math.max(...rows.map((r) => r.key.length));
const formatRow = (row) => {
if (row.meta && row.meta.length > 0) {
return ` ${row.key.padEnd(keyWidth)} ${row.meta}`;
}
return ` ${row.key}`;
};
const hasGroups = rows.some((r) => r.group !== undefined);
if (!hasGroups) {
return rows.map((row) => formatRow(row));
}
const groupMap = new Map();
for (const row of rows) {
const info = row.group;
if (!info)
continue;
const existing = groupMap.get(info.slug);
if (existing) {
existing.rows.push(row);
}
else {
groupMap.set(info.slug, {
slug: info.slug,
label: info.label ?? info.slug,
order: info.order ?? 1,
rows: [row],
});
}
}
if (groupMap.size === 1) {
return rows.map((row) => formatRow(row));
}
const ordered = [...groupMap.values()].sort((a, b) => a.order - b.order);
const lines = [];
for (const group of ordered) {
if (lines.length > 0) {
lines.push('');
}
lines.push(`── ${group.label}`);
for (const row of group.rows) {
lines.push(formatRow(row));
}
}
return lines;
}