@@ -17,7 +17,6 @@ use uucore::fsext::{FsUsage, MountInfo};
1717use uucore:: locale:: get_message;
1818
1919use std:: ffi:: OsString ;
20- use std:: fmt;
2120use std:: ops:: AddAssign ;
2221
2322/// A row in the filesystem usage data table.
@@ -482,12 +481,9 @@ impl Table {
482481
483482 alignments
484483 }
485- }
486484
487- impl fmt:: Display for Table {
488- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
489- let mut row_iter = self . rows . iter ( ) . peekable ( ) ;
490- while let Some ( row) = row_iter. next ( ) {
485+ pub ( crate ) fn write_to ( & self , writer : & mut dyn std:: io:: Write ) -> std:: io:: Result < ( ) > {
486+ for row in & self . rows {
491487 let mut col_iter = row. iter ( ) . enumerate ( ) . peekable ( ) ;
492488 while let Some ( ( i, elem) ) = col_iter. next ( ) {
493489 // TODO: Fix this, and print the bytes directly.
@@ -498,26 +494,24 @@ impl fmt::Display for Table {
498494 Some ( Alignment :: Left ) => {
499495 if is_last_col {
500496 // no trailing spaces in last column
501- write ! ( f , "{elem}" ) ?;
497+ write ! ( writer , "{elem}" ) ?;
502498 } else {
503- write ! ( f , "{elem:<width$}" , width = self . widths[ i] ) ?;
499+ write ! ( writer , "{elem:<width$}" , width = self . widths[ i] ) ?;
504500 }
505501 }
506502 Some ( Alignment :: Right ) => {
507- write ! ( f , "{elem:>width$}" , width = self . widths[ i] ) ?;
503+ write ! ( writer , "{elem:>width$}" , width = self . widths[ i] ) ?;
508504 }
509505 None => break ,
510506 }
511507
512508 if !is_last_col {
513509 // column separator
514- write ! ( f , " " ) ?;
510+ write ! ( writer , " " ) ?;
515511 }
516512 }
517513
518- if row_iter. peek ( ) . is_some ( ) {
519- writeln ! ( f) ?;
520- }
514+ writeln ! ( writer) ?;
521515 }
522516
523517 Ok ( ( ) )
@@ -996,22 +990,30 @@ mod tests {
996990 } ;
997991
998992 let table_w_total = Table :: new ( & options, filesystems. clone ( ) ) ;
993+ let mut data_w_total: Vec < u8 > = vec ! [ ] ;
994+ table_w_total
995+ . write_to ( & mut data_w_total)
996+ . expect ( "Write error." ) ;
999997 assert_eq ! (
1000- table_w_total . to_string ( ) ,
998+ String :: from_utf8_lossy ( & data_w_total ) ,
1001999 "Filesystem Inodes IUsed IFree\n \
10021000 none 99999999999 99999000000 999999\n \
10031001 none 99999999999 99999000000 999999\n \
1004- total 199999999998 199998000000 1999998"
1002+ total 199999999998 199998000000 1999998\n "
10051003 ) ;
10061004
10071005 options. show_total = false ;
10081006
10091007 let table_w_o_total = Table :: new ( & options, filesystems) ;
1008+ let mut data_w_o_total: Vec < u8 > = vec ! [ ] ;
1009+ table_w_o_total
1010+ . write_to ( & mut data_w_o_total)
1011+ . expect ( "Write error." ) ;
10101012 assert_eq ! (
1011- table_w_o_total . to_string ( ) ,
1013+ String :: from_utf8_lossy ( & data_w_o_total ) ,
10121014 "Filesystem Inodes IUsed IFree\n \
10131015 none 99999999999 99999000000 999999\n \
1014- none 99999999999 99999000000 999999"
1016+ none 99999999999 99999000000 999999\n "
10151017 ) ;
10161018 }
10171019
0 commit comments