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 @@ -18,22 +18,26 @@ for (const arg of args) {
1818 files . push ( arg ) ;
1919 }
2020}
21+ let count = 1 ;
2122
2223//Process each files
23- args . forEach ( ( file ) => {
24+ files . forEach ( ( file ) => {
2425 const content = fs . readFileSync ( file , "utf-8" ) ;
2526 const lines = content . split ( / \r ? \n / ) ;
26-
27- let count = 1 ;
27+ if ( lines [ lines . length - 1 ] == "" ) { lines . pop ( ) ;
28+ }
2829
2930 lines . forEach ( ( line ) => {
30- const shouldNumber = numberLines || ( numberNonEmpty && line !== "" ) ;
31-
32- if ( shouldNumber ) {
33- console . log ( `${ count } ${ line } ` ) ;
31+ const shouldNumber = numberNonEmpty ? line !== "" : numberLines ;
32+
33+ if ( numberNonEmpty && line === "" ) {
34+ process . stdout . write ( "\n" ) ;
35+ } else if ( shouldNumber ) {
36+ process . stdout . write ( `${ String ( count ) . padStart ( 6 ) } ${ line } \n` ) ;
3437 count ++ ;
3538 } else {
36- console . log ( line ) ;
37- }
39+ process . stdout . write ( `${ line } \n` ) ;
40+ }
41+
3842 } ) ;
3943} ) ;
You can’t perform that action at this time.
0 commit comments