From ae0182f09c0e2c3c77298cb431421cbdc64c0fa8 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sat, 1 Jun 2019 20:09:30 +0530 Subject: Simplified handling of 'no paths given' case --- Cargo.toml | 2 +- src/main.rs | 28 +++++++++++++++++++++++++--- src/options.rs | 4 ++-- tests/snapshots/success-bytes-binary | 7 ++++++- tests/snapshots/success-bytes-only | 7 ++++++- tests/snapshots/success-no-arguments | 7 ++++++- tests/snapshots/success-no-arguments-no-sort | 6 ++++++ tests/stateless-journey.sh | 25 ++++++++++++++++++++----- 8 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 tests/snapshots/success-no-arguments-no-sort diff --git a/Cargo.toml b/Cargo.toml index e797708..d9605db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dua-cli" -version = "1.0.0" +version = "1.1.0" authors = ["Sebastian Thiel "] edition = "2018" include = ["src/**/*", "Cargo.toml"] diff --git a/src/main.rs b/src/main.rs index 33d108c..66ec8f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,8 @@ use structopt::StructOpt; use dua::{ByteFormat, Color}; use failure::Error; use failure_tools::ok_or_exit; -use std::{io, io::Write, path::PathBuf, process}; +use std::path::PathBuf; +use std::{fs, io, io::Write, process}; mod options; @@ -34,7 +35,17 @@ fn run() -> Result<(), Error> { statistics, }) => ( statistics, - dua::aggregate(stdout_locked, walk_options, !no_total, !no_sort, input)?, + dua::aggregate( + stdout_locked, + walk_options, + !no_total, + !no_sort, + if input.len() == 0 { + cwd_dirlist()? + } else { + input + }, + )?, ), None => ( false, @@ -44,7 +55,7 @@ fn run() -> Result<(), Error> { true, true, if opt.input.len() == 0 { - vec![PathBuf::from(".")] + cwd_dirlist()? } else { opt.input }, @@ -61,6 +72,17 @@ fn run() -> Result<(), Error> { Ok(()) } +fn cwd_dirlist() -> Result, io::Error> { + let mut v: Vec<_> = fs::read_dir(".")? + .filter_map(|e| { + e.ok() + .and_then(|e| e.path().strip_prefix(".").ok().map(ToOwned::to_owned)) + }) + .collect(); + v.sort(); + Ok(v) +} + fn main() { ok_or_exit(run()) } diff --git a/src/options.rs b/src/options.rs index 5c30573..76c0abc 100644 --- a/src/options.rs +++ b/src/options.rs @@ -40,7 +40,7 @@ pub struct Args { #[structopt(short = "f", long = "format")] pub format: Option, - /// One or more input files. If unset, we will assume the current directory + /// One or more input files. If unset, we will use all entries in the current working directory. #[structopt(parse(from_os_str))] pub input: Vec, } @@ -60,7 +60,7 @@ pub enum Command { /// If set, no total column will be computed for multiple inputs #[structopt(long = "no-total")] no_total: bool, - /// One or more input files. If unset, we will assume the current directory + /// One or more input files. If unset, we will use all entries in the current working directory. #[structopt(parse(from_os_str))] input: Vec, }, diff --git a/tests/snapshots/success-bytes-binary b/tests/snapshots/success-bytes-binary index dc4db03..acc26fc 100644 --- a/tests/snapshots/success-bytes-binary +++ b/tests/snapshots/success-bytes-binary @@ -1 +1,6 @@ - 1.20 MiB . \ No newline at end of file + 0.00 B b.empty + 256.00 B a + 256.00 B c.lnk + 666.00 B .hidden.666 + 1.20 MiB dir + 1.20 MiB total \ No newline at end of file diff --git a/tests/snapshots/success-bytes-only b/tests/snapshots/success-bytes-only index 85a7d8a..b83f090 100644 --- a/tests/snapshots/success-bytes-only +++ b/tests/snapshots/success-bytes-only @@ -1 +1,6 @@ - 1258947 b . \ No newline at end of file + 0 b b.empty + 256 b a + 256 b c.lnk + 666 b .hidden.666 + 1258024 b dir + 1259202 b total \ No newline at end of file diff --git a/tests/snapshots/success-no-arguments b/tests/snapshots/success-no-arguments index 51714b5..e85cf4b 100644 --- a/tests/snapshots/success-no-arguments +++ b/tests/snapshots/success-no-arguments @@ -1 +1,6 @@ - 1.26 MB . \ No newline at end of file + 0.00 B b.empty + 256.00 B a + 256.00 B c.lnk + 666.00 B .hidden.666 + 1.26 MB dir + 1.26 MB total \ No newline at end of file diff --git a/tests/snapshots/success-no-arguments-no-sort b/tests/snapshots/success-no-arguments-no-sort new file mode 100644 index 0000000..42f24a4 --- /dev/null +++ b/tests/snapshots/success-no-arguments-no-sort @@ -0,0 +1,6 @@ + 666.00 B .hidden.666 + 256.00 B a + 0.00 B b.empty + 256.00 B c.lnk + 1.26 MB dir + 1.26 MB total \ No newline at end of file diff --git a/tests/stateless-journey.sh b/tests/stateless-journey.sh index 5d444f7..5625035 100755 --- a/tests/stateless-journey.sh +++ b/tests/stateless-journey.sh @@ -18,12 +18,27 @@ WITH_FAILURE=1 cp -R "$fixtures/sample-01/" . (with "no arguments" (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" - } + (with "no subcommand" + it "produces a human-readable (metric) aggregate of everything within the current directory, with total" && { + WITH_SNAPSHOT="$snapshot/success-no-arguments" \ + expect_run ${SUCCESSFULLY} "$exe" + } + ) + (with "the aggregate sub-command" + (with "no sorting option" + it "produces a human-readable (metric) aggregate of everything within the current directory, with total" && { + WITH_SNAPSHOT="$snapshot/success-no-arguments" \ + expect_run ${SUCCESSFULLY} "$exe" aggregate + } + ) + (with "sorting disabled" + it "produces a human-readable (metric) aggregate of everything within the current directory, alphabetically sorted, with total" && { + WITH_SNAPSHOT="$snapshot/success-no-arguments-no-sort" \ + expect_run ${SUCCESSFULLY} "$exe" aggregate --no-sort + } + ) + ) ) - ls (with "multiple given paths" (when "specifying the 'aggregate' subcommand" (with "no option to adjust the total" -- cgit v1.2.3