|
| 1 | +import { Command } from "commander"; |
| 2 | + |
| 3 | +const isTTY = process.stdout.isTTY ?? false; |
| 4 | +const BOLD = isTTY ? "\x1b[1m" : ""; |
| 5 | +const DIM = isTTY ? "\x1b[2m" : ""; |
| 6 | +const CYAN = isTTY ? "\x1b[36m" : ""; |
| 7 | +const RESET = isTTY ? "\x1b[0m" : ""; |
| 8 | + |
| 9 | +const DATE_KEYS = new Set(["created_at", "updated_at", "publish_at", "publication_date"]); |
| 10 | + |
| 11 | +function formatValue(value: unknown, key?: string): string { |
| 12 | + if (value === null || value === undefined) return ""; |
| 13 | + if (Array.isArray(value)) return value.join(", "); |
| 14 | + if (typeof value === "object") return JSON.stringify(value); |
| 15 | + if (key && DATE_KEYS.has(key) && typeof value === "string") { |
| 16 | + const d = new Date(value); |
| 17 | + if (!isNaN(d.getTime())) return d.toLocaleString(); |
| 18 | + } |
| 19 | + return String(value); |
| 20 | +} |
| 21 | + |
| 22 | +function wrapText(str: string, width: number, indent: number): string { |
| 23 | + if (width < 10) width = 10; |
| 24 | + if (str.length <= width) return str; |
| 25 | + |
| 26 | + const lines: string[] = []; |
| 27 | + const pad = " ".repeat(indent); |
| 28 | + let remaining = str; |
| 29 | + |
| 30 | + while (remaining.length > 0) { |
| 31 | + const max = lines.length === 0 ? width : width; |
| 32 | + if (remaining.length <= max) { |
| 33 | + lines.push(remaining); |
| 34 | + break; |
| 35 | + } |
| 36 | + let breakAt = remaining.lastIndexOf(" ", max); |
| 37 | + if (breakAt <= 0) breakAt = max; |
| 38 | + lines.push(remaining.slice(0, breakAt)); |
| 39 | + remaining = remaining.slice(breakAt).trimStart(); |
| 40 | + } |
| 41 | + |
| 42 | + return lines.join("\n" + pad); |
| 43 | +} |
| 44 | + |
| 45 | +function formatList(rows: Record<string, unknown>[]) { |
| 46 | + const termWidth = process.stdout.columns ?? 80; |
| 47 | + |
| 48 | + for (let i = 0; i < rows.length; i++) { |
| 49 | + const entries = Object.entries(rows[i]).filter(([, v]) => v !== null && v !== undefined && v !== ""); |
| 50 | + const maxKeyLen = entries.reduce((max, [k]) => Math.max(max, k.length), 0); |
| 51 | + const valueWidth = termWidth - maxKeyLen - 3; |
| 52 | + |
| 53 | + const indent = maxKeyLen + 3; |
| 54 | + for (const [key, value] of entries) { |
| 55 | + const label = `${BOLD}${CYAN}${key.padEnd(maxKeyLen)}${RESET}`; |
| 56 | + const formatted = wrapText(formatValue(value, key), Math.max(valueWidth, 20), indent); |
| 57 | + console.log(`${label}${DIM} : ${RESET}${formatted}`); |
| 58 | + } |
| 59 | + |
| 60 | + if (i < rows.length - 1) { |
| 61 | + console.log(`${DIM}${"─".repeat(Math.min(termWidth, 60))}${RESET}`); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +function formatKeyValue(obj: Record<string, unknown>) { |
| 67 | + const termWidth = process.stdout.columns ?? 80; |
| 68 | + const entries = Object.entries(obj).filter(([, v]) => v !== null && v !== undefined && v !== ""); |
| 69 | + const maxKeyLen = entries.reduce((max, [k]) => Math.max(max, k.length), 0); |
| 70 | + const valueWidth = termWidth - maxKeyLen - 3; |
| 71 | + const indent = maxKeyLen + 3; |
| 72 | + |
| 73 | + for (const [key, value] of entries) { |
| 74 | + const label = `${BOLD}${CYAN}${key.padEnd(maxKeyLen)}${RESET}`; |
| 75 | + const formatted = wrapText(formatValue(value, key), Math.max(valueWidth, 20), indent); |
| 76 | + console.log(`${label}${DIM} : ${RESET}${formatted}`); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +export function output(data: unknown, cmd: Command) { |
| 81 | + const opts = cmd.optsWithGlobals(); |
| 82 | + if (opts.json) { |
| 83 | + console.log(JSON.stringify(data)); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if (data === null || data === undefined) return; |
| 88 | + |
| 89 | + if (Array.isArray(data)) { |
| 90 | + if (data.length === 0) { |
| 91 | + console.log(DIM + "(no results)" + RESET); |
| 92 | + return; |
| 93 | + } |
| 94 | + formatList(data); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + if (typeof data === "object") { |
| 99 | + formatKeyValue(data as Record<string, unknown>); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + console.log(String(data)); |
| 104 | +} |
0 commit comments