@@ -25,26 +25,39 @@ def read_gitignore(path):
25
25
return []
26
26
27
27
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 ):
29
38
if xml :
30
- print_as_xml (writer , path , content )
39
+ print_as_xml (writer , path , content , line_numbers )
31
40
else :
32
- print_default (writer , path , content )
41
+ print_default (writer , path , content , line_numbers )
33
42
34
43
35
- def print_default (writer , path , content ):
44
+ def print_default (writer , path , content , line_numbers ):
36
45
writer (path )
37
46
writer ("---" )
47
+ if line_numbers :
48
+ content = add_line_numbers (content )
38
49
writer (content )
39
50
writer ("" )
40
51
writer ("---" )
41
52
42
53
43
- def print_as_xml (writer , path , content ):
54
+ def print_as_xml (writer , path , content , line_numbers ):
44
55
global global_index
45
56
writer (f'<document index="{ global_index } ">' )
46
57
writer (f"<source>{ path } </source>" )
47
58
writer ("<document_content>" )
59
+ if line_numbers :
60
+ content = add_line_numbers (content )
48
61
writer (content )
49
62
writer ("</document_content>" )
50
63
writer ("</document>" )
@@ -60,11 +73,12 @@ def process_path(
60
73
ignore_patterns ,
61
74
writer ,
62
75
claude_xml ,
76
+ line_numbers = False ,
63
77
):
64
78
if os .path .isfile (path ):
65
79
try :
66
80
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 )
68
82
except UnicodeDecodeError :
69
83
warning_message = f"Warning: Skipping file { path } due to UnicodeDecodeError"
70
84
click .echo (click .style (warning_message , fg = "red" ), err = True )
@@ -101,7 +115,9 @@ def process_path(
101
115
file_path = os .path .join (root , file )
102
116
try :
103
117
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
+ )
105
121
except UnicodeDecodeError :
106
122
warning_message = (
107
123
f"Warning: Skipping file { file_path } due to UnicodeDecodeError"
@@ -143,6 +159,13 @@ def process_path(
143
159
is_flag = True ,
144
160
help = "Output in XML-ish format suitable for Claude's long context window." ,
145
161
)
162
+ @click .option (
163
+ "line_numbers" ,
164
+ "-n" ,
165
+ "--line-numbers" ,
166
+ is_flag = True ,
167
+ help = "Add line numbers to the output" ,
168
+ )
146
169
@click .version_option ()
147
170
def cli (
148
171
paths ,
@@ -152,6 +175,7 @@ def cli(
152
175
ignore_patterns ,
153
176
output_file ,
154
177
claude_xml ,
178
+ line_numbers ,
155
179
):
156
180
"""
157
181
Takes one or more paths to files or directories and outputs every file,
@@ -204,6 +228,7 @@ def cli(
204
228
ignore_patterns ,
205
229
writer ,
206
230
claude_xml ,
231
+ line_numbers ,
207
232
)
208
233
if claude_xml :
209
234
writer ("</documents>" )
0 commit comments