diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-04-02 21:12:22 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-04-02 21:12:22 -0400 |
commit | 37fa8efbd8691d89fcd22727e3720621fff3680c (patch) | |
tree | 8723b96b26f0d30ffa66f8408d5ca2cd93e4a858 | |
parent | 554f06d72aeb639548314336b2318e14f8ffcc7e (diff) |
refactor sorting structs
- filtering hidden files is de-coupled from creating direntries
-rw-r--r-- | src/commands/change_directory.rs | 6 | ||||
-rw-r--r-- | src/commands/delete_files.rs | 2 | ||||
-rw-r--r-- | src/commands/open_file.rs | 2 | ||||
-rw-r--r-- | src/commands/parent_directory.rs | 2 | ||||
-rw-r--r-- | src/commands/reload_dir.rs | 2 | ||||
-rw-r--r-- | src/commands/rename_file.rs | 2 | ||||
-rw-r--r-- | src/commands/show_hidden.rs | 8 | ||||
-rw-r--r-- | src/commands/tab_operations.rs | 2 | ||||
-rw-r--r-- | src/commands/tab_switch.rs | 2 | ||||
-rw-r--r-- | src/config/config.rs | 41 | ||||
-rw-r--r-- | src/history.rs | 16 | ||||
-rw-r--r-- | src/preview.rs | 2 | ||||
-rw-r--r-- | src/run.rs | 8 | ||||
-rw-r--r-- | src/sort.rs | 83 | ||||
-rw-r--r-- | src/structs.rs | 22 | ||||
-rw-r--r-- | src/tab.rs | 19 |
16 files changed, 104 insertions, 115 deletions
diff --git a/src/commands/change_directory.rs b/src/commands/change_directory.rs index ae77964..8406940 100644 --- a/src/commands/change_directory.rs +++ b/src/commands/change_directory.rs @@ -47,11 +47,11 @@ impl ChangeDirectory { curr_tab .history - .populate_to_root(&curr_tab.curr_path, &context.config_t.sort_type); + .populate_to_root(&curr_tab.curr_path, &context.config_t.sort_option); curr_tab.curr_list = match curr_tab .history - .pop_or_create(&curr_tab.curr_path, &context.config_t.sort_type) + .pop_or_create(&curr_tab.curr_path, &context.config_t.sort_option) { Ok(s) => Some(s), Err(e) => { @@ -63,7 +63,7 @@ impl ChangeDirectory { if let Some(parent) = curr_tab.curr_path.parent() { curr_tab.parent_list = match curr_tab .history - .pop_or_create(&parent, &context.config_t.sort_type) + .pop_or_create(&parent, &context.config_t.sort_option) { Ok(s) => Some(s), Err(e) => { diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs index 1875b39..f450e57 100644 --- a/src/commands/delete_files.rs +++ b/src/commands/delete_files.rs @@ -63,7 +63,7 @@ impl JoshutoRunnable for DeleteFiles { } let curr_tab = &mut context.tabs[context.curr_tab_index]; - curr_tab.reload_contents(&context.config_t.sort_type); + curr_tab.reload_contents(&context.config_t.sort_option); curr_tab.refresh( &view, &context.config_t, diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index 3cbdc2c..ad71b51 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -65,7 +65,7 @@ impl OpenFile { curr_tab.curr_list = match curr_tab .history - .pop_or_create(&path, &context.config_t.sort_type) + .pop_or_create(&path, &context.config_t.sort_option) { Ok(s) => Some(s), Err(e) => { diff --git a/src/commands/parent_directory.rs b/src/commands/parent_directory.rs index 4542af7..fe219df 100644 --- a/src/commands/parent_directory.rs +++ b/src/commands/parent_directory.rs @@ -33,7 +33,7 @@ impl ParentDirectory { Some(parent) => { curr_tab.parent_list = match curr_tab .history - .pop_or_create(&parent, &context.config_t.sort_type) + .pop_or_create(&parent, &context.config_t.sort_option) { Ok(s) => Some(s), Err(e) => { diff --git a/src/commands/reload_dir.rs b/src/commands/reload_dir.rs index d842b00..7b03ded 100644 --- a/src/commands/reload_dir.rs +++ b/src/commands/reload_dir.rs @@ -16,7 +16,7 @@ impl ReloadDirList { pub fn reload(context: &mut JoshutoContext, view: &JoshutoView) { let curr_tab = &mut context.tabs[context.curr_tab_index]; - curr_tab.reload_contents(&context.config_t.sort_type); + curr_tab.reload_contents(&context.config_t.sort_option); curr_tab.refresh( view, &context.config_t, diff --git a/src/commands/rename_file.rs b/src/commands/rename_file.rs index 2165277..9a07de1 100644 --- a/src/commands/rename_file.rs +++ b/src/commands/rename_file.rs @@ -71,7 +71,7 @@ impl RenameFile { Ok(_) => { let curr_tab = &mut context.tabs[context.curr_tab_index]; if let Some(ref mut s) = curr_tab.curr_list { - s.update_contents(&context.config_t.sort_type).unwrap(); + s.update_contents(&context.config_t.sort_option).unwrap(); } curr_tab.refresh_curr(&view.mid_win, context.config_t.scroll_offset); } diff --git a/src/commands/show_hidden.rs b/src/commands/show_hidden.rs index 12f08c5..be41264 100644 --- a/src/commands/show_hidden.rs +++ b/src/commands/show_hidden.rs @@ -14,12 +14,12 @@ impl ToggleHiddenFiles { "toggle_hidden" } pub fn toggle_hidden(context: &mut JoshutoContext) { - let opposite = !context.config_t.sort_type.show_hidden(); - context.config_t.sort_type.set_show_hidden(opposite); + let opposite = !context.config_t.sort_option.show_hidden; + context.config_t.sort_option.show_hidden = opposite; for tab in &mut context.tabs { tab.history.depecrate_all_entries(); - tab.reload_contents(&context.config_t.sort_type); + tab.reload_contents(&context.config_t.sort_option); } } } @@ -36,7 +36,7 @@ impl JoshutoRunnable for ToggleHiddenFiles { fn execute(&self, context: &mut JoshutoContext, view: &JoshutoView) { Self::toggle_hidden(context); let curr_tab = &mut context.tabs[context.curr_tab_index]; - curr_tab.reload_contents(&context.config_t.sort_type); + curr_tab.reload_contents(&context.config_t.sort_option); curr_tab.refresh( view, &context.config_t, diff --git a/src/commands/tab_operations.rs b/src/commands/tab_operations.rs index ca8e977..d1a15a9 100644 --- a/src/commands/tab_operations.rs +++ b/src/commands/tab_operations.rs @@ -27,7 +27,7 @@ impl NewTab { } }; - match JoshutoTab::new(curr_path, &context.config_t.sort_type) { + match JoshutoTab::new(curr_path, &context.config_t.sort_option) { Ok(tab) => { context.tabs.push(tab); context.curr_tab_index = context.tabs.len() - 1; diff --git a/src/commands/tab_switch.rs b/src/commands/tab_switch.rs index b0102b5..49e3210 100644 --- a/src/commands/tab_switch.rs +++ b/src/commands/tab_switch.rs @@ -26,7 +26,7 @@ impl TabSwitch { Ok(_) => { { let curr_tab = &mut context.tabs[context.curr_tab_index]; - curr_tab.reload_contents(&context.config_t.sort_type); + curr_tab.reload_contents(&context.config_t.sort_option); curr_tab.refresh( view, &context.config_t, diff --git a/src/config/config.rs b/src/config/config.rs index 6bfa627..2caa1b5 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -17,7 +17,7 @@ pub struct SortRawOption { pub struct JoshutoRawConfig { scroll_offset: Option<usize>, tilde_in_titlebar: Option<bool>, - sort_type: Option<String>, + sort_method: Option<String>, sort_option: Option<SortRawOption>, column_ratio: Option<[usize; 3]>, } @@ -28,7 +28,7 @@ impl JoshutoRawConfig { JoshutoRawConfig { scroll_offset: None, tilde_in_titlebar: None, - sort_type: None, + sort_method: None, sort_option: None, column_ratio: None, } @@ -45,6 +45,14 @@ impl Flattenable<JoshutoConfig> for JoshutoRawConfig { let scroll_offset: usize = self.scroll_offset.unwrap_or(6); let tilde_in_titlebar: bool = self.tilde_in_titlebar.unwrap_or(true); + let sort_method: sort::SortType = match self.sort_method { + Some(s) => match s.as_str() { + "mtime" => sort::SortType::SortMtime, + _ => sort::SortType::SortNatural, + }, + _ => sort::SortType::SortNatural, + }; + let show_hidden: bool; let case_sensitive: bool; let reverse: bool; @@ -66,26 +74,18 @@ impl Flattenable<JoshutoConfig> for JoshutoRawConfig { } let sort_option = sort::SortOption { - show_hidden, - directories_first, - case_sensitive, - reverse, - }; - - let sort_type: sort::SortType = match self.sort_type { - Some(s) => match s.as_str() { - "natural" => sort::SortType::SortNatural(sort_option), - "mtime" => sort::SortType::SortMtime(sort_option), - _ => sort::SortType::SortNatural(sort_option), - }, - _ => sort::SortType::SortNatural(sort_option), - }; + show_hidden, + directories_first, + case_sensitive, + reverse, + sort_method, + }; JoshutoConfig { scroll_offset, tilde_in_titlebar, - sort_type, column_ratio, + sort_option } } } @@ -94,7 +94,7 @@ impl Flattenable<JoshutoConfig> for JoshutoRawConfig { pub struct JoshutoConfig { pub scroll_offset: usize, pub tilde_in_titlebar: bool, - pub sort_type: sort::SortType, + pub sort_option: sort::SortOption, pub column_ratio: (usize, usize, usize), } @@ -105,13 +105,14 @@ impl JoshutoConfig { directories_first: true, case_sensitive: false, reverse: false, + sort_method: sort::SortType::SortNatural, }; - let sort_type = sort::SortType::SortNatural(sort_option); + JoshutoConfig { scroll_offset: 6, tilde_in_titlebar: true, - sort_type, + sort_option, column_ratio: (1, 3, 4), } } diff --git a/src/history.rs b/src/history.rs index 8ad6619..d803f88 100644 --- a/src/history.rs +++ b/src/history.rs @@ -15,11 +15,11 @@ impl DirHistory { } } - pub fn populate_to_root(&mut self, pathbuf: &PathBuf, sort_type: &sort::SortType) { + pub fn populate_to_root(&mut self, pathbuf: &PathBuf, sort_option: &sort::SortOption) { let mut ancestors = pathbuf.ancestors(); if let Some(mut ancestor) = ancestors.next() { for curr in ancestors { - match structs::JoshutoDirList::new(curr.to_path_buf().clone(), sort_type) { + match structs::JoshutoDirList::new(curr.to_path_buf().clone(), sort_option) { Ok(mut s) => { for (i, dirent) in s.contents.iter().enumerate() { if dirent.path == ancestor { @@ -39,18 +39,18 @@ impl DirHistory { pub fn pop_or_create( &mut self, path: &Path, - sort_type: &sort::SortType, + sort_option: &sort::SortOption, ) -> Result<structs::JoshutoDirList, std::io::Error> { match self.map.remove(&path.to_path_buf()) { Some(mut dir_entry) => { if dir_entry.need_update() { - dir_entry.update_contents(&sort_type)? + dir_entry.update_contents(&sort_option)? } Ok(dir_entry) } None => { let path_clone = path.to_path_buf(); - structs::JoshutoDirList::new(path_clone, &sort_type) + structs::JoshutoDirList::new(path_clone, &sort_option) } } } @@ -58,18 +58,18 @@ impl DirHistory { pub fn get_mut_or_create( &mut self, path: &Path, - sort_type: &sort::SortType, + sort_option: &sort::SortOption, ) -> Result<&mut structs::JoshutoDirList, std::io::Error> { let pathbuf = path.to_path_buf(); match self.map.entry(pathbuf.clone()) { Entry::Occupied(mut entry) => { let dir_entry = entry.get_mut(); if dir_entry.need_update() { - dir_entry.update_contents(&sort_type).unwrap(); + dir_entry.update_contents(&sort_option).unwrap(); } } Entry::Vacant(entry) => { - let s = structs::JoshutoDirList::new(path.to_path_buf(), &sort_type)?; + let s = structs::JoshutoDirList::new(path.to_path_buf(), &sort_option)?; entry.insert(s); } }; diff --git a/src/preview.rs b/src/preview.rs index bd093bd..2dbf21c 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -12,7 +12,7 @@ pub fn preview_file(curr_tab: &mut JoshutoTab, views: &JoshutoView, config_t: &J if entry.path.is_dir() { match curr_tab .history - .get_mut_or_create(&entry.path, &config_t.sort_type) + .get_mut_or_create(&entry.path, &config_t.sort_option) { Ok(dirlist) => { views @@ -61,7 +61,7 @@ fn process_threads(context: &mut JoshutoContext, view: &JoshutoView) { thread.handle.join().unwrap(); let (tab_src, tab_dest) = (thread.tab_src, thread.tab_dest); if tab_src < context.tabs.len() { - context.tabs[tab_src].reload_contents(&context.config_t.sort_type); + context.tabs[tab_src].reload_contents(&context.config_t.sort_option); if tab_src == context.curr_tab_index { context.tabs[tab_src].refresh( view, @@ -72,7 +72,7 @@ fn process_threads(context: &mut JoshutoContext, view: &JoshutoView) { } } if tab_dest != tab_src && tab_dest < context.tabs.len() { - context.tabs[tab_dest].reload_contents(&context.config_t.sort_type); + context.tabs[tab_dest].reload_contents(&context.config_t.sort_option); if tab_dest == context.curr_tab_index { context.tabs[tab_dest].refresh( view, @@ -99,7 +99,7 @@ fn process_threads(context: &mut JoshutoContext, view: &JoshutoView) { thread.handle.join().unwrap(); if tab_src < context.tabs.len() { let dirty_tab = &mut context.tabs[tab_src]; - dirty_tab.reload_contents(&context.config_t.sort_type); + dirty_tab.reload_contents(&context.config_t.sort_option); if tab_src == context.curr_tab_index { dirty_tab.refresh( view, @@ -112,7 +112,7 @@ fn process_threads(context: &mut JoshutoContext, view: &JoshutoView) { } if tab_dest != tab_src && tab_dest < context.tabs.len() { let dirty_tab = &mut context.tabs[tab_dest]; - dirty_tab.reload_contents(&context.config_t.sort_type); + dirty_tab.reload_contents(&context.config_t.sort_option); if tab_src == context.curr_tab_index { dirty_tab.refresh( view, diff --git a/src/sort.rs b/src/sort.rs index a93d7ac..15f7b55 100644 --- a/src/sort.rs +++ b/src/sort.rs @@ -10,30 +10,23 @@ pub struct SortOption { pub directories_first: bool, pub case_sensitive: bool, pub reverse: bool, + pub sort_method: SortType, } -#[derive(Debug, Clone)] -pub enum SortType { - SortNatural(SortOption), - SortMtime(SortOption), -} - -impl SortType { - pub fn compare_func( - &self, - ) -> fn(&structs::JoshutoDirEntry, &structs::JoshutoDirEntry) -> std::cmp::Ordering { - match *self { - SortType::SortNatural(ref ss) => { - if ss.directories_first && !ss.case_sensitive && !ss.reverse { +impl SortOption { + pub fn compare_func(&self) -> fn(&structs::JoshutoDirEntry, &structs::JoshutoDirEntry) -> std::cmp::Ordering { + match self.sort_method { + SortType::SortNatural => { + if self.directories_first && !self.case_sensitive && !self.reverse { SortNatural::dir_first_case_insensitive - } else if ss.directories_first && ss.case_sensitive && !ss.reverse { + } else if self.directories_first && self.case_sensitive && !self.reverse { SortNatural::dir_first } else { SortNatural::default_sort } } - SortType::SortMtime(ref ss) => { - if ss.directories_first && !ss.reverse { + SortType::SortMtime => { + if self.directories_first && !self.reverse { SortMtime::dir_first } else { SortMtime::default_sort @@ -42,47 +35,41 @@ impl SortType { } } - pub fn filter_func( - &self, - ) -> fn(Result<fs::DirEntry, std::io::Error>) -> Option<structs::JoshutoDirEntry> { - match *self { - SortType::SortNatural(ref ss) => { - if ss.show_hidden { - filter_default - } else { - filter_hidden_files - } - } - SortType::SortMtime(ref ss) => { - if ss.show_hidden { - filter_default - } else { - filter_hidden_files - } - } + pub fn filter_func(&self) -> fn(&Result<fs::DirEntry, std::io::Error>) -> bool { + if self.show_hidden { + no_filter + } else { + filter_hidden } } +} - pub fn show_hidden(&self) -> bool { - match *self { - SortType::SortNatural(ref ss) => ss.show_hidden, - SortType::SortMtime(ref ss) => ss.show_hidden, - } - } +#[derive(Debug, Clone)] +pub enum SortType { + SortNatural, + SortMtime, +} - pub fn set_show_hidden(&mut self, show_hidden: bool) { - match self { - SortType::SortNatural(ref mut ss) => { - ss.show_hidden = show_hidden; - } - SortType::SortMtime(ref mut ss) => { - ss.show_hidden = show_hidden; +#[inline] +fn no_filter(result: &Result<fs::DirEntry, std::io::Error>) -> bool { + true +} + +fn filter_hidden(result: &Result<fs::DirEntry, std::io::Error>) -> bool { + match result { + Err(_) => false, + Ok(entry) => { + let file_name = entry.file_name(); + if let Some(file_name) = file_name.to_str() { + !file_name.starts_with(".") + } else { + false } } } } -fn filter_default( +pub fn map_entry_default( result: Result<fs::DirEntry, std::io::Error>, ) -> Option<structs::JoshutoDirEntry> { match result { diff --git a/src/structs.rs b/src/structs.rs index ddc5d70..0d501cb 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -87,9 +87,9 @@ pub struct JoshutoDirList { } impl JoshutoDirList { - pub fn new(path: PathBuf, sort_type: &sort::SortType) -> Result<Self, std::io::Error> { - let mut contents = Self::read_dir_list(path.as_path(), sort_type)?; - contents.sort_by(&sort_type.compare_func()); + pub fn new(path: PathBuf, sort_option: &sort::SortOption) -> Result<Self, std::io::Error> { + let mut contents = Self::read_dir_list(path.as_path(), sort_option)?; + contents.sort_by(&sort_option.compare_func()); let index = if !contents.is_empty() { Some(0) } else { None }; @@ -109,11 +109,15 @@ impl JoshutoDirList { fn read_dir_list( path: &Path, - sort_type: &sort::SortType, + sort_option: &sort::SortOption, ) -> Result<Vec<JoshutoDirEntry>, std::io::Error> { - let filter_func = sort_type.filter_func(); + + let filter_func = sort_option.filter_func(); let results: fs::ReadDir = fs::read_dir(path)?; - let result_vec: Vec<JoshutoDirEntry> = results.filter_map(filter_func).collect(); + let result_vec: Vec<JoshutoDirEntry> = + results.filter(filter_func) + .filter_map(sort::map_entry_default) + .collect(); Ok(result_vec) } @@ -129,11 +133,11 @@ impl JoshutoDirList { true } - pub fn update_contents(&mut self, sort_type: &sort::SortType) -> Result<(), std::io::Error> { - let sort_func = sort_type.compare_func(); + pub fn update_contents(&mut self, sort_option: &sort::SortOption) -> Result<(), std::io::Error> { + let sort_func = sort_option.compare_func(); self.update_needed = false; - let mut contents = Self::read_dir_list(&self.path, sort_type)?; + let mut contents = Self::read_dir_list(&self.path, sort_option)?; contents.sort_by(&sort_func); let contents_len = contents.len() as i32; @@ -17,34 +17,31 @@ pub struct JoshutoTab { } impl JoshutoTab { - pub fn new(curr_path: PathBuf, sort_type: &sort::SortType) -> Result<Self, std::io::Error> { + pub fn new(curr_path: PathBuf, sort_option: &sort::SortOption) -> Result<Self, std::io::Error> { let mut history = history::DirHistory::new(); - history.populate_to_root(&curr_path, sort_type); + history.populate_to_root(&curr_path, sort_option); - let curr_list: JoshutoDirList = history.pop_or_create(&curr_path, sort_type)?; + let curr_list: Option<JoshutoDirList> = Some(history.pop_or_create(&curr_path, sort_option)?); let parent_list: Option<JoshutoDirList> = match curr_path.parent() { - Some(parent) => { - let tmp_list = history.pop_or_create(&parent, sort_type)?; - Some(tmp_list) - } + Some(parent) => Some(history.pop_or_create(&parent, sort_option)?), None => None, }; let tab = JoshutoTab { curr_path, history, - curr_list: Some(curr_list), + curr_list, parent_list, }; Ok(tab) } - pub fn reload_contents(&mut self, sort_type: &sort::SortType) { + pub fn reload_contents(&mut self, sort_option: &sort::SortOption) { let mut list = self.curr_list.take(); if let Some(ref mut s) = list { if s.path.exists() { - s.update_contents(sort_type).unwrap(); + s.update_contents(sort_option).unwrap(); } }; self.curr_list = list; @@ -52,7 +49,7 @@ impl JoshutoTab { list = self.parent_list.take(); if let Some(ref mut s) = list { if s.path.exists() { - s.update_contents(sort_type).unwrap(); + s.update_contents(sort_option).unwrap(); } }; self.parent_list = list; |