1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-15 11:36:16 +00:00

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.
This commit is contained in:
Jeffrey Finkelstein 2022-03-13 14:42:15 -04:00
parent fa6af85b8e
commit 6371eb8298

View file

@ -282,19 +282,30 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// Get Point for each input_path // Get Point for each input_path
get_point_list(&mounts, &paths) get_point_list(&mounts, &paths)
}; };
let data: Vec<Row> = op_mount_points
// Get the list of filesystems to display in the output table.
let filesystems: Vec<Filesystem> = op_mount_points
.into_iter() .into_iter()
.filter_map(Filesystem::new) .filter_map(Filesystem::new)
.filter(|fs| fs.usage.blocks != 0 || opt.show_all_fs || opt.show_listed_fs)
.map(Into::into)
.collect(); .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"); let mut total = Row::new("total");
for row in data {
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)); println!("{}", DisplayRow::new(&row, &opt));
total += row; total += row;
} }
}
if opt.show_total { if opt.show_total {
println!("{}", DisplayRow::new(&total, &opt)); println!("{}", DisplayRow::new(&total, &opt));
} }