diff --git a/src/context/mod.rs b/src/context/mod.rs index abb3fe8a..e4ec857f 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -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, diff --git a/src/context/sort.rs b/src/context/sort.rs index 96e75049..60a34a6f 100644 --- a/src/context/sort.rs +++ b/src/context/sort.rs @@ -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, } diff --git a/src/tree/node/cmp.rs b/src/tree/node/cmp.rs index c135b9a8..9359fcbc 100644 --- a/src/tree/node/cmp.rs +++ b/src/tree/node/cmp.rs @@ -52,79 +52,73 @@ fn dir_last_comparator( fn base_comparator(sort_type: sort::Type) -> Box { 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; diff --git a/tests/flat.rs b/tests/flat.rs index 2f3efb24..1d395601 100644 --- a/tests/flat.rs +++ b/tests/flat.rs @@ -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