diff options
author | makeefu <makeefu@ya.ru> | 2021-05-17 18:19:26 +0300 |
---|---|---|
committer | makeefu <makeefu@ya.ru> | 2021-05-17 18:19:26 +0300 |
commit | 1fc447503c83da8641b83b65b07429c6013ca780 (patch) | |
tree | 9b23adc7617b0cd68bc63dd1ae9738027d3a3354 /src/util | |
parent | 0b5ece8f8f6197ca5fa4670f54ca70dedf33da61 (diff) |
sort mtime changed
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/sort.rs | 142 |
1 files changed, 36 insertions, 106 deletions
diff --git a/src/util/sort.rs b/src/util/sort.rs index f94cc8b..f637a4b 100644 --- a/src/util/sort.rs +++ b/src/util/sort.rs @@ -6,40 +6,12 @@ use serde_derive::Deserialize; use crate::fs::JoshutoDirEntry; -#[derive(Clone, Debug)] -pub struct SortTypes { - pub list: std::collections::LinkedList<SortType>, -} - -impl SortTypes { - pub fn reorganize(&mut self, st: SortType) { - self.list.push_front(st); - self.list.pop_back(); - } - - pub fn cmp( - &self, - f1: &JoshutoDirEntry, - f2: &JoshutoDirEntry, - sort_option: &SortOption, - ) -> cmp::Ordering { - for st in &self.list { - let res = st.cmp(f1, f2, sort_option); - if res != cmp::Ordering::Equal { - return res; - } - } - cmp::Ordering::Equal - } -} - -#[derive(Clone, Copy, Debug, Deserialize, PartialEq)] +#[derive(Clone, Copy, Debug, Deserialize)] pub enum SortType { Lexical, Mtime, Natural, Size, - Ext, } impl SortType { @@ -49,7 +21,6 @@ impl SortType { "mtime" => Some(SortType::Mtime), "natural" => Some(SortType::Natural), "size" => Some(SortType::Size), - "ext" => Some(SortType::Ext), _ => None, } } @@ -59,24 +30,8 @@ impl SortType { SortType::Mtime => "mtime", SortType::Natural => "natural", SortType::Size => "size", - SortType::Ext => "ext", } } - pub fn cmp( - &self, - f1: &JoshutoDirEntry, - f2: &JoshutoDirEntry, - sort_option: &SortOption, - ) -> cmp::Ordering { - let res = match &self { - SortType::Natural => natural_sort(f1, f2, sort_option), - SortType::Lexical => lexical_sort(f1, f2, sort_option), - SortType::Size => size_sort(f1, f2), - SortType::Mtime => mtime_sort(f1, f2), - SortType::Ext => ext_sort(f1, f2), - }; - return res; - } } impl std::fmt::Display for SortType { @@ -91,14 +46,9 @@ pub struct SortOption { pub case_sensitive: bool, pub reverse: bool, pub sort_method: SortType, - pub sort_methods: SortTypes, } impl SortOption { - pub fn set_sort_method(&mut self, method: SortType) { - self.sort_methods.reorganize(method); - } - pub fn compare(&self, f1: &JoshutoDirEntry, f2: &JoshutoDirEntry) -> cmp::Ordering { if self.directories_first { let f1_isdir = f1.file_path().is_dir(); @@ -111,33 +61,51 @@ impl SortOption { } } - // let mut res = self.sort_method.cmp(f1, f2, &self); - let mut res = self.sort_methods.cmp(f1, f2, &self); + let mut res = match self.sort_method { + SortType::Lexical => { + let f1_name = f1.file_name(); + let f2_name = f2.file_name(); + if self.case_sensitive { + f1_name.cmp(&f2_name) + } else { + let f1_name = f1_name.to_lowercase(); + let f2_name = f2_name.to_lowercase(); + f1_name.cmp(&f2_name) + } + } + SortType::Natural => { + let f1_name = f1.file_name(); + let f2_name = f2.file_name(); + if self.case_sensitive { + alphanumeric_sort::compare_str(&f1_name, &f2_name) + } else { + let f1_name = f1_name.to_lowercase(); + let f2_name = f2_name.to_lowercase(); + alphanumeric_sort::compare_str(&f1_name, &f2_name) + } + } + SortType::Mtime => mtime_sort(f1, f2), + SortType::Size => size_sort(f1, f2), + }; + if self.reverse { res = match res { cmp::Ordering::Less => cmp::Ordering::Greater, cmp::Ordering::Greater => cmp::Ordering::Less, s => s, }; - }; + } res } } impl std::default::Default for SortOption { fn default() -> Self { - let mut sort_methods = std::collections::LinkedList::new(); - sort_methods.push_back(SortType::Ext); - sort_methods.push_back(SortType::Size); - sort_methods.push_back(SortType::Mtime); - sort_methods.push_back(SortType::Lexical); - sort_methods.push_back(SortType::Natural); SortOption { directories_first: true, case_sensitive: false, reverse: false, sort_method: SortType::Natural, - sort_methods: SortTypes { list: sort_methods }, } } } @@ -152,54 +120,16 @@ fn mtime_sort(file1: &JoshutoDirEntry, file2: &JoshutoDirEntry) -> cmp::Ordering let f1_mtime: time::SystemTime = f1_meta.modified()?; let f2_mtime: time::SystemTime = f2_meta.modified()?; - - Ok(if f1_mtime >= f2_mtime { - cmp::Ordering::Less - } else { - cmp::Ordering::Greater - }) + Ok(f1_mtime.cmp(&f2_mtime)) + // Ok(if f1_mtime >= f2_mtime { + // cmp::Ordering::Less + // } else { + // cmp::Ordering::Greater + // }) } - compare(&file1, &file2).unwrap_or(cmp::Ordering::Less) + compare(&file1, &file2).unwrap_or(cmp::Ordering::Equal) } fn size_sort(file1: &JoshutoDirEntry, file2: &JoshutoDirEntry) -> cmp::Ordering { file1.metadata.len().cmp(&file2.metadata.len()) } - -fn ext_sort(file1: &JoshutoDirEntry, file2: &JoshutoDirEntry) -> cmp::Ordering { - let f1_ext = file1.get_ext(); - let f2_ext = file2.get_ext(); - alphanumeric_sort::compare_str(&f1_ext, &f2_ext) -} - -fn lexical_sort( - f1: &JoshutoDirEntry, - f2: &JoshutoDirEntry, - sort_option: &SortOption, -) -> cmp::Ordering { - let f1_name = f1.file_name(); - let f2_name = f2.file_name(); - if sort_option.case_sensitive { - f1_name.cmp(&f2_name) - } else { - let f1_name = f1_name.to_lowercase(); - let f2_name = f2_name.to_lowercase(); - f1_name.cmp(&f2_name) - } -} - -fn natural_sort( - f1: &JoshutoDirEntry, - f2: &JoshutoDirEntry, - sort_option: &SortOption, -) -> cmp::Ordering { - let f1_name = f1.file_name(); - let f2_name = f2.file_name(); - if sort_option.case_sensitive { - alphanumeric_sort::compare_str(&f1_name, &f2_name) - } else { - let f1_name = f1_name.to_lowercase(); - let f2_name = f2_name.to_lowercase(); - alphanumeric_sort::compare_str(&f1_name, &f2_name) - } -} |