From 6371eb8298b5dfaa9a18fcdb25b6affc7f567f04 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Sun, 13 Mar 2022 14:42:15 -0400 Subject: [PATCH] df: use for loop over filesystems to display rows Replace a loop over `Row` objects with a loop over `Filesystem` objects when it comes time to display each row of the output table. This helps with code structure, since the `Filesystem` is the primary object of interest in the `df` program. This makes it easier for us to independently vary how the list of filesystems is produced and how the list of filesystems is displayed. --- src/uu/df/src/df.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index b3244ff18..8f81b3efe 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -282,18 +282,29 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // Get Point for each input_path get_point_list(&mounts, &paths) }; - let data: Vec = op_mount_points + + // Get the list of filesystems to display in the output table. + let filesystems: Vec = op_mount_points .into_iter() .filter_map(Filesystem::new) - .filter(|fs| fs.usage.blocks != 0 || opt.show_all_fs || opt.show_listed_fs) - .map(Into::into) .collect(); - println!("{}", Header::new(&opt)); + // The running total of filesystem sizes and usage. + // + // This accumulator is computed in case we need to display the + // total counts in the last row of the table. let mut total = Row::new("total"); - for row in data { - println!("{}", DisplayRow::new(&row, &opt)); - total += row; + + println!("{}", Header::new(&opt)); + for filesystem in filesystems { + // If the filesystem is not empty, or if the options require + // showing all filesystems, then print the data as a row in + // the output table. + if opt.show_all_fs || opt.show_listed_fs || filesystem.usage.blocks > 0 { + let row = Row::from(filesystem); + println!("{}", DisplayRow::new(&row, &opt)); + total += row; + } } if opt.show_total { println!("{}", DisplayRow::new(&total, &opt));