From 5a820a7a275bf5cacd2c545c0a0ac533789ec349 Mon Sep 17 00:00:00 2001 From: Jiayi Zhao Date: Thu, 4 Apr 2019 11:49:23 -0400 Subject: fix cursor resetting to top when action is completed - remove unused code - clean up compiler warnings --- src/commands/cursor_move.rs | 7 +++---- src/config/config.rs | 25 ++++++++++++------------- src/config/keymap.rs | 2 +- src/sort.rs | 27 ++++----------------------- src/structs.rs | 28 +++++++++++++++++++--------- src/tab.rs | 3 ++- 6 files changed, 41 insertions(+), 51 deletions(-) diff --git a/src/commands/cursor_move.rs b/src/commands/cursor_move.rs index 4bc16c7..57e629d 100644 --- a/src/commands/cursor_move.rs +++ b/src/commands/cursor_move.rs @@ -1,12 +1,11 @@ use crate::commands::{JoshutoCommand, JoshutoRunnable}; use crate::context::JoshutoContext; +use crate::preview; use crate::window::JoshutoView; -pub mod CursorMove { - use crate::context::JoshutoContext; - use crate::preview; - use crate::window::JoshutoView; +pub struct CursorMove; +impl CursorMove { pub fn cursor_move(mut new_index: usize, context: &mut JoshutoContext, view: &JoshutoView) { let curr_tab = &mut context.tabs[context.curr_tab_index]; diff --git a/src/config/config.rs b/src/config/config.rs index 2caa1b5..eecefb3 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -46,12 +46,12 @@ impl Flattenable for JoshutoRawConfig { 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, - }, + Some(s) => match s.as_str() { + "mtime" => sort::SortType::SortMtime, _ => sort::SortType::SortNatural, - }; + }, + _ => sort::SortType::SortNatural, + }; let show_hidden: bool; let case_sensitive: bool; @@ -74,18 +74,18 @@ impl Flattenable for JoshutoRawConfig { } let sort_option = sort::SortOption { - show_hidden, - directories_first, - case_sensitive, - reverse, - sort_method, - }; + show_hidden, + directories_first, + case_sensitive, + reverse, + sort_method, + }; JoshutoConfig { scroll_offset, tilde_in_titlebar, column_ratio, - sort_option + sort_option, } } } @@ -108,7 +108,6 @@ impl JoshutoConfig { sort_method: sort::SortType::SortNatural, }; - JoshutoConfig { scroll_offset: 6, tilde_in_titlebar: true, diff --git a/src/config/keymap.rs b/src/config/keymap.rs index 5fd79cd..4b44b2d 100644 --- a/src/config/keymap.rs +++ b/src/config/keymap.rs @@ -70,7 +70,7 @@ fn insert_keycommand( if keys.len() == 1 { if let Some(s) = key_to_i32(&keys[0]) { match map.entry(s) { - hash_map::Entry::Occupied(entry) => { + hash_map::Entry::Occupied(_) => { eprintln!("Error: Keybindings ambiguous"); exit(1); } diff --git a/src/sort.rs b/src/sort.rs index 15f7b55..1845585 100644 --- a/src/sort.rs +++ b/src/sort.rs @@ -14,7 +14,9 @@ pub struct SortOption { } impl SortOption { - pub fn compare_func(&self) -> fn(&structs::JoshutoDirEntry, &structs::JoshutoDirEntry) -> std::cmp::Ordering { + 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 { @@ -51,7 +53,7 @@ pub enum SortType { } #[inline] -fn no_filter(result: &Result) -> bool { +fn no_filter(_: &Result) -> bool { true } @@ -81,27 +83,6 @@ pub fn map_entry_default( } } -fn filter_hidden_files( - result: Result, -) -> Option { - match result { - Ok(direntry) => match direntry.file_name().into_string() { - Ok(file_name) => { - if file_name.starts_with('.') { - None - } else { - match structs::JoshutoDirEntry::from(&direntry) { - Ok(s) => Some(s), - Err(_) => None, - } - } - } - Err(_) => None, - }, - Err(_) => None, - } -} - pub struct SortNatural {} impl SortNatural { pub fn dir_first_case_insensitive( diff --git a/src/structs.rs b/src/structs.rs index 0d501cb..c162c73 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -111,13 +111,12 @@ impl JoshutoDirList { path: &Path, sort_option: &sort::SortOption, ) -> Result, std::io::Error> { - let filter_func = sort_option.filter_func(); let results: fs::ReadDir = fs::read_dir(path)?; - let result_vec: Vec = - results.filter(filter_func) - .filter_map(sort::map_entry_default) - .collect(); + let result_vec: Vec = results + .filter(filter_func) + .filter_map(sort::map_entry_default) + .collect(); Ok(result_vec) } @@ -133,24 +132,35 @@ impl JoshutoDirList { true } - pub fn update_contents(&mut self, sort_option: &sort::SortOption) -> Result<(), std::io::Error> { + 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_option)?; contents.sort_by(&sort_func); - let contents_len = contents.len() as i32; + let contents_len = contents.len(); if contents_len == 0 { self.index = None; } else { - self.index = Some(0); + self.index = match self.index { + Some(index) => { + if index >= contents_len { + Some(contents_len - 1) + } else { + self.index + } + } + None => Some(0), + }; } let metadata = std::fs::metadata(&self.path)?; let metadata = JoshutoMetadata::from(&metadata)?; self.metadata = metadata; - self.contents = contents; Ok(()) } diff --git a/src/tab.rs b/src/tab.rs index e12baad..d5a63cc 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -21,7 +21,8 @@ impl JoshutoTab { let mut history = history::DirHistory::new(); history.populate_to_root(&curr_path, sort_option); - let curr_list: Option = Some(history.pop_or_create(&curr_path, sort_option)?); + let curr_list: Option = + Some(history.pop_or_create(&curr_path, sort_option)?); let parent_list: Option = match curr_path.parent() { Some(parent) => Some(history.pop_or_create(&parent, sort_option)?), -- cgit v1.2.3