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

Function printFileTree

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

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