MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / printFileTree

Function printFileTree

src/bin/codegraph.ts:2077–2141  ·  view source on GitHub ↗

* Print files as a tree

(
  files: { path: string; language: string; nodeCount: number }[],
  includeMetadata: boolean,
  maxDepth: number | undefined,
  chalk: { dim: (s: string) => string; cyan: (s: string) => string }
)

Source from the content-addressed store, hash-verified

2075 * Print files as a tree
2076 */
2077function printFileTree(
2078 files: { path: string; language: string; nodeCount: number }[],
2079 includeMetadata: boolean,
2080 maxDepth: number | undefined,
2081 chalk: { dim: (s: string) => string; cyan: (s: string) => string }
2082): void {
2083 interface TreeNode {
2084 name: string;
2085 children: Map<string, TreeNode>;
2086 file?: { language: string; nodeCount: number };
2087 }
2088
2089 const root: TreeNode = { name: '', children: new Map() };
2090
2091 for (const file of files) {
2092 const parts = file.path.split('/');
2093 let current = root;
2094
2095 for (let i = 0; i < parts.length; i++) {
2096 const part = parts[i];
2097 if (!part) continue;
2098
2099 if (!current.children.has(part)) {
2100 current.children.set(part, { name: part, children: new Map() });
2101 }
2102 current = current.children.get(part)!;
2103
2104 if (i === parts.length - 1) {
2105 current.file = { language: file.language, nodeCount: file.nodeCount };
2106 }
2107 }
2108 }
2109
2110 const renderNode = (node: TreeNode, prefix: string, isLast: boolean, depth: number): void => {
2111 if (maxDepth !== undefined && depth > maxDepth) return;
2112
2113 const glyphs = getGlyphs();
2114 const connector = isLast ? glyphs.treeLast : glyphs.treeBranch;
2115 const childPrefix = isLast ? ' ' : glyphs.treePipe;
2116
2117 if (node.name) {
2118 let line = prefix + connector + node.name;
2119 if (node.file && includeMetadata) {
2120 line += chalk.dim(` (${node.file.language}, ${node.file.nodeCount} symbols)`);
2121 }
2122 console.log(line);
2123 }
2124
2125 const children = [...node.children.values()];
2126 children.sort((a, b) => {
2127 const aIsDir = a.children.size > 0 && !a.file;
2128 const bIsDir = b.children.size > 0 && !b.file;
2129 if (aIsDir !== bIsDir) return aIsDir ? -1 : 1;
2130 return a.name.localeCompare(b.name);
2131 });
2132
2133 for (let i = 0; i < children.length; i++) {
2134 const child = children[i]!;

Callers 1

mainFunction · 0.85

Calls 4

renderNodeFunction · 0.85
hasMethod · 0.80
getMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected