-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils):
formatTree
utility (#223)
- Loading branch information
1 parent
e3be6f6
commit ce2b368
Showing
2 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { TreeItem, formatTree } from "../src/utils/tree"; | ||
import { consola } from "./utils"; | ||
|
||
function main() { | ||
const keywords = [ | ||
"console", | ||
"logger", | ||
"reporter", | ||
"elegant", | ||
"cli", | ||
"universal", | ||
"unified", | ||
"prompt", | ||
"clack", | ||
"format", | ||
"error", | ||
"stacktrace", | ||
]; | ||
|
||
consola.log(formatTree(keywords)); | ||
|
||
consola.log( | ||
formatTree(keywords, { | ||
color: "cyan", | ||
prefix: " | ", | ||
}), | ||
); | ||
|
||
consola.log( | ||
formatTree( | ||
[ | ||
{ | ||
text: "consola", | ||
color: "green", | ||
}, | ||
{ | ||
text: "logger", | ||
}, | ||
].map( | ||
(item) => | ||
({ | ||
text: ` ${item.text}`, | ||
color: item.color, | ||
}) as TreeItem, | ||
), | ||
{ | ||
color: "gray", | ||
}, | ||
), | ||
); | ||
|
||
// Deep tree | ||
consola.log( | ||
formatTree([ | ||
{ | ||
text: "format", | ||
color: "red", | ||
}, | ||
{ | ||
text: "consola", | ||
color: "yellow", | ||
children: [ | ||
{ | ||
text: "logger", | ||
color: "green", | ||
children: [ | ||
{ | ||
text: "reporter", | ||
color: "cyan", | ||
}, | ||
{ | ||
text: "test", | ||
color: "magenta", | ||
children: ["nice tree"], | ||
}, | ||
], | ||
}, | ||
{ | ||
text: "reporter", | ||
color: "bold", | ||
}, | ||
"test", | ||
], | ||
}, | ||
]), | ||
); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { type ColorName, colorize } from "./color"; | ||
|
||
export type TreeItemObject = { | ||
/** | ||
* Text of the item | ||
*/ | ||
text: string; | ||
|
||
/** | ||
* Children of the item | ||
*/ | ||
children?: TreeItem[]; | ||
|
||
/** | ||
* Color of the item | ||
*/ | ||
color?: ColorName; | ||
}; | ||
|
||
export type TreeItem = string | TreeItemObject; | ||
|
||
export type TreeOptions = { | ||
/** | ||
* Color of the tree | ||
*/ | ||
color?: ColorName; | ||
|
||
/** | ||
* Prefix of the tree | ||
* | ||
* @default " " | ||
*/ | ||
prefix?: string; | ||
}; | ||
|
||
export function formatTree(items: TreeItem[], options?: TreeOptions): string { | ||
options = { | ||
prefix: " ", | ||
...options, | ||
}; | ||
|
||
const tree = _buildTree(items, options).join(""); | ||
if (options && options.color) { | ||
return colorize(options.color, tree); | ||
} | ||
|
||
return tree; | ||
} | ||
|
||
function _buildTree(items: TreeItem[], options?: TreeOptions): string[] { | ||
const chunks: string[] = []; | ||
|
||
const total = items.length - 1; | ||
for (let i = 0; i <= total; i++) { | ||
const item = items[i]; | ||
const isLast = i === total; | ||
const prefix = isLast ? `${options?.prefix}└─` : `${options?.prefix}├─`; | ||
|
||
if (typeof item === "string") { | ||
chunks.push(`${prefix}${item}\n`); | ||
} else { | ||
const log = `${prefix}${item.text}\n`; | ||
chunks.push(item.color ? colorize(item.color, log) : log); | ||
|
||
if (item.children) { | ||
const _tree = _buildTree(item.children, { | ||
...options, | ||
prefix: `${options?.prefix}${isLast ? " " : "│ "}`, | ||
}); | ||
chunks.push(..._tree); | ||
} | ||
} | ||
} | ||
|
||
return chunks; | ||
} |