summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Thiel <sthiel@thoughtworks.com>2019-06-01 14:27:52 +0530
committerSebastian Thiel <sthiel@thoughtworks.com>2019-06-01 14:27:52 +0530
commitf8c3ba29134af08ea7b70b4fe3951307c6be6e3a (patch)
treec34182c60235def2f99ca2b92bc42e7da0d046ba
parente7da7843ad7894a3560b4d70076a74798404da94 (diff)
Sort by size in bytes by default; can be turned off for immediate feedback
-rw-r--r--src/aggregate.rs14
-rw-r--r--src/main.rs9
-rw-r--r--src/options.rs4
-rw-r--r--tests/snapshots/failure-no-arguments-multiple-input-paths-some-not-existing4
-rw-r--r--tests/snapshots/success-no-arguments-multiple-input-paths6
-rw-r--r--tests/snapshots/success-no-arguments-multiple-input-paths-no-sort6
-rw-r--r--tests/snapshots/success-no-arguments-multiple-input-paths-no-total6
-rwxr-xr-xtests/stateless-journey.sh14
8 files changed, 47 insertions, 16 deletions
diff --git a/src/aggregate.rs b/src/aggregate.rs
index 62b8acc..c910ed7 100644
--- a/src/aggregate.rs
+++ b/src/aggregate.rs
@@ -7,11 +7,13 @@ pub fn aggregate(
mut out: impl io::Write,
options: WalkOptions,
compute_total: bool,
+ sort_by_size_in_bytes: bool,
paths: impl IntoIterator<Item = impl AsRef<Path>>,
) -> Result<WalkResult, Error> {
let mut res = WalkResult::default();
let mut total = 0;
let mut num_roots = 0;
+ let mut aggregates = Vec::new();
for path in paths.into_iter() {
num_roots += 1;
let mut num_bytes = 0u64;
@@ -35,10 +37,20 @@ pub fn aggregate(
}
}
- write_path(&mut out, &options, path, num_bytes, num_errors)?;
+ if sort_by_size_in_bytes {
+ aggregates.push((path.as_ref().to_owned(), num_bytes, num_errors));
+ } else {
+ write_path(&mut out, &options, path, num_bytes, num_errors)?;
+ }
total += num_bytes;
res.num_errors += num_errors;
}
+ if sort_by_size_in_bytes {
+ aggregates.sort_by_key(|&(_, num_bytes, _)| num_bytes);
+ for (path, num_bytes, num_errors) in aggregates.into_iter() {
+ write_path(&mut out, &options, path, num_bytes, num_errors)?;
+ }
+ }
if num_roots > 1 && compute_total {
write_path(
&mut out,
diff --git a/src/main.rs b/src/main.rs
index 89d17fa..f73fdbc 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -22,13 +22,16 @@ fn run() -> Result<(), Error> {
format: opt.format.map(Into::into).unwrap_or(ByteFormat::Metric),
};
let res = match opt.command {
- Some(Aggregate { input, no_total }) => {
- dua::aggregate(stdout_locked, walk_options, !no_total, input)
- }
+ Some(Aggregate {
+ input,
+ no_total,
+ no_sort,
+ }) => dua::aggregate(stdout_locked, walk_options, !no_total, !no_sort, input),
None => dua::aggregate(
stdout_locked,
walk_options,
true,
+ true,
if opt.input.len() == 0 {
vec![PathBuf::from(".")]
} else {
diff --git a/src/options.rs b/src/options.rs
index 6136109..986c256 100644
--- a/src/options.rs
+++ b/src/options.rs
@@ -49,6 +49,10 @@ pub enum Command {
/// Aggregrate the consumed space of one or more directories or files
#[structopt(name = "aggregate", alias = "a")]
Aggregate {
+ /// If set, paths will be printed in their order of occurrence on the command-line.
+ /// Otherwise they are sorted by their size in bytes, ascending.
+ #[structopt(long = "no-sort")]
+ no_sort: bool,
/// If set, no total column will be computed for multiple inputs
#[structopt(long = "no-total")]
no_total: bool,
diff --git a/tests/snapshots/failure-no-arguments-multiple-input-paths-some-not-existing b/tests/snapshots/failure-no-arguments-multiple-input-paths-some-not-existing
index 4c9d9a2..6166323 100644
--- a/tests/snapshots/failure-no-arguments-multiple-input-paths-some-not-existing
+++ b/tests/snapshots/failure-no-arguments-multiple-input-paths-some-not-existing
@@ -1,6 +1,6 @@
- 1.26 MB .
- 1.26 MB .
0.00 B foo <1 IO Error(s)>
0.00 B bar <1 IO Error(s)>
0.00 B baz <1 IO Error(s)>
+ 1.26 MB .
+ 1.26 MB .
2.52 MB total <3 IO Error(s)> \ No newline at end of file
diff --git a/tests/snapshots/success-no-arguments-multiple-input-paths b/tests/snapshots/success-no-arguments-multiple-input-paths
index 1725633..f157e57 100644
--- a/tests/snapshots/success-no-arguments-multiple-input-paths
+++ b/tests/snapshots/success-no-arguments-multiple-input-paths
@@ -1,6 +1,6 @@
- 1.26 MB .
- 1.26 MB .
+ 256.00 KB ./dir/sub
1.26 MB dir
1.26 MB ./dir/
- 256.00 KB ./dir/sub
+ 1.26 MB .
+ 1.26 MB .
5.29 MB total \ No newline at end of file
diff --git a/tests/snapshots/success-no-arguments-multiple-input-paths-no-sort b/tests/snapshots/success-no-arguments-multiple-input-paths-no-sort
new file mode 100644
index 0000000..1725633
--- /dev/null
+++ b/tests/snapshots/success-no-arguments-multiple-input-paths-no-sort
@@ -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 \ No newline at end of file
diff --git a/tests/snapshots/success-no-arguments-multiple-input-paths-no-total b/tests/snapshots/success-no-arguments-multiple-input-paths-no-total
index 2c1725a..2427939 100644
--- a/tests/snapshots/success-no-arguments-multiple-input-paths-no-total
+++ b/tests/snapshots/success-no-arguments-multiple-input-paths-no-total
@@ -1,5 +1,5 @@
- 1.26 MB .
- 1.26 MB .
+ 256.00 KB ./dir/sub
1.26 MB dir
1.26 MB ./dir/
- 256.00 KB ./dir/sub \ No newline at end of file
+ 1.26 MB .
+ 1.26 MB . \ No newline at end of file
diff --git a/tests/stateless-journey.sh b/tests/stateless-journey.sh
index bd31903..1cd0fa9 100755
--- a/tests/stateless-journey.sh
+++ b/tests/stateless-journey.sh
@@ -27,26 +27,32 @@ WITH_FAILURE=1
(with "multiple given paths"
(when "specifying the 'aggregate' subcommand"
(with "no option to adjust the total"
- it "produces a human-readable (metric) aggregate of the current directory, with total" && {
+ it "produces a human-readable aggregate, with total" && {
WITH_SNAPSHOT="$snapshot/success-no-arguments-multiple-input-paths" \
expect_run ${SUCCESSFULLY} "$exe" aggregate . . dir ./dir/ ./dir/sub
}
)
(with "the --no-total option set"
- it "produces a human-readable (metric) aggregate of the current directory, without total" && {
+ it "produces a human-readable aggregate, without total" && {
WITH_SNAPSHOT="$snapshot/success-no-arguments-multiple-input-paths-no-total" \
expect_run ${SUCCESSFULLY} "$exe" aggregate --no-total . . dir ./dir/ ./dir/sub
}
)
+ (with "the --no-sort option set"
+ it "produces a human-readable aggregate, sorted in order specified on the command-line" && {
+ WITH_SNAPSHOT="$snapshot/success-no-arguments-multiple-input-paths-no-sort" \
+ expect_run ${SUCCESSFULLY} "$exe" aggregate --no-sort . . dir ./dir/ ./dir/sub
+ }
+ )
)
(when "specifying no subcommand"
- it "produces a human-readable (metric) aggregate of the current directory, with total" && {
+ it "produces a human-readable aggregate" && {
WITH_SNAPSHOT="$snapshot/success-no-arguments-multiple-input-paths" \
expect_run ${SUCCESSFULLY} "$exe" . . dir ./dir/ ./dir/sub
}
)
(when "specifying no subcommand and some of the directories don't exist"
- it "produces a human-readable (metric) aggregate of the current directory, with total" && {
+ it "produces a human-readable aggregate, with the number of errors per root" && {
WITH_SNAPSHOT="$snapshot/failure-no-arguments-multiple-input-paths-some-not-existing" \
expect_run ${WITH_FAILURE} "$exe" . . foo bar baz
}