File tree Expand file tree Collapse file tree
implement-shell-tools/cat Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1414number_nonblank = args .number_nonblank
1515number_all = args .number and not number_nonblank
1616
17+
18+ total_lines = 0
19+ total_nonblank_lines = 0
20+
21+ for path in file_paths :
22+ try :
23+ with open (path , 'r' , encoding = 'utf-8' ) as f :
24+ lines = f .readlines ()
25+ total_lines += len (lines )
26+ total_nonblank_lines += sum (1 for line in lines if line .strip ())
27+ except Exception as e :
28+ print (f'Error reading file "{ path } ": { e } ' , file = sys .stderr )
29+ sys .exit (1 )
30+
31+
32+ if number_nonblank :
33+ max_digits = len (str (total_nonblank_lines ))
34+ elif number_all :
35+ max_digits = len (str (total_lines ))
36+
1737line_number = 1
1838
39+
1940for path in file_paths :
2041 try :
2142 with open (path , 'r' , encoding = 'utf-8' ) as f :
2243 lines = f .readlines ()
2344
2445 if number_nonblank :
25- nonblank_lines = [line for line in lines if line .strip ()]
26- max_digits = len (str (len (nonblank_lines )))
27-
2846 for line in lines :
2947 if line .strip () == "" :
3048 print ()
3452 line_number += 1
3553
3654 elif number_all :
37- max_digits = len (str (len (lines ) * len (file_paths )))
38-
3955 for line in lines :
4056 num_str = str (line_number ).rjust (max_digits )
4157 print (f"{ num_str } \t { line .rstrip ()} " )
4460 else :
4561 for line in lines :
4662 print (line , end = '' )
47- if not lines [- 1 ].endswith ('\n ' ):
63+ if lines and not lines [- 1 ].endswith ('\n ' ):
4864 print ()
4965
5066 except Exception as e :
You can’t perform that action at this time.
0 commit comments