summaryrefslogtreecommitdiffstats
path: root/src/config/option/sort_option.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config/option/sort_option.rs')
-rw-r--r--src/config/option/sort_option.rs54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/config/option/sort_option.rs b/src/config/option/sort_option.rs
new file mode 100644
index 0000000..e83b049
--- /dev/null
+++ b/src/config/option/sort_option.rs
@@ -0,0 +1,54 @@
+use std::cmp;
+use std::convert::From;
+
+use crate::config::option::{SortType, SortTypes};
+use crate::fs::JoshutoDirEntry;
+
+#[derive(Clone, Debug)]
+pub struct SortOption {
+ pub directories_first: bool,
+ pub case_sensitive: bool,
+ pub reverse: bool,
+ 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();
+ let f2_isdir = f2.file_path().is_dir();
+
+ if f1_isdir && !f2_isdir {
+ return cmp::Ordering::Less;
+ } else if !f1_isdir && f2_isdir {
+ return cmp::Ordering::Greater;
+ }
+ }
+
+ // let mut res = self.sort_method.cmp(f1, f2, &self);
+ let mut res = self.sort_methods.cmp(f1, f2, &self);
+ 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 {
+ SortOption {
+ directories_first: true,
+ case_sensitive: false,
+ reverse: false,
+ sort_methods: SortTypes::default(),
+ }
+ }
+}