@@ -15,7 +15,14 @@ program.parse(process.argv);
1515const options = program . opts ( ) ;
1616const files = program . args . length ? program . args : [ "/dev/stdin" ] ;
1717
18- function countFile ( filePath , options ) {
18+ // Determine active counts
19+ const activeCounts = {
20+ lines : options . lines || ( ! options . words && ! options . chars ) ,
21+ words : options . words || ( ! options . lines && ! options . chars ) ,
22+ chars : options . chars || ( ! options . lines && ! options . words ) ,
23+ } ;
24+
25+ function countFile ( filePath ) {
1926 let content = "" ;
2027 try {
2128 if ( filePath === "/dev/stdin" ) {
@@ -38,18 +45,26 @@ function countFile(filePath, options) {
3845
3946 return {
4047 file : filePath ,
41- lines : options . lines || ( ! options . words && ! options . chars ) ? lineCount : null ,
42- words : options . words || ( ! options . lines && ! options . chars ) ? wordCount : null ,
43- chars : options . chars || ( ! options . lines && ! options . words ) ? charCount : null ,
48+ lines : activeCounts . lines ? lineCount : null ,
49+ words : activeCounts . words ? wordCount : null ,
50+ chars : activeCounts . chars ? charCount : null ,
4451 } ;
4552}
4653
54+ function formatCounts ( result ) {
55+ const output = [ ] ;
56+ if ( result . lines !== null ) output . push ( result . lines . toString ( ) . padStart ( 8 ) ) ;
57+ if ( result . words !== null ) output . push ( result . words . toString ( ) . padStart ( 8 ) ) ;
58+ if ( result . chars !== null ) output . push ( result . chars . toString ( ) . padStart ( 8 ) ) ;
59+ return output . join ( " " ) ;
60+ }
61+
4762const results = [ ] ;
4863let totalLines = 0 , totalWords = 0 , totalChars = 0 ;
4964const hasMultipleFiles = files . length > 1 ;
5065
5166for ( const file of files ) {
52- const result = countFile ( file , options ) ;
67+ const result = countFile ( file ) ;
5368 if ( result ) {
5469 results . push ( result ) ;
5570 if ( result . lines !== null ) totalLines += result . lines ;
@@ -58,18 +73,16 @@ for (const file of files) {
5873 }
5974}
6075
61- results . forEach ( result => {
62- const output = [ ] ;
63- if ( result . lines !== null ) output . push ( result . lines . toString ( ) . padStart ( 8 ) ) ;
64- if ( result . words !== null ) output . push ( result . words . toString ( ) . padStart ( 8 ) ) ;
65- if ( result . chars !== null ) output . push ( result . chars . toString ( ) . padStart ( 8 ) ) ;
66- console . log ( output . join ( " " ) , result . file ) ;
67- } ) ;
76+ // Print per-file results
77+ results . forEach ( result => console . log ( `${ formatCounts ( result ) } ${ result . file } ` ) ) ;
6878
79+ // Print totals if more than one file
6980if ( hasMultipleFiles && results . length > 0 ) {
70- const totalOutput = [ ] ;
71- if ( options . lines || ( ! options . words && ! options . chars ) ) totalOutput . push ( totalLines . toString ( ) . padStart ( 8 ) ) ;
72- if ( options . words || ( ! options . lines && ! options . chars ) ) totalOutput . push ( totalWords . toString ( ) . padStart ( 8 ) ) ;
73- if ( options . chars || ( ! options . lines && ! options . words ) ) totalOutput . push ( totalChars . toString ( ) . padStart ( 8 ) ) ;
74- console . log ( totalOutput . join ( " " ) , "total" ) ;
81+ const total = {
82+ file : "total" ,
83+ lines : activeCounts . lines ? totalLines : null ,
84+ words : activeCounts . words ? totalWords : null ,
85+ chars : activeCounts . chars ? totalChars : null ,
86+ } ;
87+ console . log ( `${ formatCounts ( total ) } total` ) ;
7588}
0 commit comments