From 9a6ff66d7204c5c0599fdb7411fdb351a0abb563 Mon Sep 17 00:00:00 2001 From: Slawomir Koszewski Date: Fri, 6 Mar 2026 23:03:01 +0100 Subject: [PATCH] Fixed incorrect handling of table output. --- package.json | 2 +- src/cli.ts | 2 +- src/cli/utils.ts | 21 +++++++++------------ src/markdown.ts | 25 ++++++++++++++----------- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index eef68b0..793da92 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@slawek/sk-tools", - "version": "0.1.0", + "version": "0.1.1", "type": "module", "files": [ "dist", diff --git a/src/cli.ts b/src/cli.ts index 9cf71ee..3203641 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -113,7 +113,7 @@ async function main(): Promise { const filtered = outputFiltered(result, typedValues.query); const headerSpec = parseHeaderSpec(typedValues.header); - renderOutput(command, filtered, outputFormat, headerSpec); + renderOutput(outputFormat, headerSpec, filtered); } main().catch((err: unknown) => { diff --git a/src/cli/utils.ts b/src/cli/utils.ts index 46dceac..1c780f2 100644 --- a/src/cli/utils.ts +++ b/src/cli/utils.ts @@ -201,28 +201,25 @@ export async function readCsvFromStdin(separator: string): Promise { } export function renderOutput( - command: string, - output: unknown, outputFormat: OutputFormat, headerSpec: HeaderSpec, + output: unknown, ): void { if (outputFormat === "tsv") { console.log(toTsv(output)); return; } - if (command === "table") { - console.log(toMarkdownTable( - output, - outputFormat === "alignedtable" || outputFormat === "prettytable", - headerSpec, - )); + // Map output format to a markdown table mode. + // - table: compact + // - alignedtable: aligned columns, no quoting + // - prettytable: aligned columns + quoting selected values + if (outputFormat === "prettytable") { + console.log(toMarkdownTable(output, "prettytable", headerSpec)); } else if (outputFormat === "alignedtable") { - console.log(toMarkdownTable(output, true, headerSpec)); - } else if (outputFormat === "prettytable") { - console.log(toMarkdownTable(output, true, headerSpec)); + console.log(toMarkdownTable(output, "alignedtable", headerSpec)); } else if (outputFormat === "table") { - console.log(toMarkdownTable(output, false, headerSpec)); + console.log(toMarkdownTable(output, "table", headerSpec)); } else { console.log(JSON.stringify(output, null, 2)); } diff --git a/src/markdown.ts b/src/markdown.ts index ed625f2..0efc833 100644 --- a/src/markdown.ts +++ b/src/markdown.ts @@ -10,6 +10,8 @@ type HeaderSpec = | { mode: "list"; labels: string[] } | { mode: "map"; map: Record }; +type TableMode = "table" | "alignedtable" | "prettytable"; + function formatCell(value: unknown): string { const text = value == null ? "" @@ -24,15 +26,15 @@ const inlineCodePredicates = [ && /^(?:\d{1,3}\.){3}\d{1,3}(?:\/(?:\d{1,2}|(?:\d{1,3}\.){3}\d{1,3}))?$/.test(value), ]; -function renderCell(raw: Scalar): string { +function renderCell(raw: Scalar, shouldQuote: boolean): string { const text = formatCell(raw); - - for (const predicate of inlineCodePredicates) { - if (predicate(raw)) { - return `\`${text}\``; + if (shouldQuote) { + for (const predicate of inlineCodePredicates) { + if (predicate(raw)) { + return `\`${text}\``; + } } } - return text; } @@ -106,9 +108,10 @@ function getScalarRowsAndHeaders(value: unknown): { headers: string[]; rows: Sca export function toMarkdownTable( value: unknown, - pretty = false, + mode: TableMode = "table", headerSpec: HeaderSpec = { mode: "default" }, ): string { + const prettyMode = mode === "prettytable"; const { headers, rows } = getScalarRowsAndHeaders(value); const headerDefinitions = headers.map((key, idx) => { let label = key; @@ -122,11 +125,11 @@ export function toMarkdownTable( return { key, label }; }); - if (!pretty) { + if (mode !== "alignedtable" && mode !== "prettytable") { const headerLine = `| ${headerDefinitions.map((h) => h.label).join(" | ")} |`; const separatorLine = `| ${headerDefinitions.map(() => "---").join(" | ")} |`; const rowLines = rows.map((row) => - `| ${headerDefinitions.map((h) => formatCell(row[h.key])).join(" | ")} |`, + `| ${headerDefinitions.map((h) => renderCell(row[h.key], prettyMode)).join(" | ")} |`, ); return [headerLine, separatorLine, ...rowLines].join("\n"); } @@ -134,7 +137,7 @@ export function toMarkdownTable( const widths = headerDefinitions.map((header, idx) => Math.max( header.label.length, - ...rows.map((row) => renderCell(row[headerDefinitions[idx].key]).length), + ...rows.map((row) => renderCell(row[headerDefinitions[idx].key], prettyMode).length), ) ); @@ -144,7 +147,7 @@ export function toMarkdownTable( const headerLine = renderRow(headerDefinitions.map((h) => h.label)); const separatorLine = `|-${widths.map((w) => "-".repeat(w)).join("-|-")}-|`; const rowLines = rows.map((row) => - renderRow(headerDefinitions.map((header) => renderCell(row[header.key]))), + renderRow(headerDefinitions.map((header) => renderCell(row[header.key], prettyMode))), ); return [headerLine, separatorLine, ...rowLines].join("\n");