df: fix incorrect whitespace between columns

Fixes #3194
This commit is contained in:
Daniel Hofstetter
2022-04-17 14:00:52 +02:00
parent 34a1b23342
commit a052855061
4 changed files with 337 additions and 162 deletions
+27
View File
@@ -183,4 +183,31 @@ impl Column {
_ => Err(()),
}
}
/// Return the alignment of the specified column.
pub(crate) fn alignment(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,
}
+7 -20
View File
@@ -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.";
@@ -380,26 +380,13 @@ 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");
// This can happen if paths are given as command-line arguments
// but none of the paths exist.
if filesystems.is_empty() {
return Ok(());
}
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 || 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));
}
println!("{}", Table::new(&opt, filesystems));
Ok(())
}
+257 -117
View File
File diff suppressed because it is too large Load Diff
+46 -25
View File
@@ -29,9 +29,26 @@ fn test_df_compatible_si() {
#[test]
fn test_df_output() {
let expected = if cfg!(target_os = "macos") {
"Filesystem Size Used Available Capacity Use% Mounted on "
vec![
"Filesystem",
"Size",
"Used",
"Available",
"Capacity",
"Use%",
"Mounted",
"on",
]
} else {
"Filesystem Size Used Available Use% Mounted on "
vec![
"Filesystem",
"Size",
"Used",
"Available",
"Use%",
"Mounted",
"on",
]
};
let output = new_ucmd!()
.arg("-H")
@@ -39,6 +56,7 @@ fn test_df_output() {
.succeeds()
.stdout_move_str();
let actual = output.lines().take(1).collect::<Vec<&str>>()[0];
let actual = actual.split_whitespace().collect::<Vec<_>>();
assert_eq!(actual, expected);
}
@@ -253,23 +271,26 @@ fn test_block_size_1024() {
assert_eq!(get_header(34 * 1024 * 1024 * 1024), "34G-blocks");
}
// TODO The spacing does not match GNU df. Also we need to remove
// trailing spaces from the heading row.
#[test]
fn test_output_selects_columns() {
let output = new_ucmd!()
.args(&["--output=source"])
.succeeds()
.stdout_move_str();
assert_eq!(output.lines().next().unwrap(), "Filesystem ");
assert_eq!(output.lines().next().unwrap().trim_end(), "Filesystem");
let output = new_ucmd!()
.args(&["--output=source,target"])
.succeeds()
.stdout_move_str();
assert_eq!(
output.lines().next().unwrap(),
"Filesystem Mounted on "
output
.lines()
.next()
.unwrap()
.split_whitespace()
.collect::<Vec<_>>(),
vec!["Filesystem", "Mounted", "on"]
);
let output = new_ucmd!()
@@ -277,8 +298,13 @@ fn test_output_selects_columns() {
.succeeds()
.stdout_move_str();
assert_eq!(
output.lines().next().unwrap(),
"Filesystem Mounted on Used "
output
.lines()
.next()
.unwrap()
.split_whitespace()
.collect::<Vec<_>>(),
vec!["Filesystem", "Mounted", "on", "Used"]
);
}
@@ -289,12 +315,16 @@ fn test_output_multiple_occurrences() {
.succeeds()
.stdout_move_str();
assert_eq!(
output.lines().next().unwrap(),
"Filesystem Mounted on "
output
.lines()
.next()
.unwrap()
.split_whitespace()
.collect::<Vec<_>>(),
vec!["Filesystem", "Mounted", "on"]
);
}
// TODO Fix the spacing.
#[test]
fn test_output_file_all_filesystems() {
// When run with no positional arguments, `df` lets "-" represent
@@ -304,13 +334,12 @@ fn test_output_file_all_filesystems() {
.succeeds()
.stdout_move_str();
let mut lines = output.lines();
assert_eq!(lines.next().unwrap(), "File ");
assert_eq!(lines.next().unwrap(), "File");
for line in lines {
assert_eq!(line, "- ");
assert_eq!(line, "- ");
}
}
// TODO Fix the spacing.
#[test]
fn test_output_file_specific_files() {
// Create three files.
@@ -326,15 +355,7 @@ fn test_output_file_specific_files() {
.succeeds()
.stdout_move_str();
let actual: Vec<&str> = output.lines().collect();
assert_eq!(
actual,
vec![
"File ",
"a ",
"b ",
"c "
]
);
assert_eq!(actual, vec!["File", "a ", "b ", "c "]);
}
#[test]
@@ -355,5 +376,5 @@ fn test_nonexistent_file() {
.args(&["--output=file", "does-not-exist", "."])
.fails()
.stderr_is("df: does-not-exist: No such file or directory\n")
.stdout_is("File \n. \n");
.stdout_is("File\n. \n");
}