From 01be93e66092de881c260b32b3c10cc9369c447f Mon Sep 17 00:00:00 2001 From: Plumbiu <3434909403@qq.com> Date: Thu, 14 Dec 2023 11:02:51 +0800 Subject: [PATCH 1/6] feat(formatTree): support max depth --- examples/tree.ts | 77 +++++++++++++++++++++++++++++++++++++++++++++++ src/utils/tree.ts | 29 ++++++++++++++++-- 2 files changed, 103 insertions(+), 3 deletions(-) diff --git a/examples/tree.ts b/examples/tree.ts index 847e131f..5b6ac262 100644 --- a/examples/tree.ts +++ b/examples/tree.ts @@ -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", + ], + }, + ], { + depth: 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", + ], + }, + ], { + depth: 2, + ellipsis: '---' + }), + ); } main(); diff --git a/src/utils/tree.ts b/src/utils/tree.ts index 2ea5cb9a..1a326232 100644 --- a/src/utils/tree.ts +++ b/src/utils/tree.ts @@ -1,4 +1,5 @@ import { type ColorName, colorize } from "./color"; +import { log } from "./prompt"; export type TreeItemObject = { /** @@ -31,11 +32,22 @@ export type TreeOptions = { * @default " " */ prefix?: string; + /** + * Limit the depth of tree + */ + depth?: number; + /** + * Ellipsis of the tree + * + * @default "..." + */ + ellipsis?: string }; export function formatTree(items: TreeItem[], options?: TreeOptions): string { options = { prefix: " ", + ellipsis: "...", ...options, }; @@ -49,14 +61,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?.depth != null && options.depth <= 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`; @@ -65,6 +87,7 @@ function _buildTree(items: TreeItem[], options?: TreeOptions): string[] { if (item.children) { const _tree = _buildTree(item.children, { ...options, + depth: options?.depth ? options.depth - 1 : Number.POSITIVE_INFINITY, prefix: `${options?.prefix}${isLast ? " " : "│ "}`, }); chunks.push(..._tree); From e3617b558df593633aafce2e169a4e3c9b948868 Mon Sep 17 00:00:00 2001 From: Plumbiu <3434909403@qq.com> Date: Thu, 14 Dec 2023 11:09:15 +0800 Subject: [PATCH 2/6] chore: remote unused import --- src/utils/tree.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/tree.ts b/src/utils/tree.ts index 1a326232..c8a3b244 100644 --- a/src/utils/tree.ts +++ b/src/utils/tree.ts @@ -1,5 +1,4 @@ import { type ColorName, colorize } from "./color"; -import { log } from "./prompt"; export type TreeItemObject = { /** From 90d9448d4a72e3c672d1d8e63a67e2bff8a105d5 Mon Sep 17 00:00:00 2001 From: Plumbiu <3434909403@qq.com> Date: Thu, 14 Dec 2023 11:14:14 +0800 Subject: [PATCH 3/6] chore: type --- src/utils/tree.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/tree.ts b/src/utils/tree.ts index c8a3b244..b8e9381b 100644 --- a/src/utils/tree.ts +++ b/src/utils/tree.ts @@ -86,7 +86,7 @@ function _buildTree(items: TreeItem[], options?: TreeOptions): string[] { if (item.children) { const _tree = _buildTree(item.children, { ...options, - depth: options?.depth ? options.depth - 1 : Number.POSITIVE_INFINITY, + depth: options?.depth == null ? undefined : options.depth - 1, prefix: `${options?.prefix}${isLast ? " " : "│ "}`, }); chunks.push(..._tree); From ad81872a8505fe94ad4d95340205054c1b2e44b4 Mon Sep 17 00:00:00 2001 From: Plumbiu <3434909403@qq.com> Date: Thu, 14 Dec 2023 21:14:22 +0800 Subject: [PATCH 4/6] chore: rename depth to maxDepth --- examples/tree.ts | 4 ++-- src/utils/tree.ts | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/examples/tree.ts b/examples/tree.ts index 5b6ac262..99f1a02d 100644 --- a/examples/tree.ts +++ b/examples/tree.ts @@ -119,7 +119,7 @@ function main() { ], }, ], { - depth: 2 + maxDepth: 2 }), ); @@ -157,7 +157,7 @@ function main() { ], }, ], { - depth: 2, + maxDepth: 2, ellipsis: '---' }), ); diff --git a/src/utils/tree.ts b/src/utils/tree.ts index b8e9381b..c42c5db9 100644 --- a/src/utils/tree.ts +++ b/src/utils/tree.ts @@ -31,10 +31,12 @@ export type TreeOptions = { * @default " " */ prefix?: string; + /** - * Limit the depth of tree + * The max depth of tree */ - depth?: number; + maxDepth?: number; + /** * Ellipsis of the tree * @@ -64,7 +66,7 @@ function _buildTree(items: TreeItem[], options?: TreeOptions): string[] { for (let i = 0; i <= total; i++) { const item = items[i]; const isItemString = typeof item === "string"; - const isLimit = options?.depth != null && options.depth <= 0; + const isLimit = options?.maxDepth != null && options.maxDepth <= 0; if (isLimit) { const ellipsis = `${options.prefix}${options.ellipsis}\n`; return [ @@ -86,7 +88,7 @@ function _buildTree(items: TreeItem[], options?: TreeOptions): string[] { if (item.children) { const _tree = _buildTree(item.children, { ...options, - depth: options?.depth == null ? undefined : options.depth - 1, + maxDepth: options?.maxDepth == null ? undefined : options.maxDepth - 1, prefix: `${options?.prefix}${isLast ? " " : "│ "}`, }); chunks.push(..._tree); From 17d43d675180e86bb0f16707228626c1dfd88d1a Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Thu, 19 Dec 2024 23:43:15 +0000 Subject: [PATCH 5/6] chore: apply automated updates --- examples/tree.ts | 136 ++++++++++++++++++++++++---------------------- src/utils/tree.ts | 11 ++-- 2 files changed, 77 insertions(+), 70 deletions(-) diff --git a/examples/tree.ts b/examples/tree.ts index 99f1a02d..54cfb861 100644 --- a/examples/tree.ts +++ b/examples/tree.ts @@ -87,79 +87,85 @@ function main() { // Deep tree with max depth consola.log( - formatTree([ - { - text: "format", - color: "red", - }, + 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", + ], + }, + ], { - 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, }, - ], { - maxDepth: 2 - }), + ), ); // Indicate the ellipsis consola.log( - formatTree([ - { - text: "format", - color: "red", - }, + 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", + ], + }, + ], { - 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: "---", }, - ], { - maxDepth: 2, - ellipsis: '---' - }), + ), ); } diff --git a/src/utils/tree.ts b/src/utils/tree.ts index ed9e8597..6d9828ec 100644 --- a/src/utils/tree.ts +++ b/src/utils/tree.ts @@ -42,7 +42,7 @@ export type TreeOptions = { * * @default "..." */ - ellipsis?: string + ellipsis?: string; }; /** @@ -84,9 +84,9 @@ function _buildTree(items: TreeItem[], options?: TreeOptions): string[] { return [ isItemString ? ellipsis - : (item.color - ? colorize(item.color, ellipsis) - : ellipsis), + : item.color + ? colorize(item.color, ellipsis) + : ellipsis, ]; } const isLast = i === total; @@ -100,7 +100,8 @@ function _buildTree(items: TreeItem[], options?: TreeOptions): string[] { if (item.children) { const _tree = _buildTree(item.children, { ...options, - maxDepth: options?.maxDepth == null ? undefined : options.maxDepth - 1, + maxDepth: + options?.maxDepth == null ? undefined : options.maxDepth - 1, prefix: `${options?.prefix}${isLast ? " " : "│ "}`, }); chunks.push(..._tree); From 406fdeb68356ae285117bf7be1c3b9a2c89499f8 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 20 Dec 2024 00:44:58 +0100 Subject: [PATCH 6/6] lint --- src/utils/tree.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/utils/tree.ts b/src/utils/tree.ts index 6d9828ec..17ad379a 100644 --- a/src/utils/tree.ts +++ b/src/utils/tree.ts @@ -84,9 +84,10 @@ function _buildTree(items: TreeItem[], options?: TreeOptions): string[] { return [ isItemString ? ellipsis - : item.color + : + (item.color ? colorize(item.color, ellipsis) - : ellipsis, + : ellipsis), // prettier-ignore ]; } const isLast = i === total;