From 25ab1f0639246a4622c0561abe6acb54c686df51 Mon Sep 17 00:00:00 2001 From: fawn Date: Tue, 14 Mar 2023 03:11:43 +0200 Subject: [PATCH 1/3] allow `dirs-first` flag to work regardless of order --- src/render/order.rs | 4 ++++ src/render/tree/mod.rs | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/render/order.rs b/src/render/order.rs index a2e0e8f7..ff606a79 100644 --- a/src/render/order.rs +++ b/src/render/order.rs @@ -13,6 +13,9 @@ pub enum SortType { /// Sort entries by size largest to smallest, bottom to top SizeRev, + + /// Do not sort entries + None, } #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] @@ -56,6 +59,7 @@ impl SortType { Self::Name => Some(Box::new(Self::name_comparator)), Self::Size => Some(Box::new(Self::size_comparator)), Self::SizeRev => Some(Box::new(Self::size_rev_comparator)), + Self::None => None, } } diff --git a/src/render/tree/mod.rs b/src/render/tree/mod.rs index dea93f95..c1dbdbd9 100644 --- a/src/render/tree/mod.rs +++ b/src/render/tree/mod.rs @@ -1,4 +1,8 @@ -use crate::render::{context::Context, disk_usage::FileSize, order::Order}; +use crate::render::{ + context::Context, + disk_usage::FileSize, + order::{Order, SortType}, +}; use crossbeam::channel::{self, Sender}; use error::Error; use ignore::{WalkBuilder, WalkParallel, WalkState}; @@ -172,6 +176,10 @@ impl Tree { if let Some(ordr) = ctx.sort().map(|s| Order::from((s, ctx.dirs_first()))) { ordr.comparator() .map(|func| current_node.sort_children(func)); + } else if ctx.dirs_first() { + Order::from((SortType::None, true)) + .comparator() + .map(|func| current_node.sort_children(func)); } } } From 86a6b750bc4afbd48dd1abb45b24bd6f4cc6023e Mon Sep 17 00:00:00 2001 From: fawn Date: Tue, 14 Mar 2023 06:10:30 +0200 Subject: [PATCH 2/3] Option -> SortType, set default value --- src/render/context/mod.rs | 6 +++--- src/render/context/test.rs | 4 +--- src/render/tree/mod.rs | 17 ++++------------- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/src/render/context/mod.rs b/src/render/context/mod.rs index 7cd1e3c8..c9cdf416 100644 --- a/src/render/context/mod.rs +++ b/src/render/context/mod.rs @@ -78,8 +78,8 @@ pub struct Context { pub prune: bool, /// Sort-order to display directory content - #[arg(short, long, value_enum)] - sort: Option, + #[arg(short, long, value_enum, default_value_t = SortType::None)] + sort: SortType, /// Always sorts directories above files #[arg(long)] @@ -147,7 +147,7 @@ impl Context { } /// The sort-order used for printing. - pub fn sort(&self) -> Option { + pub fn sort(&self) -> SortType { self.sort } diff --git a/src/render/context/test.rs b/src/render/context/test.rs index 1e0fcb63..c9817aaf 100644 --- a/src/render/context/test.rs +++ b/src/render/context/test.rs @@ -13,9 +13,7 @@ fn config() { assert_eq!(level, 1, "Failed to properly read 'level' from config"); - let sort = context - .sort() - .expect("Failed to properly read 'sort' from config"); + let sort = context.sort(); assert_eq!( sort, diff --git a/src/render/tree/mod.rs b/src/render/tree/mod.rs index c1dbdbd9..548f1ed7 100644 --- a/src/render/tree/mod.rs +++ b/src/render/tree/mod.rs @@ -1,8 +1,4 @@ -use crate::render::{ - context::Context, - disk_usage::FileSize, - order::{Order, SortType}, -}; +use crate::render::{context::Context, disk_usage::FileSize, order::Order}; use crossbeam::channel::{self, Sender}; use error::Error; use ignore::{WalkBuilder, WalkParallel, WalkState}; @@ -173,14 +169,9 @@ impl Tree { current_node.set_file_size(dir_size) } - if let Some(ordr) = ctx.sort().map(|s| Order::from((s, ctx.dirs_first()))) { - ordr.comparator() - .map(|func| current_node.sort_children(func)); - } else if ctx.dirs_first() { - Order::from((SortType::None, true)) - .comparator() - .map(|func| current_node.sort_children(func)); - } + Order::from((ctx.sort(), ctx.dirs_first())) + .comparator() + .map(|func| current_node.sort_children(func)); } } From 510d52b28aff0b88dee3354373fd097e45dcd631 Mon Sep 17 00:00:00 2001 From: fawn Date: Tue, 14 Mar 2023 12:38:56 +0200 Subject: [PATCH 3/3] apply clippy this seems to have been applied before resolving the conflict --- src/render/tree/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/render/tree/mod.rs b/src/render/tree/mod.rs index 393c8512..45be38d3 100644 --- a/src/render/tree/mod.rs +++ b/src/render/tree/mod.rs @@ -167,9 +167,9 @@ impl Tree { current_node.set_file_size(dir_size) } - Order::from((ctx.sort(), ctx.dirs_first())) - .comparator() - .map(|func| current_node.sort_children(func)); + if let Some(func) = Order::from((ctx.sort(), ctx.dirs_first())).comparator() { + current_node.sort_children(func) + } } }