@@ -25,26 +25,39 @@ def read_gitignore(path):
2525 return []
2626
2727
28- def print_path (writer , path , content , xml ):
28+ def add_line_numbers (content ):
29+ lines = content .splitlines ()
30+
31+ padding = len (str (len (lines )))
32+
33+ numbered_lines = [f"{ i + 1 :{padding }} { line } " for i , line in enumerate (lines )]
34+ return "\n " .join (numbered_lines )
35+
36+
37+ def print_path (writer , path , content , xml , line_numbers ):
2938 if xml :
30- print_as_xml (writer , path , content )
39+ print_as_xml (writer , path , content , line_numbers )
3140 else :
32- print_default (writer , path , content )
41+ print_default (writer , path , content , line_numbers )
3342
3443
35- def print_default (writer , path , content ):
44+ def print_default (writer , path , content , line_numbers ):
3645 writer (path )
3746 writer ("---" )
47+ if line_numbers :
48+ content = add_line_numbers (content )
3849 writer (content )
3950 writer ("" )
4051 writer ("---" )
4152
4253
43- def print_as_xml (writer , path , content ):
54+ def print_as_xml (writer , path , content , line_numbers ):
4455 global global_index
4556 writer (f'<document index="{ global_index } ">' )
4657 writer (f"<source>{ path } </source>" )
4758 writer ("<document_content>" )
59+ if line_numbers :
60+ content = add_line_numbers (content )
4861 writer (content )
4962 writer ("</document_content>" )
5063 writer ("</document>" )
@@ -60,11 +73,12 @@ def process_path(
6073 ignore_patterns ,
6174 writer ,
6275 claude_xml ,
76+ line_numbers = False ,
6377):
6478 if os .path .isfile (path ):
6579 try :
6680 with open (path , "r" ) as f :
67- print_path (writer , path , f .read (), claude_xml )
81+ print_path (writer , path , f .read (), claude_xml , line_numbers )
6882 except UnicodeDecodeError :
6983 warning_message = f"Warning: Skipping file { path } due to UnicodeDecodeError"
7084 click .echo (click .style (warning_message , fg = "red" ), err = True )
@@ -101,7 +115,9 @@ def process_path(
101115 file_path = os .path .join (root , file )
102116 try :
103117 with open (file_path , "r" ) as f :
104- print_path (writer , file_path , f .read (), claude_xml )
118+ print_path (
119+ writer , file_path , f .read (), claude_xml , line_numbers
120+ )
105121 except UnicodeDecodeError :
106122 warning_message = (
107123 f"Warning: Skipping file { file_path } due to UnicodeDecodeError"
@@ -143,6 +159,13 @@ def process_path(
143159 is_flag = True ,
144160 help = "Output in XML-ish format suitable for Claude's long context window." ,
145161)
162+ @click .option (
163+ "line_numbers" ,
164+ "-n" ,
165+ "--line-numbers" ,
166+ is_flag = True ,
167+ help = "Add line numbers to the output" ,
168+ )
146169@click .version_option ()
147170def cli (
148171 paths ,
@@ -152,6 +175,7 @@ def cli(
152175 ignore_patterns ,
153176 output_file ,
154177 claude_xml ,
178+ line_numbers ,
155179):
156180 """
157181 Takes one or more paths to files or directories and outputs every file,
@@ -204,6 +228,7 @@ def cli(
204228 ignore_patterns ,
205229 writer ,
206230 claude_xml ,
231+ line_numbers ,
207232 )
208233 if claude_xml :
209234 writer ("</documents>" )
0 commit comments