diff options
-rw-r--r-- | src/commands/delete_files.rs | 9 | ||||
-rw-r--r-- | src/commands/file_operations.rs | 7 | ||||
-rw-r--r-- | src/commands/new_directory.rs | 7 | ||||
-rw-r--r-- | src/commands/open_file.rs | 10 | ||||
-rw-r--r-- | src/commands/reload_dir.rs | 10 | ||||
-rw-r--r-- | src/commands/show_hidden.rs | 26 | ||||
-rw-r--r-- | src/commands/tab_operations.rs | 10 | ||||
-rw-r--r-- | src/commands/tab_switch.rs | 46 | ||||
-rw-r--r-- | src/history.rs | 17 | ||||
-rw-r--r-- | src/run.rs | 20 | ||||
-rw-r--r-- | src/sort.rs | 14 | ||||
-rw-r--r-- | src/structs.rs | 16 | ||||
-rw-r--r-- | src/tab.rs | 10 | ||||
-rw-r--r-- | src/textfield.rs | 75 |
14 files changed, 132 insertions, 145 deletions
diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs index 071f629..035099d 100644 --- a/src/commands/delete_files.rs +++ b/src/commands/delete_files.rs @@ -58,15 +58,16 @@ impl JoshutoRunnable for DeleteFiles { if let Some(paths) = s.get_selected_paths() { match Self::remove_files(paths) { Ok(_) => ui::wprint_msg(&view.bot_win, "Deleted files"), - Err(e) => { - return Err(JoshutoError::IO(e)); - } + Err(e) => return Err(JoshutoError::IO(e)), } } } let curr_tab = &mut context.tabs[context.curr_tab_index]; - curr_tab.reload_contents(&context.config_t.sort_option); + match curr_tab.reload_contents(&context.config_t.sort_option) { + Err(e) => return Err(JoshutoError::IO(e)), + _ => {} + } curr_tab.refresh( &view, &context.config_t, diff --git a/src/commands/file_operations.rs b/src/commands/file_operations.rs index f93d8b4..db0667a 100644 --- a/src/commands/file_operations.rs +++ b/src/commands/file_operations.rs @@ -319,9 +319,8 @@ fn fs_cut_thread( } } destination.pop(); - progress_info.bytes_finished += 1; - tx.send(progress_info.clone()); + tx.send(progress_info.clone()).unwrap(); } return Ok(()); } @@ -389,9 +388,7 @@ fn fs_copy_thread( } destination.pop(); progress_info.bytes_finished += 1; - match tx.send(progress_info.clone()) { - _ => {} - } + tx.send(progress_info.clone()).unwrap(); } return Ok(()); } diff --git a/src/commands/new_directory.rs b/src/commands/new_directory.rs index 64d7e5c..f8df3b1 100644 --- a/src/commands/new_directory.rs +++ b/src/commands/new_directory.rs @@ -50,9 +50,12 @@ impl JoshutoRunnable for NewDirectory { let path = path::PathBuf::from(user_input); match std::fs::create_dir_all(&path) { - Ok(_) => ReloadDirList::reload(context, view), + Ok(_) => match ReloadDirList::reload(context, view) { + Ok(_) => {} + Err(e) => return Err(JoshutoError::IO(e)), + }, Err(e) => return Err(JoshutoError::IO(e)), - } + }; } ncurses::doupdate(); Ok(()) diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index 2012387..87b2442 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -129,7 +129,10 @@ impl JoshutoRunnable for OpenFile { match curr_tab.curr_list { Some(ref mut s) => { if s.need_update() { - s.update_contents(&context.config_t.sort_option); + match s.update_contents(&context.config_t.sort_option) { + Err(e) => return Err(JoshutoError::IO(e)), + _ => {} + } } } None => {} @@ -157,7 +160,10 @@ impl JoshutoRunnable for OpenFile { match curr_tab.curr_list { Some(ref mut s) => { if s.need_update() { - s.update_contents(&context.config_t.sort_option); + match s.update_contents(&context.config_t.sort_option) { + Err(e) => return Err(JoshutoError::IO(e)), + _ => {} + } } } None => {} diff --git a/src/commands/reload_dir.rs b/src/commands/reload_dir.rs index 454abf4..80e966b 100644 --- a/src/commands/reload_dir.rs +++ b/src/commands/reload_dir.rs @@ -15,15 +15,16 @@ impl ReloadDirList { "reload_dir_list" } - pub fn reload(context: &mut JoshutoContext, view: &JoshutoView) { + pub fn reload(context: &mut JoshutoContext, view: &JoshutoView) -> Result<(), std::io::Error> { let curr_tab = &mut context.tabs[context.curr_tab_index]; - curr_tab.reload_contents(&context.config_t.sort_option); + curr_tab.reload_contents(&context.config_t.sort_option)?; curr_tab.refresh( view, &context.config_t, &context.username, &context.hostname, ); + Ok(()) } } @@ -41,7 +42,10 @@ impl JoshutoRunnable for ReloadDirList { context: &mut JoshutoContext, view: &JoshutoView, ) -> Result<(), JoshutoError> { - Self::reload(context, view); + match Self::reload(context, view) { + Ok(_) => {} + Err(e) => return Err(JoshutoError::IO(e)), + } preview::preview_file( &mut context.tabs[context.curr_tab_index], view, diff --git a/src/commands/show_hidden.rs b/src/commands/show_hidden.rs index 10e8ada..cc089ce 100644 --- a/src/commands/show_hidden.rs +++ b/src/commands/show_hidden.rs @@ -1,7 +1,6 @@ -use crate::commands::{JoshutoCommand, JoshutoRunnable}; +use crate::commands::{JoshutoCommand, JoshutoRunnable, ReloadDirList}; use crate::context::JoshutoContext; use crate::error::JoshutoError; -use crate::preview; use crate::window::JoshutoView; #[derive(Clone, Debug)] @@ -20,7 +19,12 @@ impl ToggleHiddenFiles { for tab in &mut context.tabs { tab.history.depecrate_all_entries(); - tab.reload_contents(&context.config_t.sort_option); + if let Some(s) = tab.curr_list.as_mut() { + s.depreciate(); + } + if let Some(s) = tab.parent_list.as_mut() { + s.depreciate(); + } } } } @@ -40,20 +44,6 @@ impl JoshutoRunnable for ToggleHiddenFiles { view: &JoshutoView, ) -> Result<(), JoshutoError> { Self::toggle_hidden(context); - let curr_tab = &mut context.tabs[context.curr_tab_index]; - curr_tab.reload_contents(&context.config_t.sort_option); - curr_tab.refresh( - view, - &context.config_t, - &context.username, - &context.hostname, - ); - preview::preview_file( - &mut context.tabs[context.curr_tab_index], - view, - &context.config_t, - ); - ncurses::doupdate(); - Ok(()) + ReloadDirList::new().execute(context, view) } } diff --git a/src/commands/tab_operations.rs b/src/commands/tab_operations.rs index b7359a3..0753191 100644 --- a/src/commands/tab_operations.rs +++ b/src/commands/tab_operations.rs @@ -29,7 +29,10 @@ impl NewTab { context.tabs.push(tab); context.curr_tab_index = context.tabs.len() - 1; - TabSwitch::tab_switch(context.curr_tab_index, context, view) + match TabSwitch::tab_switch(context.curr_tab_index, context, view) { + Ok(_) => Ok(()), + Err(e) => Err(JoshutoError::IO(e)), + } } Err(e) => Err(JoshutoError::IO(e)), } @@ -74,7 +77,10 @@ impl CloseTab { if context.curr_tab_index > 0 { context.curr_tab_index -= 1; } - TabSwitch::tab_switch(context.curr_tab_index, context, view) + match TabSwitch::tab_switch(context.curr_tab_index, context, view) { + Ok(_) => Ok(()), + Err(e) => Err(JoshutoError::IO(e)), + } } } diff --git a/src/commands/tab_switch.rs b/src/commands/tab_switch.rs index cc6a6fb..d5c349b 100644 --- a/src/commands/tab_switch.rs +++ b/src/commands/tab_switch.rs @@ -24,29 +24,34 @@ impl TabSwitch { new_index: usize, context: &mut JoshutoContext, view: &JoshutoView, - ) -> Result<(), JoshutoError> { + ) -> Result<(), std::io::Error> { context.curr_tab_index = new_index; let path = &context.curr_tab_ref().curr_path; - match env::set_current_dir(path) { - Ok(_) => { - { - let curr_tab = &mut context.tabs[context.curr_tab_index]; - curr_tab.reload_contents(&context.config_t.sort_option); - curr_tab.refresh( - view, - &context.config_t, - &context.username, - &context.hostname, - ); + env::set_current_dir(path)?; + { + let curr_tab = &mut context.tabs[context.curr_tab_index]; + if let Some(s) = curr_tab.curr_list.as_mut() { + if s.need_update() { + s.update_contents(&context.config_t.sort_option)?; } - ui::redraw_tab_view(&view.tab_win, &context); - let curr_tab = &mut context.tabs[context.curr_tab_index]; - preview::preview_file(curr_tab, view, &context.config_t); - ncurses::doupdate(); - Ok(()) } - Err(e) => Err(JoshutoError::IO(e)), + if let Some(s) = curr_tab.parent_list.as_mut() { + if s.need_update() { + s.update_contents(&context.config_t.sort_option)?; + } + } + curr_tab.refresh( + view, + &context.config_t, + &context.username, + &context.hostname, + ); } + ui::redraw_tab_view(&view.tab_win, &context); + let curr_tab = &mut context.tabs[context.curr_tab_index]; + preview::preview_file(curr_tab, view, &context.config_t); + ncurses::doupdate(); + Ok(()) } } @@ -73,6 +78,9 @@ impl JoshutoRunnable for TabSwitch { new_index -= tab_len; } let new_index = new_index as usize; - Self::tab_switch(new_index, context, view) + match Self::tab_switch(new_index, context, view) { + Ok(_) => Ok(()), + Err(e) => Err(JoshutoError::IO(e)), + } } } diff --git a/src/history.rs b/src/history.rs index 0981b9d..92ddd60 100644 --- a/src/history.rs +++ b/src/history.rs @@ -64,25 +64,18 @@ impl DirHistory { path: &Path, sort_option: &sort::SortOption, ) -> Result<&mut structs::JoshutoDirList, std::io::Error> { - let pathbuf = path.to_path_buf(); - match self.map.entry(pathbuf.clone()) { + match self.map.entry(path.to_path_buf().clone()) { Entry::Occupied(mut entry) => { let dir_entry = entry.get_mut(); if dir_entry.need_update() { - dir_entry.update_contents(&sort_option); + dir_entry.update_contents(&sort_option)?; } + Ok(entry.into_mut()) } Entry::Vacant(entry) => { let s = structs::JoshutoDirList::new(path.to_path_buf(), &sort_option)?; - entry.insert(s); + Ok(entry.insert(s)) } - }; - match self.map.get_mut(&pathbuf) { - Some(s) => Ok(s), - None => Err(std::io::Error::new( - std::io::ErrorKind::NotFound, - "Can't find file", - )), } } @@ -95,6 +88,6 @@ impl DirHistory { pub fn depecrate_all_entries(&mut self) { self.map .iter_mut() - .for_each(|(_, v)| v.update_needed = true); + .for_each(|(_, v)| v.depreciate()); } } @@ -48,7 +48,7 @@ fn recurse_get_keycommand(keymap: &HashMap<i32, CommandKeybind>) -> Option<&Box< } } -fn join_thread(context: &mut JoshutoContext, thread: FileOperationThread, view: &JoshutoView) { +fn join_thread(context: &mut JoshutoContext, thread: FileOperationThread, view: &JoshutoView) -> Result<(), std::io::Error> { ncurses::werase(view.bot_win.win); ncurses::doupdate(); @@ -61,7 +61,7 @@ fn join_thread(context: &mut JoshutoContext, thread: FileOperationThread, view: Ok(_) => { if tab_src < context.tabs.len() { let dirty_tab = &mut context.tabs[tab_src]; - dirty_tab.reload_contents(&context.config_t.sort_option); + dirty_tab.reload_contents(&context.config_t.sort_option)?; if tab_src == context.curr_tab_index { dirty_tab.refresh( view, @@ -74,7 +74,7 @@ fn join_thread(context: &mut JoshutoContext, thread: FileOperationThread, view: } 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_option); + dirty_tab.reload_contents(&context.config_t.sort_option)?; if tab_src == context.curr_tab_index { dirty_tab.refresh( view, @@ -87,15 +87,16 @@ fn join_thread(context: &mut JoshutoContext, thread: FileOperationThread, view: } } } + Ok(()) } -fn process_threads(context: &mut JoshutoContext, view: &JoshutoView) { +fn process_threads(context: &mut JoshutoContext, view: &JoshutoView) -> Result<(), std::io::Error> { let thread_wait_duration: time::Duration = time::Duration::from_millis(100); for i in 0..context.threads.len() { match &context.threads[i].recv_timeout(&thread_wait_duration) { Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => { let thread = context.threads.swap_remove(i); - join_thread(context, thread, view); + join_thread(context, thread, view)?; ncurses::doupdate(); } Ok(progress_info) => { @@ -107,6 +108,7 @@ fn process_threads(context: &mut JoshutoContext, view: &JoshutoView) { _ => {} } } + Ok(()) } #[inline] @@ -147,7 +149,13 @@ pub fn run(config_t: config::JoshutoConfig, keymap_t: config::JoshutoKeymap) { while !context.exit { if !context.threads.is_empty() { ncurses::timeout(0); - process_threads(&mut context, &view); + match process_threads(&mut context, &view) { + Ok(()) => {} + Err(e) => { + ui::wprint_err(&view.bot_win, e.to_string().as_str()); + ncurses::doupdate(); + } + } } else { ncurses::timeout(-1); } diff --git a/src/sort.rs b/src/sort.rs index ead49d0..c78541f 100644 --- a/src/sort.rs +++ b/src/sort.rs @@ -121,19 +121,13 @@ fn natural_sort_case_insensitive( ) -> cmp::Ordering { let f1_name = f1.file_name_as_string.to_lowercase(); let f2_name = f2.file_name_as_string.to_lowercase(); - if f1_name <= f2_name { - cmp::Ordering::Less - } else { - cmp::Ordering::Greater - } + f1_name.partial_cmp(&f2_name).unwrap_or(cmp::Ordering::Less) } fn natural_sort(f1: &structs::JoshutoDirEntry, f2: &structs::JoshutoDirEntry) -> cmp::Ordering { - if f1.file_name <= f2.file_name { - cmp::Ordering::Less - } else { - cmp::Ordering::Greater - } + f1.file_name + .partial_cmp(&f2.file_name) + .unwrap_or(cmp::Ordering::Less) } fn mtime_sort(file1: &structs::JoshutoDirEntry, file2: &structs::JoshutoDirEntry) -> cmp::Ordering { diff --git a/src/structs.rs b/src/structs.rs index 0f5d0a4..f822474 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -80,10 +80,10 @@ impl std::fmt::Debug for JoshutoDirEntry { pub struct JoshutoDirList { pub index: Option<usize>, pub path: PathBuf, - pub update_needed: bool, pub metadata: JoshutoMetadata, pub contents: Vec<JoshutoDirEntry>, pub pagestate: JoshutoPageState, + outdated: bool, } impl JoshutoDirList { @@ -100,7 +100,7 @@ impl JoshutoDirList { Ok(JoshutoDirList { index, path, - update_needed: false, + outdated: false, metadata, contents, pagestate, @@ -120,8 +120,12 @@ impl JoshutoDirList { Ok(result_vec) } + pub fn depreciate(&mut self) { + self.outdated = true; + } + pub fn need_update(&self) -> bool { - if self.update_needed { + if self.outdated { return true; } if let Ok(metadata) = std::fs::metadata(&self.path) { @@ -129,16 +133,16 @@ impl JoshutoDirList { return self.metadata.modified < modified; } } - true + false } 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; + self.outdated = false; + let sort_func = sort_option.compare_func(); let mut contents = Self::read_dir_list(&self.path, sort_option)?; contents.sort_by(&sort_func); @@ -38,11 +38,14 @@ impl JoshutoTab { Ok(tab) } - pub fn reload_contents(&mut self, sort_option: &sort::SortOption) { + pub fn reload_contents( + &mut self, + sort_option: &sort::SortOption, + ) -> Result<(), std::io::Error> { let mut list = self.curr_list.take(); if let Some(ref mut s) = list { if s.path.exists() { - s.update_contents(sort_option); + s.update_contents(sort_option)?; } }; self.curr_list = list; @@ -50,10 +53,11 @@ impl JoshutoTab { let mut list = self.parent_list.take(); if let Some(ref mut s) = list { if s.path.exists() { - s.update_contents(sort_option); + s.update_contents(sort_option)?; } }; self.parent_list = list; + Ok(()) } pub fn refresh( diff --git a/src/textfield.rs b/src/textfield.rs index e15da1c..3892c1e 100644 --- a/src/textfield.rs +++ b/src/textfield.rs @@ -8,7 +8,6 @@ struct CompletionTracker { pub pos: usize, pub original: String, pub candidates: Vec<rustyline::completion::Pair>, - pub need_update: bool, } impl CompletionTracker { @@ -18,7 +17,6 @@ impl CompletionTracker { pos, original, candidates, - need_update: false, } } } @@ -63,6 +61,7 @@ impl JoshutoTextField { ncurses::mvwaddstr(win, coord.0, coord.1 as i32, line_buffer.as_str()); ncurses::wclrtoeol(win); + /* draws cursor */ ncurses::mvwchgat( win, coord.0, @@ -86,51 +85,44 @@ impl JoshutoTextField { } else if ch == ncurses::KEY_HOME { line_buffer.move_home(); curr_pos = 0; - if let Some(ref mut s) = completion_tracker { - s.need_update = true; - } + completion_tracker.take(); } else if ch == ncurses::KEY_END { line_buffer.move_end(); curr_pos = unicode_width::UnicodeWidthStr::width(line_buffer.as_str()); - if let Some(ref mut s) = completion_tracker { - s.need_update = true; - } + completion_tracker.take(); } else if ch == ncurses::KEY_LEFT { if line_buffer.move_backward(1) { let pos = line_buffer.pos(); curr_pos = unicode_width::UnicodeWidthStr::width(&line_buffer.as_str()[..pos]); - if let Some(ref mut s) = completion_tracker { - s.need_update = true; - } + completion_tracker.take(); } } else if ch == ncurses::KEY_RIGHT { if line_buffer.move_forward(1) { let pos = line_buffer.pos(); curr_pos = unicode_width::UnicodeWidthStr::width(&line_buffer.as_str()[..pos]); - if let Some(ref mut s) = completion_tracker { - s.need_update = true; - } + completion_tracker.take(); } } else if ch == keymap::BACKSPACE { if line_buffer.backspace(1) { let pos = line_buffer.pos(); curr_pos = unicode_width::UnicodeWidthStr::width(&line_buffer.as_str()[..pos]); - if let Some(ref mut s) = completion_tracker { - s.need_update = true; - } + completion_tracker.take(); } } else if ch == ncurses::KEY_DC { if let Some(_) = line_buffer.delete(1) { - if let Some(ref mut s) = completion_tracker { - s.need_update = true; - } + completion_tracker.take(); } } else if ch == 0x9 { match completion_tracker { None => { if line_buffer.len() == line_buffer.pos() { let res = completer.complete(line_buffer.as_str(), line_buffer.len()); - if let Ok((pos, candidates)) = res { + if let Ok((pos, mut candidates)) = res { + candidates.sort_by(|x, y| { + x.display() + .partial_cmp(y.display()) + .unwrap_or(std::cmp::Ordering::Less) + }); let ct = CompletionTracker::new( pos, candidates, @@ -140,34 +132,16 @@ impl JoshutoTextField { } } } - Some(ref mut s) => { - if s.need_update { - completion_tracker = None; - if line_buffer.len() == line_buffer.pos() { - let res = - completer.complete(line_buffer.as_str(), line_buffer.len()); - if let Ok((pos, candidates)) = res { - let ct = CompletionTracker::new( - pos, - candidates, - String::from(line_buffer.as_str()), - ); - completion_tracker = Some(ct); - } - } - } - } + _ => {} } match completion_tracker { Some(ref mut s) => { if s.index < s.candidates.len() { let candidate = &s.candidates[s.index]; - line_buffer.kill_line(); - line_buffer.yank_pop(line_buffer.pos(), candidate.display()); + completer.update(&mut line_buffer, 0, candidate.display()); s.index += 1; } else { - line_buffer.kill_line(); - line_buffer.yank_pop(line_buffer.pos(), &s.original); + completer.update(&mut line_buffer, 0, &s.original); s.index = 0; } } @@ -177,19 +151,14 @@ impl JoshutoTextField { &line_buffer.as_str()[..line_buffer.pos()], ); } else if ch == ncurses::KEY_UP { - + completion_tracker.take(); } else if ch == ncurses::KEY_DOWN { - + completion_tracker.take(); } else if let Some(ch) = std::char::from_u32(ch as u32) { - match line_buffer.insert(ch, 1) { - Some(_) => { - curr_pos += unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1); - if let Some(ref mut s) = completion_tracker { - s.need_update = true; - } - } - None => {} - }; + if let Some(_) = line_buffer.insert(ch, 1) { + curr_pos += unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1); + completion_tracker.take(); + } } } let lbstr = line_buffer.to_string(); |