Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to suppress size from being printed #47

Merged
merged 3 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Options:
-l, --level <NUM> Maximum depth to display
-n, --scale <NUM> Total number of digits after the decimal to display for disk usage [default: 2]
-s, --sort <SORT> Sort-order to display directory content [default: none] [possible values: name, size, size-rev, none]
--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 <THREADS> Number of threads to use [default: 4]
Expand Down
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ pub struct Clargs {
/// Number of threads to use
#[arg(short, long, default_value_t = 4)]
pub threads: usize,

/// Omit disk usage from output; disabled by default"
#[arg(long)]
pub suppress_size: bool,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you reword to "Omit disk usage from output; disabled by default"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, updated in both locations.

}

/// Order in which to print nodes.
Expand Down
18 changes: 12 additions & 6 deletions src/fs/erdtree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ pub struct NodePrecursor<'a> {
dir_entry: DirEntry,
show_icon: bool,
scale: usize,
suppress_size: bool,
}

impl<'a> NodePrecursor<'a> {
Expand All @@ -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,
}
}
}
Expand All @@ -241,6 +244,7 @@ impl From<NodePrecursor<'_>> for Node {
dir_entry,
show_icon,
scale,
suppress_size,
} = precursor;

let children = None;
Expand Down Expand Up @@ -272,12 +276,14 @@ impl From<NodePrecursor<'_>> 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),
}
}
}
}
Expand Down
20 changes: 17 additions & 3 deletions src/fs/erdtree/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub struct Tree {
root: Node,
#[allow(dead_code)]
scale: usize,
#[allow(dead_code)]
suppress_size: bool,
}

pub type TreeResult<T> = Result<T, Error>;
Expand All @@ -48,8 +50,9 @@ impl Tree {
icons: bool,
disk_usage: DiskUsage,
scale: usize,
suppress_size: bool,
) -> TreeResult<Self> {
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,
Expand All @@ -58,6 +61,7 @@ impl Tree {
root,
icons,
scale,
suppress_size,
})
}

Expand All @@ -77,6 +81,7 @@ impl Tree {
icons: bool,
disk_usage: &DiskUsage,
scale: usize,
suppress_size: bool,
) -> TreeResult<Node> {
let (tx, rx) = channel::unbounded::<Node>();

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -197,7 +202,16 @@ impl TryFrom<Clargs> 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)
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/suppress_size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use indoc::indoc;

mod utils;

#[test]
fn suppress_size() {
assert_eq!(
utils::run_cmd(&["--suppress-size", "--sort", "name", "tests/data"]),
indoc!(
"
data
├─ dream_cycle
│ └─ polaris.txt
├─ lipsum
│ └─ lipsum.txt
├─ necronomicon.txt
├─ nemesis.txt
├─ nylarlathotep.txt
└─ the_yellow_king
└─ cassildas_song.md"
),
"Failed to suppress size."
)
}