Skip to content

Commit 18180f8

Browse files
committed
Add --levels/-L option
Signed-off-by: John Pennycook <[email protected]>
1 parent c395e80 commit 18180f8

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

codebasin/report.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,11 @@ def insert(
649649
def _print(
650650
self,
651651
node: Node,
652+
depth: int = 0,
652653
prefix: str = "",
653654
connector: str = "",
654655
fancy: bool = True,
656+
levels: int = None,
655657
):
656658
"""
657659
Recursive helper function to print all nodes in a FileTree.
@@ -669,7 +671,14 @@ def _print(
669671
670672
fancy: bool, default: True
671673
Whether to use fancy formatting (including colors).
674+
675+
levels: int, optional
676+
The maximum number of levels to print.
672677
"""
678+
# Skip this node and its children if we have hit the maximum depth.
679+
if levels and depth > levels:
680+
return []
681+
673682
if fancy:
674683
dash = "\u2500"
675684
cont = "\u251C"
@@ -722,23 +731,29 @@ def _print(
722731
next_connector = cont
723732
lines += self._print(
724733
node.children[name],
734+
depth + 1,
725735
next_prefix,
726736
next_connector,
727737
fancy,
738+
levels,
728739
)
729740

730741
return lines
731742

732-
def write_to(self, stream: TextIO):
743+
def write_to(self, stream: TextIO, levels: int = None):
733744
"""
734745
Write the FileTree to the specified stream.
735746
736747
Parameters
737748
----------
738749
stream: TextIO
739750
The text stream to write to.
751+
752+
levels: int, optional
753+
The maximum number of levels to print.
754+
If 0, print only the top-level summary.
740755
"""
741-
lines = self._print(self.root, fancy=stream.isatty())
756+
lines = self._print(self.root, fancy=stream.isatty(), levels=levels)
742757
output = "\n".join(lines)
743758
if not stream.isatty():
744759
output = _strip_colors(output)
@@ -751,6 +766,7 @@ def files(
751766
*,
752767
stream: TextIO = sys.stdout,
753768
prune: bool = False,
769+
levels: int = None,
754770
):
755771
"""
756772
Produce a file tree representing the code base.
@@ -815,4 +831,4 @@ def files(
815831
print(legend, file=stream)
816832

817833
# Print the tree.
818-
tree.write_to(stream)
834+
tree.write_to(stream, levels=levels)

codebasin/tree.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ def _build_parser() -> argparse.ArgumentParser:
7070
help=_help_string(
7171
"Prune unused files from the tree.",
7272
is_long=True,
73+
),
74+
)
75+
parser.add_argument(
76+
"-L",
77+
"--levels",
78+
dest="levels",
79+
metavar="<level>",
80+
type=int,
81+
help=_help_string(
82+
"Print only the specified number of levels.",
83+
is_long=True,
7384
is_last=True,
7485
),
7586
)
@@ -88,6 +99,10 @@ def _build_parser() -> argparse.ArgumentParser:
8899

89100

90101
def _tree(args: argparse.Namespace):
102+
# Refuse to print a tree with no levels, consistent with tree utility.
103+
if args.levels is not None and args.levels <= 0:
104+
raise ValueError("Number of levels must be greater than 0.")
105+
91106
# TODO: Refactor this to avoid duplication in __main__
92107
# Determine the root directory based on where codebasin is run.
93108
rootdir = os.path.abspath(os.getcwd())
@@ -140,7 +155,7 @@ def _tree(args: argparse.Namespace):
140155
)
141156

142157
# Print the file tree.
143-
report.files(codebase, state, prune=args.prune)
158+
report.files(codebase, state, prune=args.prune, levels=args.levels)
144159
sys.exit(0)
145160

146161

0 commit comments

Comments
 (0)