diff --git a/packages/cli-uwf/src/format.ts b/packages/cli-uwf/src/format.ts index 47d32c0..ff339f4 100644 --- a/packages/cli-uwf/src/format.ts +++ b/packages/cli-uwf/src/format.ts @@ -2,7 +2,7 @@ import { stringify } from "yaml"; export type OutputFormat = "json" | "yaml" | "table"; -function formatTable(data: Array>): string { +function formatHorizontalTable(data: Array>): string { if (data.length === 0) return ""; const keys = Object.keys(data[0]); const widths = keys.map((k) => { @@ -20,6 +20,17 @@ function formatTable(data: Array>): string { return [header, ...rows].join("\n"); } +function formatVerticalTable(data: Record): string { + const entries = Object.entries(data); + if (entries.length === 0) return ""; + const keyWidth = Math.max(...entries.map(([k]) => k.length)); + const header = `${"KEY".padEnd(keyWidth)} VALUE`; + const rows = entries.map( + ([k, v]) => `${k.padEnd(keyWidth)} ${typeof v === "object" ? JSON.stringify(v) : String(v)}`, + ); + return [header, ...rows].join("\n"); +} + export function formatOutput(data: unknown, format: OutputFormat): string { switch (format) { case "json": @@ -27,8 +38,16 @@ export function formatOutput(data: unknown, format: OutputFormat): string { case "yaml": return stringify(data).trimEnd(); case "table": - if (Array.isArray(data) && data.length > 0 && typeof data[0] === "object" && data[0] !== null) { - return formatTable(data as Array>); + if ( + Array.isArray(data) && + data.length > 0 && + typeof data[0] === "object" && + data[0] !== null + ) { + return formatHorizontalTable(data as Array>); + } + if (typeof data === "object" && data !== null && !Array.isArray(data)) { + return formatVerticalTable(data as Record); } return stringify(data).trimEnd(); }