Skip to content

Commit

Permalink
Merge pull request #67 from fawni/fix/dirs-first
Browse files Browse the repository at this point in the history
allow `dirs-first` flag to work regardless of order
  • Loading branch information
solidiquis authored Mar 14, 2023
2 parents 7673539 + 510d52b commit 7de7f5f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
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

0 comments on commit 7de7f5f

Please sign in to comment.