1
+ #!/usr/bin/env python3
2
+ # filepath: count_roc_lines.py
3
+
4
+ import os
5
+ import sys
6
+ from pathlib import Path
7
+
8
+ def count_lines_in_file (file_path ):
9
+ """Count the number of lines in a file."""
10
+ try :
11
+ with open (file_path , 'r' , encoding = 'utf-8' ) as file :
12
+ return len (file .readlines ())
13
+ except Exception as e :
14
+ print (f"Error reading { file_path } : { e } " , file = sys .stderr )
15
+ return 0
16
+
17
+ def main ():
18
+ current_dir = Path ('.' )
19
+ roc_files = list (current_dir .glob ('**/*.roc' ))
20
+
21
+ if not roc_files :
22
+ print ("No .roc files found in the current directory or subdirectories." )
23
+ return
24
+
25
+ total_lines = 0
26
+ results = []
27
+
28
+ for file_path in roc_files :
29
+ line_count = count_lines_in_file (file_path )
30
+ total_lines += line_count
31
+ results .append ((str (file_path ), line_count ))
32
+
33
+ # Sort by line count (descending)
34
+ results .sort (key = lambda x : x [1 ], reverse = True )
35
+
36
+ # Print results in a table format
37
+ print ("\n Lines of code in .roc files:" )
38
+ print ("-" * 60 )
39
+ print (f"{ 'File' :<45} | { 'Lines' :<10} " )
40
+ print ("-" * 60 )
41
+
42
+ for file_path , line_count in results :
43
+ print (f"{ file_path :<45} | { line_count :<10} " )
44
+
45
+ print ("-" * 60 )
46
+ print (f"{ 'Total' :<45} | { total_lines :<10} " )
47
+ print (f"\n Found { len (roc_files )} .roc files with a total of { total_lines } lines" )
48
+
49
+ if __name__ == "__main__" :
50
+ main ()
0 commit comments