Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(formatTree): support max depth #267

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 77 additions & 0 deletions examples/tree.ts
Expand Up @@ -84,6 +84,83 @@ function main() {
},
]),
);

// Deep tree with max depth
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",
],
},
], {
maxDepth: 2
}),
);

// Indicate the ellipsis
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",
],
},
], {
maxDepth: 2,
ellipsis: '---'
}),
);
}

main();
30 changes: 27 additions & 3 deletions src/utils/tree.ts
Expand Up @@ -31,11 +31,24 @@ export type TreeOptions = {
* @default " "
*/
prefix?: string;

/**
* The max depth of tree
*/
maxDepth?: number;

/**
* Ellipsis of the tree
*
* @default "..."
*/
ellipsis?: string
};

export function formatTree(items: TreeItem[], options?: TreeOptions): string {
options = {
prefix: " ",
ellipsis: "...",
...options,
};

Expand All @@ -49,14 +62,24 @@ export function formatTree(items: TreeItem[], options?: TreeOptions): string {

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 isItemString = typeof item === "string";
const isLimit = options?.maxDepth != null && options.maxDepth <= 0;
if (isLimit) {
const ellipsis = `${options.prefix}${options.ellipsis}\n`;
return [
isItemString
? ellipsis
: (item.color
? colorize(item.color, ellipsis)
: ellipsis),
];
}
const isLast = i === total;
const prefix = isLast ? `${options?.prefix}└─` : `${options?.prefix}β”œβ”€`;

if (typeof item === "string") {
if (isItemString) {
chunks.push(`${prefix}${item}\n`);
} else {
const log = `${prefix}${item.text}\n`;
Expand All @@ -65,6 +88,7 @@ function _buildTree(items: TreeItem[], options?: TreeOptions): string[] {
if (item.children) {
const _tree = _buildTree(item.children, {
...options,
maxDepth: options?.maxDepth == null ? undefined : options.maxDepth - 1,
prefix: `${options?.prefix}${isLast ? " " : "β”‚ "}`,
});
chunks.push(..._tree);
Expand Down