From 9e969d77129f5350583cd1619fb65d0d8f644d6c Mon Sep 17 00:00:00 2001 From: Benjamin Nguyen Date: Sun, 30 Apr 2023 23:22:47 -0700 Subject: [PATCH 1/2] show symlink target's path, not just name --- src/render/tree/node/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/render/tree/node/mod.rs b/src/render/tree/node/mod.rs index 521d21d7..71a5aaf3 100644 --- a/src/render/tree/node/mod.rs +++ b/src/render/tree/node/mod.rs @@ -154,7 +154,7 @@ impl Node { /// Returns the file name of the symlink target if [Node] represents a symlink. pub fn symlink_target_file_name(&self) -> Option<&OsStr> { - self.symlink_target_path().and_then(Path::file_name) + self.symlink_target_path().map(Path::as_os_str) } /// Returns reference to underlying [`FileType`]. From 2598eb68cdd0b124a9de06dcf107259bfd880654 Mon Sep 17 00:00:00 2001 From: Benjamin Nguyen Date: Sun, 30 Apr 2023 23:34:52 -0700 Subject: [PATCH 2/2] count unfollowed symlink file towards disk usage --- README.md | 5 +++-- src/render/context/config.rs | 4 +--- src/render/context/dir.rs | 2 +- src/render/tree/node/cmp.rs | 16 ++++++++++++---- src/render/tree/node/mod.rs | 18 ++++++++++++++---- tests/sort.rs | 4 ++-- 6 files changed, 33 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 09394f03..6ae1707b 100644 --- a/README.md +++ b/README.md @@ -210,8 +210,9 @@ If multiple hardlinks that point to the same inode are in the same file-tree, al -f, --follow Follow symlinks ``` -Symlinks will never be counted towards the total disk usage. When a symlink to a directory is followed all of the box-drawing characters of its descendants will -be painted in a different color for better visual feedback: +Symlinks when followed will have their targets (and descendants) counted towards total disk usage, otherwise the size of the symlink itself will be reported. +If a symlink's target happens to be in the same file-tree as the symlink itself, the target and its descendants will not be double-counted towards the total disk-usage. +When a symlink to a directory is followed all of the box-drawing characters of its descendants will be painted in a different color for better visual feedback:

failed to load picture diff --git a/src/render/context/config.rs b/src/render/context/config.rs index 7dd20be3..c0a908ed 100644 --- a/src/render/context/config.rs +++ b/src/render/context/config.rs @@ -91,9 +91,7 @@ fn config_from_home() -> Option { fn config_from_appdata() -> Option { let app_data = dirs::config_dir()?; - let config_path = app_data - .join(ERDTREE_DIR) - .join(ERDTREE_CONFIG_NAME); + let config_path = app_data.join(ERDTREE_DIR).join(ERDTREE_CONFIG_NAME); fs::read_to_string(config_path).ok() } diff --git a/src/render/context/dir.rs b/src/render/context/dir.rs index 37ac8729..0a87dd32 100644 --- a/src/render/context/dir.rs +++ b/src/render/context/dir.rs @@ -11,5 +11,5 @@ pub enum Order { First, /// Sort directories below files - Last + Last, } diff --git a/src/render/tree/node/cmp.rs b/src/render/tree/node/cmp.rs index 91ae37a9..69f8bc0e 100644 --- a/src/render/tree/node/cmp.rs +++ b/src/render/tree/node/cmp.rs @@ -13,10 +13,10 @@ pub fn comparator(ctx: &Context) -> Box { dir::Order::None => (), dir::Order::First => { return Box::new(move |a, b| dir_first_comparator(a, b, base_comparator(sort_type))); - }, + } dir::Order::Last => { return Box::new(move |a, b| dir_last_comparator(a, b, base_comparator(sort_type))); - }, + } }; base_comparator(sort_type) @@ -32,7 +32,11 @@ fn base_comparator(sort_type: sort::Type) -> Box { } /// Orders directories first. Provides a fallback if inputs are not directories. -fn dir_first_comparator(a: &Node, b: &Node, fallback: impl Fn(&Node, &Node) -> Ordering) -> Ordering { +fn dir_first_comparator( + a: &Node, + b: &Node, + fallback: impl Fn(&Node, &Node) -> Ordering, +) -> Ordering { match (a.is_dir(), b.is_dir()) { (true, false) => Ordering::Greater, (false, true) => Ordering::Less, @@ -41,7 +45,11 @@ fn dir_first_comparator(a: &Node, b: &Node, fallback: impl Fn(&Node, &Node) -> O } /// Orders directories last. Provides a fallback if inputs are not directories. -fn dir_last_comparator(a: &Node, b: &Node, fallback: impl Fn(&Node, &Node) -> Ordering) -> Ordering { +fn dir_last_comparator( + a: &Node, + b: &Node, + fallback: impl Fn(&Node, &Node) -> Ordering, +) -> Ordering { match (a.is_dir(), b.is_dir()) { (true, false) => Ordering::Less, (false, true) => Ordering::Greater, diff --git a/src/render/tree/node/mod.rs b/src/render/tree/node/mod.rs index 71a5aaf3..244a0268 100644 --- a/src/render/tree/node/mod.rs +++ b/src/render/tree/node/mod.rs @@ -241,10 +241,20 @@ impl TryFrom<(DirEntry, &Context)> for Node { let file_type = dir_entry.file_type(); let mut file_size = match file_type { - Some(ref ft) if ft.is_file() && !ctx.suppress_size => match ctx.disk_usage { - DiskUsage::Logical => Some(FileSize::logical(&metadata, ctx.unit, ctx.human)), - DiskUsage::Physical => FileSize::physical(path, &metadata, ctx.unit, ctx.human), - }, + Some(ref ft) if !ctx.suppress_size => { + if ft.is_file() || (ft.is_symlink() && !ctx.follow) { + match ctx.disk_usage { + DiskUsage::Logical => { + Some(FileSize::logical(&metadata, ctx.unit, ctx.human)) + } + DiskUsage::Physical => { + FileSize::physical(path, &metadata, ctx.unit, ctx.human) + } + } + } else { + None + } + } _ => None, }; diff --git a/tests/sort.rs b/tests/sort.rs index 3000ca44..9942447e 100644 --- a/tests/sort.rs +++ b/tests/sort.rs @@ -29,7 +29,7 @@ fn sort_name_dir_order() { assert_eq!( utils::run_cmd(&["--sort", "name", "--dir-order", "first", "tests/data"]), indoc!( - "143 B ┌─ cassildas_song.md + "143 B ┌─ cassildas_song.md 143 B ┌─ the_yellow_king 446 B │ ┌─ lipsum.txt 446 B ├─ lipsum @@ -47,7 +47,7 @@ fn sort_name_dir_order() { assert_eq!( utils::run_cmd(&["--sort", "name", "--dir-order", "last", "tests/data"]), indoc!( - "100 B ┌─ nylarlathotep.txt + "100 B ┌─ nylarlathotep.txt 161 B ├─ nemesis.txt 83 B ├─ necronomicon.txt 143 B │ ┌─ cassildas_song.md