summaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2021-10-20 15:52:41 -0400
committerJeff Zhao <jeff.no.zhao@gmail.com>2021-10-20 15:52:41 -0400
commit6e2164aeaaf02bf61099e773a0341a38652c5da4 (patch)
tree69c90c589119b1058097993f3a0221aba6af4100 /src/util
parentd151d64809f918c59b4cd5c18ce71d05b165cd84 (diff)
rework config structure
Diffstat (limited to 'src/util')
-rw-r--r--src/util/display_option.rs122
-rw-r--r--src/util/mod.rs3
-rw-r--r--src/util/sort_option.rs53
-rw-r--r--src/util/sort_type.rs160
4 files changed, 0 insertions, 338 deletions
diff --git a/src/util/display_option.rs b/src/util/display_option.rs
deleted file mode 100644
index bc2d247..0000000
--- a/src/util/display_option.rs
+++ /dev/null
@@ -1,122 +0,0 @@
-use std::fs;
-
-use tui::layout::Constraint;
-
-use crate::util::sort_option::SortOption;
-
-pub const fn default_column_ratio() -> (usize, usize, usize) {
- (1, 3, 4)
-}
-
-#[derive(Clone, Debug)]
-pub struct DisplayOption {
- pub _automatically_count_files: bool,
- pub _collapse_preview: bool,
- pub column_ratio: (usize, usize, usize),
- pub _show_borders: bool,
- pub _show_hidden: bool,
- pub _show_icons: bool,
- pub _show_preview: bool,
- pub _sort_options: SortOption,
- pub _tilde_in_titlebar: bool,
- pub default_layout: [Constraint; 3],
- pub no_preview_layout: [Constraint; 3],
-}
-
-impl DisplayOption {
- pub fn automatically_count_files(&self) -> bool {
- self._automatically_count_files
- }
-
- pub fn collapse_preview(&self) -> bool {
- self._collapse_preview
- }
-
- pub fn show_borders(&self) -> bool {
- self._show_borders
- }
-
- pub fn show_hidden(&self) -> bool {
- self._show_hidden
- }
-
- pub fn show_icons(&self) -> bool {
- self._show_icons
- }
-
- #[allow(dead_code)]
- pub fn show_preview(&self) -> bool {
- self._show_preview
- }
-
- pub fn set_show_hidden(&mut self, show_hidden: bool) {
- self._show_hidden = show_hidden;
- }
-
- pub fn sort_options_ref(&self) -> &SortOption {
- &self._sort_options
- }
-
- pub fn sort_options_mut(&mut self) -> &mut SortOption {
- &mut self._sort_options
- }
-
- pub fn tilde_in_titlebar(&self) -> bool {
- self._tilde_in_titlebar
- }
-
- pub fn filter_func(&self) -> fn(&Result<fs::DirEntry, std::io::Error>) -> bool {
- if self.show_hidden() {
- no_filter
- } else {
- filter_hidden
- }
- }
-}
-
-impl std::default::Default for DisplayOption {
- fn default() -> Self {
- let column_ratio = default_column_ratio();
-
- let total = (column_ratio.0 + column_ratio.1 + column_ratio.2) as u32;
- let default_layout = [
- Constraint::Ratio(column_ratio.0 as u32, total),
- Constraint::Ratio(column_ratio.1 as u32, total),
- Constraint::Ratio(column_ratio.2 as u32, total),
- ];
- let no_preview_layout = [
- Constraint::Ratio(column_ratio.0 as u32, total),
- Constraint::Ratio(column_ratio.1 as u32 + column_ratio.2 as u32, total),
- Constraint::Ratio(0, total),
- ];
-
- Self {
- _automatically_count_files: false,
- _collapse_preview: true,
- column_ratio,
- _show_borders: true,
- _show_hidden: false,
- _show_icons: false,
- _show_preview: true,
- _sort_options: SortOption::default(),
- _tilde_in_titlebar: true,
- default_layout,
- no_preview_layout,
- }
- }
-}
-
-const fn no_filter(_: &Result<fs::DirEntry, std::io::Error>) -> bool {
- true
-}
-
-fn filter_hidden(result: &Result<fs::DirEntry, std::io::Error>) -> bool {
- match result {
- Err(_) => true,
- Ok(entry) => {
- let file_name = entry.file_name();
- let lossy_string = file_name.as_os_str().to_string_lossy();
- !lossy_string.starts_with('.')
- }
- }
-}
diff --git a/src/util/mod.rs b/src/util/mod.rs
index ae3a590..2bf95d2 100644
--- a/src/util/mod.rs
+++ b/src/util/mod.rs
@@ -1,15 +1,12 @@
#[cfg(feature = "devicons")]
pub mod devicons;
-pub mod display_option;
pub mod format;
pub mod input;
pub mod keyparse;
pub mod name_resolution;
pub mod search;
pub mod select;
-pub mod sort_option;
-pub mod sort_type;
pub mod string;
pub mod style;
pub mod to_string;
diff --git a/src/util/sort_option.rs b/src/util/sort_option.rs
deleted file mode 100644
index df38702..0000000
--- a/src/util/sort_option.rs
+++ /dev/null
@@ -1,53 +0,0 @@
-use std::cmp;
-
-use crate::fs::JoshutoDirEntry;
-use crate::util::sort_type::{SortType, SortTypes};
-
-#[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(),
- }
- }
-}
diff --git a/src/util/sort_type.rs b/src/util/sort_type.rs
deleted file mode 100644
index 6e4180e..0000000
--- a/src/util/sort_type.rs
+++ /dev/null
@@ -1,160 +0,0 @@
-use std::cmp;
-use std::collections::VecDeque;
-use std::fs;
-use std::time;
-
-use serde_derive::Deserialize;
-
-use crate::fs::JoshutoDirEntry;
-use crate::util::sort_option::SortOption;
-
-#[derive(Clone, Copy, Debug, Deserialize, PartialEq)]
-pub enum SortType {
- Lexical,
- Mtime,
- Natural,
- Size,
- Ext,
-}
-
-impl SortType {
- pub fn parse(s: &str) -> Option<Self> {
- match s {
- "lexical" => Some(SortType::Lexical),
- "mtime" => Some(SortType::Mtime),
- "natural" => Some(SortType::Natural),
- "size" => Some(SortType::Size),
- "ext" => Some(SortType::Ext),
- _ => None,
- }
- }
- pub const fn as_str(&self) -> &str {
- match *self {
- SortType::Lexical => "lexical",
- 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 {
- 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),
- }
- }
-}
-
-impl std::fmt::Display for SortType {
- fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
- write!(f, "{}", self.as_str())
- }
-}
-
-#[derive(Clone, Debug)]
-pub struct SortTypes {
- pub list: VecDeque<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
- }
-}
-
-impl std::default::Default for SortTypes {
- fn default() -> Self {
- let list: VecDeque<SortType> = vec![
- SortType::Natural,
- SortType::Lexical,
- SortType::Size,
- SortType::Ext,
- SortType::Mtime,
- ]
- .into_iter()
- .collect();
-
- Self { list }
- }
-}
-
-fn mtime_sort(file1: &JoshutoDirEntry, file2: &JoshutoDirEntry) -> cmp::Ordering {
- fn compare(
- file1: &JoshutoDirEntry,
- file2: &JoshutoDirEntry,
- ) -> Result<cmp::Ordering, std::io::Error> {
- let f1_meta: fs::Metadata = std::fs::metadata(file1.file_path())?;
- let f2_meta: fs::Metadata = std::fs::metadata(file2.file_path())?;
-
- let f1_mtime: time::SystemTime = f1_meta.modified()?;
- let f2_mtime: time::SystemTime = f2_meta.modified()?;
- Ok(f1_mtime.cmp(&f2_mtime))
- }
- 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)
- }
-}