Skip to content

Commit

Permalink
Merge pull request #162 from solidiquis/bug/symlink-path
Browse files Browse the repository at this point in the history
show symlink target's path, not just name
  • Loading branch information
solidiquis authored May 1, 2023
2 parents 26a62b3 + 2598eb6 commit 269940e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

<p align="center">
<img src="https://github.com/solidiquis/erdtree/blob/master/assets/symlinks.png?raw=true" alt="failed to load picture" />
Expand Down
4 changes: 1 addition & 3 deletions src/render/context/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ fn config_from_home() -> Option<String> {
fn config_from_appdata() -> Option<String> {
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()
}
Expand Down
2 changes: 1 addition & 1 deletion src/render/context/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ pub enum Order {
First,

/// Sort directories below files
Last
Last,
}
16 changes: 12 additions & 4 deletions src/render/tree/node/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ pub fn comparator(ctx: &Context) -> Box<NodeComparator> {
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)
Expand All @@ -32,7 +32,11 @@ fn base_comparator(sort_type: sort::Type) -> Box<NodeComparator> {
}

/// 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,
Expand All @@ -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,
Expand Down
20 changes: 15 additions & 5 deletions src/render/tree/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -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,
};

Expand Down
4 changes: 2 additions & 2 deletions tests/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 269940e

Please sign in to comment.