@@ -5,7 +5,6 @@ extern crate clap;
55
66use  clap:: { Arg ,  App } ; 
77
8- 
98// FileStats holds information about an individual file 
109struct  FileStats  { 
1110 name :  String , 
@@ -17,15 +16,23 @@ struct FileStats {
1716// WcStats holds information for all the files 
1817struct  WcStats  { 
1918 stats :  Vec < FileStats > , 
19+  number_of_files :  usize , 
2020 total :  FileStats , 
21+  line_flag :  bool , 
22+  word_flag :  bool , 
23+  char_flag :  bool , 
2124} 
2225
2326impl  WcStats  { 
2427 // new creates an emtpy WcStats instance 
2528 fn  new ( )  -> WcStats  { 
2629 WcStats  { 
2730 stats :  Vec :: new ( ) , 
31+  number_of_files :  0 , 
2832 total :  FileStats { name :  String :: from ( "" ) ,  lines :  0 ,  words :  0 ,  characters :  0 } , 
33+  line_flag :  true , 
34+  word_flag :  true , 
35+  char_flag :  true , 
2936 } 
3037 } 
3138
@@ -48,24 +55,105 @@ impl WcStats {
4855
4956 // prints resutls to the console 
5057 fn  display ( self )  { 
58+  if  ( self . line_flag  && self . word_flag  && self . char_flag )  || !( self . line_flag  || self . word_flag  || self . char_flag )  { 
59+  self . print_all ( ) ; 
60+  }  else  if  self . line_flag  == true  && !( self . word_flag  || self . char_flag )  { 
61+  self . print_lines ( ) ; 
62+  }  else  if  self . word_flag  == true  && !( self . line_flag  || self . char_flag )  { 
63+  self . print_words ( ) ; 
64+  }  else  if  self . char_flag  == true  && !( self . word_flag  || self . line_flag )  { 
65+  self . print_chars ( ) ; 
66+  }  else  if  self . line_flag  && self . word_flag  && self . char_flag  == false  { 
67+  self . print_lines_and_words ( ) ; 
68+  }  else  if  self . line_flag  && self . char_flag  && self . word_flag  == false  { 
69+  self . print_lines_and_chars ( ) ; 
70+  }  else  { 
71+  self . print_words_and_chars ( ) ; 
72+  } 
73+  } 
74+ 
75+  fn  print_all ( self )  { 
76+  for  stat in  self . stats  { 
77+  println ! ( "{}\t {}\t {}\t {}" ,  stat. lines,  stat. words,  stat. characters,  stat. name) ; 
78+  } 
79+ 
80+  if  self . number_of_files  > 1  { 
81+  println ! ( "{}\t {}\t {}\t total" ,  self . total. lines,  self . total. words,  self . total. characters) ; 
82+  } 
83+  } 
84+ 
85+  fn  print_lines_and_words ( self )  { 
86+  for  stat in  self . stats  { 
87+  println ! ( "{}\t {}\t {}" ,  stat. lines,  stat. words,  stat. name) ; 
88+  } 
89+ 
90+  if  self . number_of_files  > 1  { 
91+  println ! ( "{}\t {}\t total" ,  self . total. lines,  self . total. words) ; 
92+  } 
93+  } 
94+ 
95+  fn  print_lines_and_chars ( self )  { 
5196 for  stat in  self . stats  { 
52-  println ! ( "{} {} {} {}" ,  stat. lines,  stat. words,  stat. characters,  stat. name) ; 
97+  println ! ( "{}\t {}\t {}" ,  stat. lines,  stat. characters,  stat. name) ; 
98+  } 
99+ 
100+  if  self . number_of_files  > 1  { 
101+  println ! ( "{}\t {}\t total" ,  self . total. lines,  self . total. characters) ; 
102+  } 
103+  } 
104+ 
105+  fn  print_words_and_chars ( self )  { 
106+  for  stat in  self . stats  { 
107+  println ! ( "{}\t {}\t {}" ,  stat. words,  stat. characters,  stat. name) ; 
108+  } 
109+ 
110+  if  self . number_of_files  > 1  { 
111+  println ! ( "{}\t {}\t total" ,  self . total. words,  self . total. characters) ; 
112+  } 
113+  } 
114+ 
115+  fn  print_lines ( self )  { 
116+  for  stat in  self . stats  { 
117+  println ! ( "{}\t {}" ,  stat. lines,  stat. name) ; 
118+  } 
119+ 
120+  if  self . number_of_files  > 1  { 
121+  println ! ( "{}\t total" ,  self . total. lines) ; 
122+  } 
123+  } 
124+ 
125+  fn  print_words ( self )  { 
126+  for  stat in  self . stats  { 
127+  println ! ( "{}\t {}" ,  stat. words,  stat. name) ; 
128+  } 
129+ 
130+  if  self . number_of_files  > 1  { 
131+  println ! ( "{}\t total" ,  self . total. words) ; 
132+  } 
133+  } 
134+ 
135+  fn  print_chars ( self )  { 
136+  for  stat in  self . stats  { 
137+  println ! ( "{}\t {}" ,  stat. characters,  stat. name) ; 
138+  } 
139+ 
140+  if  self . number_of_files  > 1  { 
141+  println ! ( "{}\t total" ,  self . total. characters) ; 
53142 } 
54143 } 
55144} 
56145
57146/* 
58147TODOs: 
59- - Evenly spaced columns in the output 
60- - Read stdin with no file 
61- - Listen for ctrl+d in case of stdin 
148+ - Read stdin with no file & listen for ctrl+d in case of stdin 
62149- Emit a warning in case of dirs but don't fail "wc: benches: Is a directory" 
63150- Add long help if needed 
64151- docs/comments 
152+ - add line lenght flag L 
153+ - add bytes flag c 
65154
66155Ehancements: 
67- - Add color to the output 
68- - Themes? 
156+ - Add color to the output, themes maybe? 
69157- Add a recursive call to process files under directories 
70158*/ 
71159fn  main ( )  { 
@@ -78,7 +166,7 @@ fn main() {
78166 . long ( "lines" ) 
79167 . help ( "prints the newline counts" ) ) 
80168 . arg ( Arg :: with_name ( "words" ) 
81-  . short ( "W " ) 
169+  . short ( "w " ) 
82170 . long ( "words" ) 
83171 . help ( "print the words counts" ) ) 
84172 . arg ( Arg :: with_name ( "chars" ) 
@@ -91,13 +179,18 @@ fn main() {
91179 . empty_values ( false ) ) 
92180 . get_matches ( ) ; 
93181
94-  let  mut  stats = WcStats :: new ( ) ; 
182+  let  mut  wc = WcStats :: new ( ) ; 
183+  wc. line_flag  = matches. is_present ( "lines" ) ; 
184+  wc. word_flag  = matches. is_present ( "words" ) ; 
185+  wc. char_flag  = matches. is_present ( "chars" ) ; 
186+ 
95187 if  matches. is_present ( "FILE" )  { 
96188 let  file_path_vec:  Vec < & str >  = matches. values_of ( "FILE" ) . unwrap ( ) . collect ( ) ; 
97189 for  path in  file_path_vec { 
98-  stats . get_stats ( & String :: from ( path) ) ; 
190+   wc . get_stats ( & String :: from ( path) ) ; 
99191 } 
100-  stats. display ( ) ; 
192+  wc. number_of_files  = wc. stats . len ( ) ; 
193+  wc. display ( ) ; 
101194 }  else  { 
102195 println ! ( "Switch on stdin" ) ; 
103196 } 
0 commit comments