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

Implement dirs-only option #95

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/render/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ pub struct Context {
#[arg(long)]
/// Print completions for a given shell to stdout
pub completions: Option<clap_complete::Shell>,

#[arg(short = 'D', long)]
/// Only show directories
pub dirs_only: bool,
}

impl Context {
Expand Down
3 changes: 3 additions & 0 deletions src/render/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ impl Tree {
let mut root_id = None;

while let Ok(TraversalState::Ongoing(node)) = rx.recv() {
if ctx.dirs_only && !node.is_dir() {
Copy link
Owner

Choose a reason for hiding this comment

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

Hmm so doing it here would result in the loss of disk usage information which is something I would still want to compute and surface to the end-user unless they supply the --supress-size option. We'd probably want to filter for dirs on the display step which happens somewhere here.

Copy link
Author

Choose a reason for hiding this comment

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

Sounds reasonable, I'll update the PR accordingly.

Copy link
Author

Choose a reason for hiding this comment

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

The condition is now checked inside the display_node function (18e7877). With aeaf10a the unit test got adjusted.

Copy link
Author

Choose a reason for hiding this comment

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

That's the output for the repository:

Screenshot 2023-03-22 at 17 08 05

By the way, is there a way to disable colors in the ouptut? If not I'll create another a issue or PR.

Copy link
Author

Choose a reason for hiding this comment

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

Just saw that this should have printed └─ in front of utils.

Copy link
Owner

Choose a reason for hiding this comment

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

There isn't currently a way to disable colors. Feel free to open an issue for that. I'd likely be the one to address that as unfortunately given how erdtree is built it would require a lot of tedious changes.

Copy link
Author

Choose a reason for hiding this comment

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

I think we need to filter this on a node and not on a display level. I'll notify you when I have a proper fix.

Copy link
Author

Choose a reason for hiding this comment

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

@solidiquis It's not ideal, but pruning the file nodes after the tree has been assembled seems to be the easiest way to get a dirs-only view that also includes size information. To get directory sizes we need to accumulate the size of all nodes below the directory. Commit 3333586 contains the patch that prunes the file nodes if dirs-only is set.

Copy link
Author

Choose a reason for hiding this comment

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

Also, the tree is now properly rendered:

Screenshot 2023-03-23 at 21 19 17

continue
}
if node.is_dir() {
let node_path = node.path();

Expand Down
24 changes: 24 additions & 0 deletions tests/dirs_only.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use indoc::indoc;

mod utils;

#[test]
fn level() {
assert_eq!(
utils::run_cmd(&[
"--sort",
"name",
"--no-config",
"--dirs-only",
"tests/data"
]),
indoc!(
"
data
├─ dream_cycle
├─ lipsum
└─ the_yellow_king"
),
"Failed to print dirs-only."
)
}