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

Function printFileTree

src/bin/codegraph.ts:2205–2269  ·  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

2203 * Print files as a tree
2204 */
2205function printFileTree(
2206 files: { path: string; language: string; nodeCount: number }[],
2207 includeMetadata: boolean,
2208 maxDepth: number | undefined,
2209 chalk: { dim: (s: string) => string; cyan: (s: string) => string }
2210): void {
2211 interface TreeNode {
2212 name: string;
2213 children: Map<string, TreeNode>;
2214 file?: { language: string; nodeCount: number };
2215 }
2216
2217 const root: TreeNode = { name: '', children: new Map() };
2218
2219 for (const file of files) {
2220 const parts = file.path.split('/');
2221 let current = root;
2222
2223 for (let i = 0; i < parts.length; i++) {
2224 const part = parts[i];
2225 if (!part) continue;
2226
2227 if (!current.children.has(part)) {
2228 current.children.set(part, { name: part, children: new Map() });
2229 }
2230 current = current.children.get(part)!;
2231
2232 if (i === parts.length - 1) {
2233 current.file = { language: file.language, nodeCount: file.nodeCount };
2234 }
2235 }
2236 }
2237
2238 const renderNode = (node: TreeNode, prefix: string, isLast: boolean, depth: number): void => {
2239 if (maxDepth !== undefined && depth > maxDepth) return;
2240
2241 const glyphs = getGlyphs();
2242 const connector = isLast ? glyphs.treeLast : glyphs.treeBranch;
2243 const childPrefix = isLast ? ' ' : glyphs.treePipe;
2244
2245 if (node.name) {
2246 let line = prefix + connector + node.name;
2247 if (node.file && includeMetadata) {
2248 line += chalk.dim(` (${node.file.language}, ${node.file.nodeCount} symbols)`);
2249 }
2250 console.log(line);
2251 }
2252
2253 const children = [...node.children.values()];
2254 children.sort((a, b) => {
2255 const aIsDir = a.children.size > 0 && !a.file;
2256 const bIsDir = b.children.size > 0 && !b.file;
2257 if (aIsDir !== bIsDir) return aIsDir ? -1 : 1;
2258 return a.name.localeCompare(b.name);
2259 });
2260
2261 for (let i = 0; i < children.length; i++) {
2262 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