|
1 | 1 | import { Chalk, Instance } from 'chalk';
|
2 | 2 | import archy from 'archy';
|
3 | 3 |
|
4 |
| -import { ParserNode, ParserNodeValue } from './Parser'; |
| 4 | +import { Parser, ParserNode, ParserNodeValue } from './Parser'; |
| 5 | +import { parseUnknown } from './parseUnknown'; |
5 | 6 |
|
6 | 7 | const hideDefault = ['pubkeys', 'sequence', 'locktime', 'scriptSig', 'witness'];
|
7 | 8 |
|
8 | 9 | export function formatSat(v: number | bigint): string {
|
9 | 10 | return (Number(v) / 1e8).toFixed(8);
|
10 | 11 | }
|
11 | 12 |
|
| 13 | +type FormatOptions = { |
| 14 | + hide?: string[]; |
| 15 | + chalk?: Chalk; |
| 16 | +}; |
| 17 | + |
| 18 | +function getDefaultChalk(): Chalk { |
| 19 | + if (process.env.NO_COLOR) { |
| 20 | + return new Instance({ level: 0 }); |
| 21 | + } |
| 22 | + return new Instance(); |
| 23 | +} |
| 24 | + |
12 | 25 | export function formatTree(
|
13 | 26 | n: ParserNode,
|
14 |
| - { hide = hideDefault, chalk = new Instance() }: { hide?: string[]; chalk?: Chalk } = {} |
| 27 | + { hide = hideDefault, chalk = getDefaultChalk() }: FormatOptions = {} |
15 | 28 | ): string {
|
16 | 29 | function getLabel(
|
17 | 30 | label: string | number,
|
@@ -61,3 +74,13 @@ export function formatTree(
|
61 | 74 |
|
62 | 75 | return archy(toArchy(n));
|
63 | 76 | }
|
| 77 | + |
| 78 | +export function formatObjAsTree( |
| 79 | + label: string | number, |
| 80 | + obj: unknown, |
| 81 | + { hide = hideDefault, chalk = getDefaultChalk() }: FormatOptions = {} |
| 82 | +): string { |
| 83 | + const p = new Parser({ parseError: 'continue' }); |
| 84 | + const node = parseUnknown(p, label, obj, { omit: hide }); |
| 85 | + return formatTree(node, { hide, chalk }); |
| 86 | +} |
0 commit comments