22import rich .table
33
44from .state import ARG
5- from .common import MFC_ROOTDIR , format_list_to_string
5+ from .common import MFC_ROOTDIR , format_list_to_string , MFCException
66from .printer import cons
77
88def handle_dir (dirpath : str ) -> typing .Tuple [typing .List [typing .Tuple [str , int ]], int ]:
@@ -11,9 +11,18 @@ def handle_dir(dirpath: str) -> typing.Tuple[typing.List[typing.Tuple[str, int]]
1111
1212 for filepath in glob .glob (os .path .join (dirpath , '*.*f*' )):
1313 with open (filepath ) as f :
14- n = sum (1 if not l .isspace () else 0 for l in f .read ().split ('\n ' ))
15- files .append ((filepath , count ))
16- total += n
14+ counter = 0
15+ for l in f .read ().split ('\n ' ):
16+ # Skip whitespace
17+ if l .isspace () or len (l ) == 0 :
18+ continue
19+ # Skip comments but not !$acc ones!
20+ if l .lstrip ().startswith ("!" ) and not l .lstrip ().startswith ("!$acc" ):
21+ continue
22+ counter += 1
23+
24+ files .append ((filepath , counter ))
25+ total += counter
1726
1827 files .sort (key = lambda x : x [1 ], reverse = True )
1928
@@ -42,3 +51,54 @@ def count():
4251 cons .print (f"[bold]Total { target_str_list } lines: [bold cyan]{ total } [/bold cyan].[/bold]" )
4352 cons .print ()
4453 cons .unindent ()
54+
55+ # pylint: disable=too-many-locals
56+ def count_diff ():
57+ target_str_list = format_list_to_string (ARG ('targets' ), 'magenta' )
58+ cons .print (f"[bold]Counting lines of code in { target_str_list } [/bold] (excluding whitespace lines)" )
59+ cons .indent ()
60+
61+ total = 0
62+ MFC_COMPAREDIR = os .getenv ('MFC_PR' )
63+ if MFC_COMPAREDIR is None :
64+ raise MFCException ("MFC_PR is not in your environment." )
65+
66+ print ('compare dir' , MFC_COMPAREDIR )
67+
68+ # MFC_COMPAREDIR="/Users/spencer/Downloads/MFC-shbfork"
69+ for codedir in ['common' ] + ARG ("targets" ):
70+ dirfiles_root , dircount = handle_dir (os .path .join (MFC_ROOTDIR , 'src' , codedir ))
71+ dirfiles_pr , dircount_pr = handle_dir (os .path .join (MFC_COMPAREDIR , 'src' , codedir ))
72+ table = rich .table .Table (show_header = True , box = rich .table .box .SIMPLE )
73+ table .add_column (f"File (in [magenta]{ codedir } [/magenta])" , justify = "left" )
74+ table .add_column (f"Lines [HEAD] ([cyan]{ dircount } [/cyan])" , justify = "right" )
75+ table .add_column (f"Lines [PR] ([cyan]{ dircount_pr } [/cyan])" , justify = "right" )
76+ table .add_column ("" , justify = "right" )
77+ table .add_column ("Diff" , justify = "right" )
78+
79+ ii = 0
80+ files_pr = [os .path .basename (dirfiles_pr [i ][0 ]) for i in range (len (dirfiles_pr ))]
81+ files_root = [os .path .basename (dirfiles_root [i ][0 ]) for i in range (len (dirfiles_root ))]
82+
83+ PLUS = "++ "
84+ MINUS = "-- "
85+
86+ for filepath , n in dirfiles_pr :
87+ for filepath_root , _ in dirfiles_root :
88+ if os .path .basename (dirfiles_pr [ii ][0 ]) == os .path .basename (filepath_root ):
89+ diff_count = n - dirfiles_root [ii ][1 ]
90+ mycolor = "red" if diff_count > 0 else "green"
91+ mysymbol = PLUS if diff_count > 0 else MINUS
92+ table .add_row (f"{ os .path .basename (filepath )} " , f"[bold cyan]{ n } [/bold cyan]" , f"[bold cyan]{ dirfiles_root [ii ][1 ]} [/bold cyan]" , mysymbol , f"[bold { mycolor } ]{ diff_count } [/bold { mycolor } ]" )
93+
94+ if files_pr [ii ] not in files_root :
95+ table .add_row (f"{ os .path .basename (files_pr [ii ])} " , "----" , f"[bold green]{ n } [/bold green]" , PLUS , f"[bold green]{ n } [/bold green]" )
96+ ii += 1
97+
98+ total += dircount
99+
100+ cons .raw .print (table )
101+
102+ cons .print (f"[bold]Total { target_str_list } lines: [bold cyan]{ total } [/bold cyan].[/bold]" )
103+ cons .print ()
104+ cons .unindent ()
0 commit comments