@1sat/cli 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/output.ts ADDED
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Output formatting utilities for the 1sat CLI.
3
+ *
4
+ * Supports --json and --quiet modes.
5
+ */
6
+
7
+ import chalk from 'chalk'
8
+
9
+ export function formatSuccess(msg: string): string {
10
+ return chalk.green(msg)
11
+ }
12
+
13
+ export function formatError(msg: string): string {
14
+ return chalk.red(msg)
15
+ }
16
+
17
+ export function formatValue(val: string | number): string {
18
+ return chalk.cyan(String(val))
19
+ }
20
+
21
+ export function formatLabel(label: string): string {
22
+ return chalk.dim(label)
23
+ }
24
+
25
+ export function formatWarning(msg: string): string {
26
+ return chalk.yellow(msg)
27
+ }
28
+
29
+ /**
30
+ * Output data in the appropriate format based on flags.
31
+ *
32
+ * --json: JSON output (machine-readable)
33
+ * --quiet: suppress all output
34
+ * default: human-readable
35
+ */
36
+ export function output(
37
+ data: unknown,
38
+ opts: { json?: boolean; quiet?: boolean },
39
+ ): void {
40
+ if (opts.quiet) return
41
+
42
+ if (opts.json) {
43
+ console.log(JSON.stringify(data, null, 2))
44
+ return
45
+ }
46
+
47
+ if (typeof data === 'string') {
48
+ console.log(data)
49
+ return
50
+ }
51
+
52
+ if (typeof data === 'object' && data !== null) {
53
+ for (const [key, value] of Object.entries(data)) {
54
+ console.log(`${formatLabel(`${key}:`)} ${formatValue(String(value))}`)
55
+ }
56
+ return
57
+ }
58
+
59
+ console.log(String(data))
60
+ }
61
+
62
+ /**
63
+ * Print an error message and exit.
64
+ */
65
+ export function fatal(msg: string): never {
66
+ console.error(formatError(`Error: ${msg}`))
67
+ process.exit(1)
68
+ }
69
+
70
+ /**
71
+ * Print a table of key-value pairs.
72
+ */
73
+ export function printKeyValue(
74
+ pairs: Record<string, string | number | boolean | undefined>,
75
+ ): void {
76
+ const maxKeyLen = Math.max(...Object.keys(pairs).map((k) => k.length))
77
+ for (const [key, value] of Object.entries(pairs)) {
78
+ if (value === undefined) continue
79
+ const paddedKey = key.padEnd(maxKeyLen)
80
+ console.log(` ${formatLabel(paddedKey)} ${formatValue(String(value))}`)
81
+ }
82
+ }