11use std:: {
22 fs:: File ,
3- io:: { BufRead , BufReader , Write , stdout} ,
3+ io:: { BufRead , BufReader , BufWriter , Write , stdout} ,
44} ;
55
66use puppyutils:: { Result , cli} ;
@@ -36,6 +36,8 @@ pub fn main() -> Result {
3636 flags = Flags :: LINES | Flags :: WORDS | Flags :: BYTES ;
3737 }
3838
39+ let mut stdout = BufWriter :: new ( stdout) ;
40+
3941 for path in files {
4042 let file = File :: open ( & path) ?;
4143
@@ -48,6 +50,7 @@ pub fn main() -> Result {
4850 let mut reader = BufReader :: new ( file) ;
4951
5052 let mut line = String :: new ( ) ;
53+
5154 loop {
5255 let read_bytes = reader. read_line ( & mut line) ?;
5356
@@ -66,9 +69,46 @@ pub fn main() -> Result {
6669 line. clear ( ) ;
6770 }
6871
69- let out = format ! ( " {lines} {words} {chars} {bytes} {path}\n " ) ; // FIXME: obv we dont wanna format but also this is temporary since we are ignoring the flags
70- stdout. write_all ( out. as_bytes ( ) ) ?;
72+ let mut first = true ;
73+
74+ let mut write_num = |num : usize | -> Result < ( ) > {
75+ if !first {
76+ stdout. write_all ( b" " ) ?;
77+ }
78+ stdout. write_all ( itoa:: Buffer :: new ( ) . format ( num) . as_bytes ( ) ) ?;
79+ first = false ;
80+ Ok ( ( ) )
81+ } ;
82+
83+ if flags. contains ( Flags :: LINES ) {
84+ write_num ( lines) ?;
85+ }
86+
87+ if flags. contains ( Flags :: WORDS ) {
88+ write_num ( words) ?;
89+ }
90+
91+ if flags. contains ( Flags :: CHARS ) {
92+ write_num ( chars) ?;
93+ }
94+
95+ if flags. contains ( Flags :: BYTES ) {
96+ write_num ( bytes) ?;
97+ }
98+
99+ stdout. write_all ( b" " ) ?;
100+ stdout. write_all ( path. as_bytes ( ) ) ?;
101+ stdout. write_all ( b"\n " ) ?;
71102 }
72103
73104 Ok ( ( ) )
74105}
106+
107+ fn _count_padding ( mut n : usize ) -> usize {
108+ let mut count = 0 ;
109+ while n % 10 == 0 {
110+ n /= 10 ;
111+ count += 1 ;
112+ }
113+ count
114+ }
0 commit comments