Skip to content
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

Add option to sort directories first (--dirs-first) #38

Merged
merged 2 commits into from
Mar 5, 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
8 changes: 8 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ pub struct Clargs {
#[arg(short, long, value_enum, default_value_t = Order::None)]
sort: Order,

/// Always sorts directories above files
#[arg(long)]
dirs_first: bool,

/// Traverse symlink directories and consider their disk usage; disabled by default
#[arg(short = 'S', long)]
follow_links: bool,
Expand Down Expand Up @@ -96,6 +100,10 @@ impl Clargs {
self.sort
}

pub fn dirs_first(&self) -> bool {
self.dirs_first
}

/// The max depth to print. Note that all directories are fully traversed to compute file
/// sizes; this just determines how much to print.
pub fn level(&self) -> Option<usize> {
Expand Down
69 changes: 56 additions & 13 deletions src/fs/erdtree/order.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,58 @@
use crate::cli;
use super::node::Node;
use std::{
convert::From,
cmp::Ordering,
};
use crate::cli;
use std::{cmp::Ordering, convert::From};

/// Order in which to print nodes.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Order {
pub enum SortType {
Name,
Size,
None,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Order {
sort: SortType,
dir_first: bool,
}

impl Order {
/// Yields function pointer to the appropriate `Node` comparator.
pub fn comparator(&self) -> Option<fn(a: &Node, b: &Node) -> Ordering> {
pub fn comparator(&self) -> Option<Box<dyn Fn(&Node, &Node) -> Ordering + '_>> {
if self.dir_first {
Some(Box::new(|a, b| {
Self::dir_comparator(a, b, self.sort.comparator())
}))
} else {
self.sort.comparator()
}
}

fn dir_comparator(
a: &Node,
b: &Node,
fallback: Option<impl Fn(&Node, &Node) -> Ordering>,
) -> Ordering {
match (a.is_dir(), b.is_dir()) {
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
_ => {
if let Some(sort) = fallback {
sort(a, b)
} else {
Ordering::Equal
}
}
}
}
}

impl SortType {
/// Yields function pointer to the appropriate `Node` comparator.
pub fn comparator(&self) -> Option<Box<dyn Fn(&Node, &Node) -> Ordering>> {
match self {
Self::Name => Some(Self::name_comparator),
Self::Size => Some(Self::size_comparator),
Self::Name => Some(Box::new(Self::name_comparator)),
Self::Size => Some(Box::new(Self::size_comparator)),
_ => None,
}
}
Expand All @@ -37,12 +71,21 @@ impl Order {
}
}

impl From<cli::Order> for Order {
impl From<(cli::Order, bool)> for Order {
fn from((order, dir_first): (cli::Order, bool)) -> Self {
Order {
sort: order.into(),
dir_first,
}
}
}

impl From<cli::Order> for SortType {
fn from(ord: cli::Order) -> Self {
match ord {
cli::Order::Name => Order::Name,
cli::Order::Size => Order::Size,
cli::Order::None => Order::None
cli::Order::Name => SortType::Name,
cli::Order::Size => SortType::Size,
cli::Order::None => SortType::None,
}
}
}
2 changes: 1 addition & 1 deletion src/fs/erdtree/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl TryFrom<Clargs> for Tree {

fn try_from(clargs: Clargs) -> Result<Self, Self::Error> {
let walker = WalkParallel::try_from(&clargs)?;
let order = Order::from(clargs.sort());
let order = Order::from((clargs.sort(), clargs.dirs_first()));
let tree = Tree::new(walker, order, clargs.level(), clargs.icons)?;
Ok(tree)
}
Expand Down
1 change: 1 addition & 0 deletions tests/data/lipsum/lipsum.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
13 changes: 10 additions & 3 deletions tests/glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ fn glob() {
utils::run_cmd(&["--sort", "name", "--glob", "*.txt", "tests/data"]),
indoc!(
"
data (652.00 B)
data (1.10 KB)
├─ dream_cycle (308.00 B)
│ └─ polaris.txt (308.00 B)
├─ lipsum (446.00 B)
│ └─ lipsum.txt (446.00 B)
├─ necronomicon.txt (83.00 B)
├─ nemesis.txt (161.00 B)
├─ nylarlathotep.txt (100.00 B)
Expand All @@ -27,6 +29,7 @@ fn glob_negative() {
"
data (143.00 B)
├─ dream_cycle
├─ lipsum
└─ the_yellow_king (143.00 B)
└─ cassildas_song.md (143.00 B)"
)
Expand All @@ -46,9 +49,11 @@ fn glob_case_insensitive() {
]),
indoc!(
"
data (652.00 B)
data (1.10 KB)
├─ dream_cycle (308.00 B)
│ └─ polaris.txt (308.00 B)
├─ lipsum (446.00 B)
│ └─ lipsum.txt (446.00 B)
├─ necronomicon.txt (83.00 B)
├─ nemesis.txt (161.00 B)
├─ nylarlathotep.txt (100.00 B)
Expand All @@ -63,9 +68,11 @@ fn iglob() {
utils::run_cmd(&["--sort", "name", "--iglob", "*.TXT", "tests/data"]),
indoc!(
"
data (652.00 B)
data (1.10 KB)
├─ dream_cycle (308.00 B)
│ └─ polaris.txt (308.00 B)
├─ lipsum (446.00 B)
│ └─ lipsum.txt (446.00 B)
├─ necronomicon.txt (83.00 B)
├─ nemesis.txt (161.00 B)
├─ nylarlathotep.txt (100.00 B)
Expand Down
3 changes: 2 additions & 1 deletion tests/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ fn level() {
utils::run_cmd(&["--sort", "name", "--level", "1", "tests/data"]),
indoc!(
"
data (795.00 B)
data (1.24 KB)
├─ dream_cycle (308.00 B)
├─ lipsum (446.00 B)
├─ necronomicon.txt (83.00 B)
├─ nemesis.txt (161.00 B)
├─ nylarlathotep.txt (100.00 B)
Expand Down
50 changes: 48 additions & 2 deletions tests/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ fn sort_name() {
utils::run_cmd(&["--sort", "name", "tests/data"]),
indoc!(
"
data (795.00 B)
data (1.24 KB)
├─ dream_cycle (308.00 B)
│ └─ polaris.txt (308.00 B)
├─ lipsum (446.00 B)
│ └─ lipsum.txt (446.00 B)
├─ necronomicon.txt (83.00 B)
├─ nemesis.txt (161.00 B)
├─ nylarlathotep.txt (100.00 B)
Expand All @@ -21,13 +23,36 @@ fn sort_name() {
)
}

#[test]
fn sort_name_dir_first() {
assert_eq!(
utils::run_cmd(&["--sort", "name", "--dirs-first", "tests/data"]),
indoc!(
"
data (1.24 KB)
├─ dream_cycle (308.00 B)
│ └─ polaris.txt (308.00 B)
├─ lipsum (446.00 B)
│ └─ lipsum.txt (446.00 B)
├─ the_yellow_king (143.00 B)
│ └─ cassildas_song.md (143.00 B)
├─ necronomicon.txt (83.00 B)
├─ nemesis.txt (161.00 B)
└─ nylarlathotep.txt (100.00 B)"
),
"Failed to sort by directory and alphabetically by file name"
)
}

#[test]
fn sort_size() {
assert_eq!(
utils::run_cmd(&["--sort", "size", "tests/data"]),
indoc!(
"
data (795.00 B)
data (1.24 KB)
├─ lipsum (446.00 B)
│ └─ lipsum.txt (446.00 B)
├─ dream_cycle (308.00 B)
│ └─ polaris.txt (308.00 B)
├─ nemesis.txt (161.00 B)
Expand All @@ -39,3 +64,24 @@ fn sort_size() {
"Failed to sort by descending size"
)
}

#[test]
fn sort_size_dir_first() {
assert_eq!(
utils::run_cmd(&["--sort", "size", "--dirs-first", "tests/data"]),
indoc!(
"
data (1.24 KB)
├─ lipsum (446.00 B)
│ └─ lipsum.txt (446.00 B)
├─ dream_cycle (308.00 B)
│ └─ polaris.txt (308.00 B)
├─ the_yellow_king (143.00 B)
│ └─ cassildas_song.md (143.00 B)
├─ nemesis.txt (161.00 B)
├─ nylarlathotep.txt (100.00 B)
└─ necronomicon.txt (83.00 B)"
),
"Failed to sort by directory and descending size"
)
}