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

Time sort #175

Merged
merged 1 commit into from
May 24, 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
2 changes: 1 addition & 1 deletion src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub struct Context {
#[arg(short = 'P', long)]
pub prune: bool,

/// Sort-order to display directory content
/// How to sort entries
#[arg(short, long, value_enum, default_value_t)]
pub sort: sort::Type,

Expand Down
29 changes: 15 additions & 14 deletions src/context/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,30 @@ pub enum Type {
/// Sort entries by file name in lexicographical order.
Name,
/// Sort entries by file name in reversed lexicographical order.
NameRev,
Rname,

/// Sort entries by size smallest to largest, top to bottom
#[default]
Size,

/// Sort entries by size largest to smallest, bottom to top
SizeRev,
// Sort entries by newer to older Accessing Date
//Access,
Rsize,

// Sort entries by older to newer Accessing Date
//AccessRev,
/// Sort entries by newer to older Accessing Date
Access,

// Sort entries by newer to older Creation Date
//Creation,
/// Sort entries by older to newer Accessing Date
Raccess,

// Sort entries by older to newer Creation Date
//CreationRev,
/// Sort entries by newer to older Creation Date
Create,

// Sort entries by newer to older Alteration Date
//Modification,
/// Sort entries by older to newer Creation Date
Rcreate,

// Sort entries by older to newer Alteration Date
//ModificationRev,
/// Sort entries by newer to older Alteration Date
Mod,

/// Sort entries by older to newer Alteration Date
Rmod,
}
128 changes: 61 additions & 67 deletions src/tree/node/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,79 +52,73 @@ fn dir_last_comparator(
fn base_comparator(sort_type: sort::Type) -> Box<NodeComparator> {
Box::new(match sort_type {
sort::Type::Name => naming::comparator,
sort::Type::NameRev => naming::rev_comparator,

sort::Type::Rname => naming::rev_comparator,
sort::Type::Size => sizing::comparator,
sort::Type::SizeRev => sizing::rev_comparator,
sort::Type::Rsize => sizing::rev_comparator,
sort::Type::Access => time_stamping::accessed::comparator,
sort::Type::Raccess => time_stamping::accessed::rev_comparator,
sort::Type::Create => time_stamping::created::comparator,
sort::Type::Rcreate => time_stamping::created::rev_comparator,
sort::Type::Mod => time_stamping::modified::comparator,
sort::Type::Rmod => time_stamping::modified::rev_comparator,
})
}

_ => unreachable!("Not yet supported.")
mod time_stamping {
pub mod accessed {
use crate::tree::node::Node;
use core::cmp::Ordering;
use std::time::SystemTime;

/// Comparator that sorts [Node]s by Last Access timestamp, newer to older.
pub fn comparator(a: &Node, b: &Node) -> Ordering {
let a_stamp = a.accessed().unwrap_or_else(SystemTime::now);
let b_stamp = b.accessed().unwrap_or_else(SystemTime::now);
a_stamp.cmp(&b_stamp)
}

//sort::Type::Access => time_stamping::accessed::comparator,
//sort::Type::AccessRev => time_stamping::accessed::rev_comparator,
/// Comparator that sorts [Node]s by Access timestamp, older to newer.
pub fn rev_comparator(a: &Node, b: &Node) -> Ordering {
comparator(b, a)
}
}

//sort::Type::Creation => time_stamping::created::comparator,
//sort::Type::CreationRev => time_stamping::created::rev_comparator,
pub mod created {
use crate::tree::node::Node;
use core::cmp::Ordering;
use std::time::SystemTime;

//sort::Type::Modification => time_stamping::modified::comparator,
//sort::Type::ModificationRev => time_stamping::modified::rev_comparator,
})
}
/// Comparator that sorts [Node]s by Creation timestamp, newer to older.
pub fn comparator(a: &Node, b: &Node) -> Ordering {
let a_stamp = a.created().unwrap_or_else(SystemTime::now);
let b_stamp = b.created().unwrap_or_else(SystemTime::now);
a_stamp.cmp(&b_stamp)
}

//mod time_stamping {
//pub mod accessed {
//use crate::render::tree::node::Node;
//use core::cmp::Ordering;
//use std::time::SystemTime;

///// Comparator that sorts [Node]s by Last Access timestamp, newer to older.
//pub fn comparator(a: &Node, b: &Node) -> Ordering {
//let a_stamp = a.accessed().unwrap_or_else(SystemTime::now);
//let b_stamp = b.accessed().unwrap_or_else(SystemTime::now);
//a_stamp.cmp(&b_stamp)
//}

///// Comparator that sorts [Node]s by Access timestamp, older to newer.
//pub fn rev_comparator(a: &Node, b: &Node) -> Ordering {
//comparator(b, a)
//}
//}

//pub mod created {
//use crate::render::tree::node::Node;
//use core::cmp::Ordering;
//use std::time::SystemTime;

///// Comparator that sorts [Node]s by Creation timestamp, newer to older.
//pub fn comparator(a: &Node, b: &Node) -> Ordering {
//let a_stamp = a.created().unwrap_or_else(SystemTime::now);
//let b_stamp = b.created().unwrap_or_else(SystemTime::now);
//a_stamp.cmp(&b_stamp)
//}

///// Comparator that sorts [Node]s by Creation timestamp, older to newer.
//pub fn rev_comparator(a: &Node, b: &Node) -> Ordering {
//comparator(b, a)
//}
//}

//pub mod modified {
//use crate::render::tree::node::Node;
//use core::cmp::Ordering;
//use std::time::SystemTime;

///// Comparator that sorts [Node]s by Alteration timestamp, newer to older.
//pub fn comparator(a: &Node, b: &Node) -> Ordering {
//let a_stamp = a.modified().unwrap_or_else(SystemTime::now);
//let b_stamp = b.modified().unwrap_or_else(SystemTime::now);
//a_stamp.cmp(&b_stamp)
//}

///// Comparator that sorts [Node]s by Alteration timestamp, older to newer.
//pub fn rev_comparator(a: &Node, b: &Node) -> Ordering {
//comparator(b, a)
//}
//}
//}
/// Comparator that sorts [Node]s by Creation timestamp, older to newer.
pub fn rev_comparator(a: &Node, b: &Node) -> Ordering {
comparator(b, a)
}
}

pub mod modified {
use crate::tree::node::Node;
use core::cmp::Ordering;
use std::time::SystemTime;

/// Comparator that sorts [Node]s by Alteration timestamp, newer to older.
pub fn comparator(a: &Node, b: &Node) -> Ordering {
let a_stamp = a.modified().unwrap_or_else(SystemTime::now);
let b_stamp = b.modified().unwrap_or_else(SystemTime::now);
a_stamp.cmp(&b_stamp)
}

/// Comparator that sorts [Node]s by Alteration timestamp, older to newer.
pub fn rev_comparator(a: &Node, b: &Node) -> Ordering {
comparator(b, a)
}
}
}

mod sizing {
use crate::disk_usage::file_size::FileSize;
Expand Down
2 changes: 1 addition & 1 deletion tests/flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn flat_human() {
assert_eq!(
utils::run_cmd(&["--layout", "flat", "--human", "tests/data"]),
indoc!(
"143 B the_yellow_king/cassildas_song.md
"143 B the_yellow_king/cassildas_song.md
143 B the_yellow_king
100 B nylarlathotep.txt
161 B nemesis.txt
Expand Down