|
1 | 1 | import { Command } from "commander"; |
2 | 2 | import { stringify } from "yaml"; |
| 3 | +import boxen from "boxen"; |
3 | 4 |
|
4 | | -export type OutputFormat = "json" | "text"; |
| 5 | +export type OutputFormat = "json" | "yaml" | "text" | "human"; |
5 | 6 |
|
6 | | -export function renderText(data: unknown): string { |
| 7 | +export function renderYaml(data: unknown): string { |
7 | 8 | return stringify(data, { lineWidth: 0 }).trimEnd(); |
8 | 9 | } |
9 | 10 |
|
| 11 | +/** @deprecated use renderYaml */ |
| 12 | +export const renderText = renderYaml; |
| 13 | + |
| 14 | +/** |
| 15 | + * Render a single value for human consumption. |
| 16 | + * Strings are returned as-is; objects/arrays are wrapped in a rounded boxen box. |
| 17 | + */ |
| 18 | +export function renderHuman(data: unknown): string { |
| 19 | + if (typeof data === "string") return data; |
| 20 | + return boxen(renderYaml(data), { padding: { top: 0, bottom: 0, left: 1, right: 1 }, borderStyle: "round" }); |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Render a collection for human consumption. |
| 25 | + * Each item is formatted as YAML and separated by a horizontal rule. |
| 26 | + */ |
| 27 | +export function renderHumanCollection(items: unknown[]): string { |
| 28 | + if (items.length === 0) return "(no results)"; |
| 29 | + const separator = "─".repeat(40); |
| 30 | + return items.map((item) => (typeof item === "string" ? item : renderYaml(item))).join(`\n${separator}\n`); |
| 31 | +} |
| 32 | + |
10 | 33 | export function output(command: Command, data: unknown): void { |
11 | | - if (command.optsWithGlobals().output === "json") { |
| 34 | + const format: OutputFormat = command.optsWithGlobals().output ?? "human"; |
| 35 | + if (format === "json") { |
12 | 36 | console.log(JSON.stringify(data)); |
| 37 | + } else if (format === "human") { |
| 38 | + console.log(renderHuman(data)); |
13 | 39 | } else { |
14 | | - console.log(renderText(data)); |
| 40 | + // "yaml" or "text" (backward-compat alias) |
| 41 | + console.log(renderYaml(data)); |
15 | 42 | } |
16 | 43 | } |
17 | 44 |
|
18 | 45 | /** |
19 | 46 | * Output a collection as NDJSON (one JSON object per line) in JSON mode, |
20 | | - * or as YAML text in text mode. |
| 47 | + * as YAML in yaml/text mode, or as a human-readable list in human mode. |
21 | 48 | */ |
22 | 49 | export function outputCollection(command: Command, items: unknown[]): void { |
23 | | - if (command.optsWithGlobals().output === "json") { |
| 50 | + const format: OutputFormat = command.optsWithGlobals().output ?? "human"; |
| 51 | + if (format === "json") { |
24 | 52 | for (const item of items) { |
25 | 53 | console.log(JSON.stringify(item)); |
26 | 54 | } |
| 55 | + } else if (format === "human") { |
| 56 | + console.log(renderHumanCollection(items)); |
27 | 57 | } else { |
28 | | - console.log(renderText(items)); |
| 58 | + // "yaml" or "text" (backward-compat alias) |
| 59 | + console.log(renderYaml(items)); |
29 | 60 | } |
30 | 61 | } |
0 commit comments