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

Function printFileTree

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

1616 * Print files as a tree
1617 */
1618function printFileTree(
1619 files: { path: string; language: string; nodeCount: number }[],
1620 includeMetadata: boolean,
1621 maxDepth: number | undefined,
1622 chalk: { dim: (s: string) => string; cyan: (s: string) => string }
1623): void {
1624 interface TreeNode {
1625 name: string;
1626 children: Map<string, TreeNode>;
1627 file?: { language: string; nodeCount: number };
1628 }
1629
1630 const root: TreeNode = { name: '', children: new Map() };
1631
1632 for (const file of files) {
1633 const parts = file.path.split('/');
1634 let current = root;
1635
1636 for (let i = 0; i < parts.length; i++) {
1637 const part = parts[i];
1638 if (!part) continue;
1639
1640 if (!current.children.has(part)) {
1641 current.children.set(part, { name: part, children: new Map() });
1642 }
1643 current = current.children.get(part)!;
1644
1645 if (i === parts.length - 1) {
1646 current.file = { language: file.language, nodeCount: file.nodeCount };
1647 }
1648 }
1649 }
1650
1651 const renderNode = (node: TreeNode, prefix: string, isLast: boolean, depth: number): void => {
1652 if (maxDepth !== undefined && depth > maxDepth) return;
1653
1654 const glyphs = getGlyphs();
1655 const connector = isLast ? glyphs.treeLast : glyphs.treeBranch;
1656 const childPrefix = isLast ? ' ' : glyphs.treePipe;
1657
1658 if (node.name) {
1659 let line = prefix + connector + node.name;
1660 if (node.file && includeMetadata) {
1661 line += chalk.dim(` (${node.file.language}, ${node.file.nodeCount} symbols)`);
1662 }
1663 console.log(line);
1664 }
1665
1666 const children = [...node.children.values()];
1667 children.sort((a, b) => {
1668 const aIsDir = a.children.size > 0 && !a.file;
1669 const bIsDir = b.children.size > 0 && !b.file;
1670 if (aIsDir !== bIsDir) return aIsDir ? -1 : 1;
1671 return a.name.localeCompare(b.name);
1672 });
1673
1674 for (let i = 0; i < children.length; i++) {
1675 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