Fixed incorrect handling of table output.
All checks were successful
build / build (push) Successful in 10s
All checks were successful
build / build (push) Successful in 10s
This commit is contained in:
@@ -113,7 +113,7 @@ async function main(): Promise<void> {
|
||||
const filtered = outputFiltered(result, typedValues.query);
|
||||
const headerSpec = parseHeaderSpec(typedValues.header);
|
||||
|
||||
renderOutput(command, filtered, outputFormat, headerSpec);
|
||||
renderOutput(outputFormat, headerSpec, filtered);
|
||||
}
|
||||
|
||||
main().catch((err: unknown) => {
|
||||
|
||||
@@ -201,28 +201,25 @@ export async function readCsvFromStdin(separator: string): Promise<unknown> {
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ type HeaderSpec =
|
||||
| { mode: "list"; labels: string[] }
|
||||
| { mode: "map"; map: Record<string, string> };
|
||||
|
||||
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");
|
||||
|
||||
Reference in New Issue
Block a user