From 3fc914dc407c0f181da64fac228fa8576194e27b Mon Sep 17 00:00:00 2001 From: Paul Sanders Date: Mon, 6 Mar 2023 21:44:13 -0500 Subject: [PATCH 1/3] Add option to suppress size from being printed --- README.md | 1 + src/cli.rs | 4 ++++ src/fs/erdtree/node.rs | 18 ++++++++++++------ src/fs/erdtree/tree/mod.rs | 20 +++++++++++++++++--- tests/suppress_size.rs | 24 ++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 tests/suppress_size.rs diff --git a/README.md b/README.md index 5c28d9ff..418a1e8c 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ Options: -l, --level Maximum depth to display -n, --scale Total number of digits after the decimal to display for disk usage [default: 2] -s, --sort Sort-order to display directory content [default: none] [possible values: name, size, size-rev, none] + --suppress-size Don't show size --dirs-first Always sorts directories above files -S, --follow-links Traverse symlink directories and consider their disk usage; disabled by default -t, --threads Number of threads to use [default: 4] diff --git a/src/cli.rs b/src/cli.rs index eedc7e9f..feafd517 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -77,6 +77,10 @@ pub struct Clargs { /// Number of threads to use #[arg(short, long, default_value_t = 4)] pub threads: usize, + + /// Don't show size; disabled by default + #[arg(long)] + pub suppress_size: bool, } /// Order in which to print nodes. diff --git a/src/fs/erdtree/node.rs b/src/fs/erdtree/node.rs index 43c0030b..06328737 100644 --- a/src/fs/erdtree/node.rs +++ b/src/fs/erdtree/node.rs @@ -215,6 +215,7 @@ pub struct NodePrecursor<'a> { dir_entry: DirEntry, show_icon: bool, scale: usize, + suppress_size: bool, } impl<'a> NodePrecursor<'a> { @@ -224,12 +225,14 @@ impl<'a> NodePrecursor<'a> { dir_entry: DirEntry, show_icon: bool, scale: usize, + suppress_size: bool, ) -> Self { Self { disk_usage, dir_entry, show_icon, scale, + suppress_size, } } } @@ -241,6 +244,7 @@ impl From> for Node { dir_entry, show_icon, scale, + suppress_size, } = precursor; let children = None; @@ -272,12 +276,14 @@ impl From> for Node { let mut file_size = None; - if let Some(ref ft) = file_type { - if ft.is_file() { - if let Some(ref md) = metadata { - file_size = match disk_usage { - DiskUsage::Logical => Some(FileSize::logical(md, scale)), - DiskUsage::Physical => FileSize::physical(path, md, scale), + if !suppress_size { + if let Some(ref ft) = file_type { + if ft.is_file() { + if let Some(ref md) = metadata { + file_size = match disk_usage { + DiskUsage::Logical => Some(FileSize::logical(md, scale)), + DiskUsage::Physical => FileSize::physical(path, md, scale), + } } } } diff --git a/src/fs/erdtree/tree/mod.rs b/src/fs/erdtree/tree/mod.rs index ca10338b..4b5568d3 100644 --- a/src/fs/erdtree/tree/mod.rs +++ b/src/fs/erdtree/tree/mod.rs @@ -33,6 +33,8 @@ pub struct Tree { root: Node, #[allow(dead_code)] scale: usize, + #[allow(dead_code)] + suppress_size: bool, } pub type TreeResult = Result; @@ -48,8 +50,9 @@ impl Tree { icons: bool, disk_usage: DiskUsage, scale: usize, + suppress_size: bool, ) -> TreeResult { - let root = Self::traverse(walker, &order, icons, &disk_usage, scale)?; + let root = Self::traverse(walker, &order, icons, &disk_usage, scale, suppress_size)?; Ok(Self { disk_usage, @@ -58,6 +61,7 @@ impl Tree { root, icons, scale, + suppress_size, }) } @@ -77,6 +81,7 @@ impl Tree { icons: bool, disk_usage: &DiskUsage, scale: usize, + suppress_size: bool, ) -> TreeResult { let (tx, rx) = channel::unbounded::(); @@ -131,7 +136,7 @@ impl Tree { let tx = Sender::clone(&tx); entry_res - .map(|entry| NodePrecursor::new(disk_usage, entry, icons, scale)) + .map(|entry| NodePrecursor::new(disk_usage, entry, icons, scale, suppress_size)) .map(Node::from) .map(|node| tx.send(node).unwrap()) .map(|_| WalkState::Continue) @@ -197,7 +202,16 @@ impl TryFrom for Tree { let order = Order::from((clargs.sort(), clargs.dirs_first())); let du = DiskUsage::from(clargs.disk_usage()); let scale = clargs.scale; - let tree = Tree::new(walker, order, clargs.level(), clargs.icons, du, scale)?; + let suppress_size = clargs.suppress_size; + let tree = Tree::new( + walker, + order, + clargs.level(), + clargs.icons, + du, + scale, + suppress_size, + )?; Ok(tree) } } diff --git a/tests/suppress_size.rs b/tests/suppress_size.rs new file mode 100644 index 00000000..327c7f00 --- /dev/null +++ b/tests/suppress_size.rs @@ -0,0 +1,24 @@ +use indoc::indoc; + +mod utils; + +#[test] +fn suppress_size() { + assert_eq!( + utils::run_cmd(&["--suppress-size", "tests/data"]), + indoc!( + " + data + ├─ nylarlathotep.txt + ├─ lipsum + │ └─ lipsum.txt + ├─ dream_cycle + │ └─ polaris.txt + ├─ necronomicon.txt + ├─ the_yellow_king + │ └─ cassildas_song.md + └─ nemesis.txt" + ), + "Failed to suppress size." + ) +} From a41ca989f17efcfd89df865cb1a493820c522bef Mon Sep 17 00:00:00 2001 From: Paul Sanders Date: Tue, 7 Mar 2023 13:42:05 -0500 Subject: [PATCH 2/3] Reword help text --- README.md | 2 +- src/cli.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 418a1e8c..968c0f48 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ Options: -l, --level Maximum depth to display -n, --scale Total number of digits after the decimal to display for disk usage [default: 2] -s, --sort Sort-order to display directory content [default: none] [possible values: name, size, size-rev, none] - --suppress-size Don't show size + --suppress-size Omit disk usage from output --dirs-first Always sorts directories above files -S, --follow-links Traverse symlink directories and consider their disk usage; disabled by default -t, --threads Number of threads to use [default: 4] diff --git a/src/cli.rs b/src/cli.rs index feafd517..2e532a47 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -78,7 +78,7 @@ pub struct Clargs { #[arg(short, long, default_value_t = 4)] pub threads: usize, - /// Don't show size; disabled by default + /// Omit disk usage from output; disabled by default" #[arg(long)] pub suppress_size: bool, } From 8a0e779b1784275a38d21566d1c7bf67b51f2926 Mon Sep 17 00:00:00 2001 From: Paul Sanders Date: Tue, 7 Mar 2023 14:44:24 -0500 Subject: [PATCH 3/3] Add sort to test --- tests/suppress_size.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/suppress_size.rs b/tests/suppress_size.rs index 327c7f00..a45d6d10 100644 --- a/tests/suppress_size.rs +++ b/tests/suppress_size.rs @@ -5,19 +5,19 @@ mod utils; #[test] fn suppress_size() { assert_eq!( - utils::run_cmd(&["--suppress-size", "tests/data"]), + utils::run_cmd(&["--suppress-size", "--sort", "name", "tests/data"]), indoc!( " data - ├─ nylarlathotep.txt - ├─ lipsum - │ └─ lipsum.txt ├─ dream_cycle │ └─ polaris.txt + ├─ lipsum + │ └─ lipsum.txt ├─ necronomicon.txt - ├─ the_yellow_king - │ └─ cassildas_song.md - └─ nemesis.txt" + ├─ nemesis.txt + ├─ nylarlathotep.txt + └─ the_yellow_king + └─ cassildas_song.md" ), "Failed to suppress size." )