Skip to content

allow dirs-first flag to work regardless of order #67

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

Merged
merged 4 commits into from
Mar 14, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/render/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ pub struct Context {
pub prune: bool,

/// Sort-order to display directory content
#[arg(short, long, value_enum)]
sort: Option<SortType>,
#[arg(short, long, value_enum, default_value_t = SortType::None)]
sort: SortType,

/// Always sorts directories above files
#[arg(long)]
Expand Down Expand Up @@ -151,7 +151,7 @@ impl Context {
}

/// The sort-order used for printing.
pub fn sort(&self) -> Option<SortType> {
pub fn sort(&self) -> SortType {
self.sort
}

Expand Down
4 changes: 1 addition & 3 deletions src/render/context/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/render/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/render/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,8 @@ impl Tree {
current_node.set_file_size(dir_size)
}

if let Some(order) = ctx.sort().map(|s| Order::from((s, ctx.dirs_first()))) {
if let Some(func) = order.comparator() {
current_node.sort_children(func);
}
if let Some(func) = Order::from((ctx.sort(), ctx.dirs_first())).comparator() {
current_node.sort_children(func)
}
}
}
Expand Down