Skip to content

Commit

Permalink
df: fix incorrect whitespace between columns
Browse files Browse the repository at this point in the history
Fixes #3194
  • Loading branch information
cakebaker committed Apr 15, 2022
1 parent f03ab63 commit df78d86
Show file tree
Hide file tree
Showing 4 changed files with 335 additions and 160 deletions.
27 changes: 27 additions & 0 deletions src/uu/df/src/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,31 @@ impl Column {
_ => Err(()),
}
}

/// Return the alignment of the specified column.
pub(crate) fn align(column: &Self) -> Alignment {
match column {
Self::Source | Self::Target | Self::File | Self::Fstype => Alignment::Left,
_ => Alignment::Right,
}
}

/// Return the minimum width of the specified column.
pub(crate) fn min_width(column: &Self) -> usize {
match column {
// 14 = length of "Filesystem" plus 4 spaces
Self::Source => 14,
// the shortest headers have a length of 4 chars so we use that as the minimum width
_ => 4,
}
}
}

/// A column's alignment.
///
/// We define our own `Alignment` enum instead of using `std::fmt::Alignment` because df doesn't
/// have centered columns and hence a `Center` variant is not needed.
pub(crate) enum Alignment {
Left,
Right,
}
27 changes: 7 additions & 20 deletions src/uu/df/src/df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::path::Path;
use crate::blocks::{block_size_from_matches, BlockSize};
use crate::columns::{Column, ColumnError};
use crate::filesystem::Filesystem;
use crate::table::{DisplayRow, Header, Row};
use crate::table::Table;

static ABOUT: &str = "Show information about the file system on which each FILE resides,\n\
or all file systems by default.";
Expand Down Expand Up @@ -383,27 +383,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}
};

// 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");

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));
// This can happen if paths are given as command-line arguments
// but none of the paths exist.
if filesystems.is_empty() {
return Ok(());
}

println!("{}", Table::new(&opt, filesystems));

Ok(())
}

Expand Down
Loading

0 comments on commit df78d86

Please sign in to comment.