Skip to content

Commit

Permalink
Compute the total if there are more than one paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 1, 2019
1 parent 7dc718b commit 04ce0c9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
28 changes: 22 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ mod aggregate {
paths: impl IntoIterator<Item = impl AsRef<Path>>,
) -> Result<WalkResult, Error> {
let mut res = WalkResult::default();
let mut total = 0;
let mut num_roots = 0;
for path in paths.into_iter() {
num_roots += 1;
let mut num_bytes = 0u64;
for entry in options.iter_from_path(path.as_ref()) {
match entry {
Expand All @@ -81,15 +84,28 @@ mod aggregate {
}
}

writeln!(
out,
"{}\t{}",
options.format_bytes(num_bytes),
path.as_ref().display()
)?;
write_path(&mut out, &options, path, num_bytes)?;
total += num_bytes;
}
if num_roots > 1 {
write_path(&mut out, &options, Path::new("<TOTAL>"), total)?;
}
Ok(res)
}

fn write_path(
out: &mut impl io::Write,
options: &WalkOptions,
path: impl AsRef<Path>,
num_bytes: u64,
) -> Result<(), io::Error> {
writeln!(
out,
"{}\t{}",
options.format_bytes(num_bytes),
path.as_ref().display()
)
}
}

pub use aggregate::aggregate;
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mod options {

#[derive(Debug, StructOpt)]
pub enum Command {
/// Aggregrate the consumed space of one or more directories or files
#[structopt(name = "aggregate", alias = "a")]
Aggregate {
/// One or more input files. If unset, we will assume the current directory
Expand All @@ -75,7 +76,7 @@ fn run() -> Result<(), Error> {
format: opt.format.map(Into::into).unwrap_or(ByteFormat::Metric),
};
let res = match opt.command {
Some(Aggregate { input: _ }) => unimplemented!(),
Some(Aggregate { input }) => dua::aggregate(stdout_locked, walk_options, input),
None => dua::aggregate(stdout_locked, walk_options, vec![PathBuf::from(".")]),
}?;

Expand Down
6 changes: 6 additions & 0 deletions tests/snapshots/success-no-arguments-multiple-input-paths
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1.26 MB .
1.26 MB .
1.26 MB dir
1.26 MB ./dir/
256.00 KB ./dir/sub
5.29 MB <TOTAL>
19 changes: 14 additions & 5 deletions tests/stateless-journey.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ SUCCESSFULLY=0

(with "a sample directory"
(sandbox
cp -R "$fixtures/sample-01" .
cp -R "$fixtures/sample-01/" .
(with "no arguments"
it "produces a human-readable (metric) aggregate of the current directory, without total" && {
WITH_SNAPSHOT="$snapshot/success-no-arguments" \
expect_run ${SUCCESSFULLY} "$exe"
}
(with "no given path"
it "produces a human-readable (metric) aggregate of the current directory, without total" && {
WITH_SNAPSHOT="$snapshot/success-no-arguments" \
expect_run ${SUCCESSFULLY} "$exe"
}
)
ls
(with "multiple given paths"
it "produces a human-readable (metric) aggregate of the current directory, with total" && {
WITH_SNAPSHOT="$snapshot/success-no-arguments-multiple-input-paths" \
expect_run ${SUCCESSFULLY} "$exe" a . . dir ./dir/ ./dir/sub
}
)
)

(with "the byte format set"
Expand Down

0 comments on commit 04ce0c9

Please sign in to comment.