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

Function printFileTree

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

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