diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-08-29 22:06:19 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-08-29 22:08:23 -0400 |
commit | 5be4a5f472655a76e1430bad09a19f6ad111e474 (patch) | |
tree | 1fcffa6c8d37cc6d538b29b6fbd773e8de58512d /src/commands | |
parent | 4f3842b56f1729dcd8e81c77f98253ed9dfb23b3 (diff) |
big rework and dependency update
- abstract JoshutoContext implementation behind functions
- rework io workers in an attempt to fix a bug
- update dependencies
- remove JoshutoContextWorker
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/bulk_rename.rs | 14 | ||||
-rw-r--r-- | src/commands/change_directory.rs | 13 | ||||
-rw-r--r-- | src/commands/command_line.rs | 6 | ||||
-rw-r--r-- | src/commands/cursor_move.rs | 42 | ||||
-rw-r--r-- | src/commands/delete_files.rs | 20 | ||||
-rw-r--r-- | src/commands/file_ops/copy.rs | 28 | ||||
-rw-r--r-- | src/commands/file_ops/cut.rs | 19 | ||||
-rw-r--r-- | src/commands/file_ops/local_state.rs | 52 | ||||
-rw-r--r-- | src/commands/file_ops/mod.rs | 1 | ||||
-rw-r--r-- | src/commands/file_ops/paste.rs | 24 | ||||
-rw-r--r-- | src/commands/new_directory.rs | 8 | ||||
-rw-r--r-- | src/commands/open_file.rs | 44 | ||||
-rw-r--r-- | src/commands/parent_directory.rs | 9 | ||||
-rw-r--r-- | src/commands/quit.rs | 2 | ||||
-rw-r--r-- | src/commands/reload_dir.rs | 54 | ||||
-rw-r--r-- | src/commands/rename_file.rs | 21 | ||||
-rw-r--r-- | src/commands/search.rs | 4 | ||||
-rw-r--r-- | src/commands/selection.rs | 35 | ||||
-rw-r--r-- | src/commands/set_mode.rs | 16 | ||||
-rw-r--r-- | src/commands/shell.rs | 7 | ||||
-rw-r--r-- | src/commands/show_hidden.rs | 2 | ||||
-rw-r--r-- | src/commands/sort.rs | 8 | ||||
-rw-r--r-- | src/commands/tab_operations.rs | 19 | ||||
-rw-r--r-- | src/commands/tab_switch.rs | 25 |
24 files changed, 224 insertions, 249 deletions
diff --git a/src/commands/bulk_rename.rs b/src/commands/bulk_rename.rs index dc60c67..ee3a519 100644 --- a/src/commands/bulk_rename.rs +++ b/src/commands/bulk_rename.rs @@ -46,14 +46,16 @@ impl BulkRename { let mut file_path = path::PathBuf::from("/tmp"); file_path.push(rand_str); - let curr_tab = &context.tabs[context.curr_tab_index]; - let paths = match curr_tab.curr_list_ref() { - Some(s) => s.get_selected_paths(), - None => Vec::new(), + let paths = { + let curr_tab = context.tab_context_ref().curr_tab_ref(); + match curr_tab.curr_list_ref() { + Some(s) => s.get_selected_paths(), + None => vec![], + } }; { let mut file = std::fs::File::create(&file_path)?; - for path in &paths { + for path in paths.iter() { let file_name = path.file_name().unwrap(); let file_name_as_bytes = file_name.to_str().unwrap().as_bytes(); file.write_all(file_name_as_bytes)?; @@ -141,7 +143,7 @@ impl JoshutoRunnable for BulkRename { backend.terminal_drop(); let res = Self::bulk_rename(context); backend.terminal_restore()?; - ReloadDirList::reload(context.curr_tab_index, context)?; + ReloadDirList::reload(context.tab_context_ref().get_index(), context)?; res } } diff --git a/src/commands/change_directory.rs b/src/commands/change_directory.rs index ac97d59..022d306 100644 --- a/src/commands/change_directory.rs +++ b/src/commands/change_directory.rs @@ -22,10 +22,7 @@ impl ChangeDirectory { pub fn cd(path: &path::Path, context: &mut JoshutoContext) -> std::io::Result<()> { std::env::set_current_dir(path)?; - - let curr_tab = &mut context.tabs[context.curr_tab_index]; - curr_tab.curr_path = path.to_path_buf(); - + context.tab_context_mut().curr_tab_mut().set_pwd(path); Ok(()) } @@ -35,10 +32,12 @@ impl ChangeDirectory { ) -> std::io::Result<()> { Self::cd(path, context)?; - let curr_tab = &mut context.tabs[context.curr_tab_index]; - curr_tab + let sort_options = context.config_t.sort_option.clone(); + context + .tab_context_mut() + .curr_tab_mut() .history - .populate_to_root(&path, &context.config_t.sort_option)?; + .populate_to_root(&path, &sort_options)?; Ok(()) } diff --git a/src/commands/command_line.rs b/src/commands/command_line.rs index 577d988..e83ded3 100644 --- a/src/commands/command_line.rs +++ b/src/commands/command_line.rs @@ -23,11 +23,11 @@ impl CommandLine { context: &mut JoshutoContext, backend: &mut TuiBackend, ) -> JoshutoResult<()> { - let mut textfield = TuiTextField::default() + let user_input: Option<String> = TuiTextField::default() .prompt(":") .prefix(self.prefix.as_str()) - .suffix(self.suffix.as_str()); - let user_input: Option<String> = textfield.get_input(backend, &context); + .suffix(self.suffix.as_str()) + .get_input(backend, &context); if let Some(s) = user_input { let trimmed = s.trim_start(); diff --git a/src/commands/cursor_move.rs b/src/commands/cursor_move.rs index a19c264..078d470 100644 --- a/src/commands/cursor_move.rs +++ b/src/commands/cursor_move.rs @@ -6,13 +6,11 @@ use crate::error::JoshutoResult; use crate::history::DirectoryHistory; use crate::ui::TuiBackend; -pub fn cursor_move(new_index: usize, context: &mut JoshutoContext) { - let mut new_index = new_index; - let curr_tab = &mut context.tabs[context.curr_tab_index]; - +pub fn cursor_move(new_index: usize, context: &mut JoshutoContext) -> JoshutoResult<()> { let mut path: Option<PathBuf> = None; + let mut new_index = new_index; - if let Some(curr_list) = curr_tab.curr_list_mut() { + if let Some(curr_list) = context.tab_context_mut().curr_tab_mut().curr_list_mut() { if curr_list.index.is_some() { let dir_len = curr_list.contents.len(); if new_index >= dir_len { @@ -21,18 +19,22 @@ pub fn cursor_move(new_index: usize, context: &mut JoshutoContext) { curr_list.index = Some(new_index); let entry = &curr_list.contents[new_index]; - path = Some(entry.file_path().clone()) + path = Some(entry.file_path().to_path_buf()) } } // get preview if let Some(path) = path { if path.is_dir() { - curr_tab + let sort_options = context.config_t.sort_option.clone(); + context + .tab_context_mut() + .curr_tab_mut() .history - .create_or_soft_update(path.as_path(), &context.config_t.sort_option); + .create_or_soft_update(path.as_path(), &sort_options)?; } } + Ok(()) } #[derive(Clone, Debug)] @@ -59,13 +61,13 @@ impl std::fmt::Display for CursorMoveDown { impl JoshutoRunnable for CursorMoveDown { fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> { - let movement = match context.curr_tab_ref().curr_list_ref() { + let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { Some(curr_list) => curr_list.index.map(|idx| idx + self.movement), None => None, }; if let Some(s) = movement { - cursor_move(s, context) + cursor_move(s, context)?; } Ok(()) } @@ -95,7 +97,7 @@ impl std::fmt::Display for CursorMoveUp { impl JoshutoRunnable for CursorMoveUp { fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> { - let movement = match context.curr_tab_ref().curr_list_ref() { + let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { Some(curr_list) => curr_list.index.map(|idx| { if idx > self.movement { idx - self.movement @@ -107,7 +109,7 @@ impl JoshutoRunnable for CursorMoveUp { }; if let Some(s) = movement { - cursor_move(s, context) + cursor_move(s, context)?; } Ok(()) } @@ -142,7 +144,7 @@ impl JoshutoRunnable for CursorMovePageUp { } }; - let movement = match context.curr_tab_ref().curr_list_ref() { + let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { Some(curr_list) => { curr_list .index @@ -187,7 +189,7 @@ impl JoshutoRunnable for CursorMovePageDown { } }; - let movement = match context.curr_tab_ref().curr_list_ref() { + let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { Some(curr_list) => { let dir_len = curr_list.contents.len(); curr_list.index.map(|idx| { @@ -202,7 +204,7 @@ impl JoshutoRunnable for CursorMovePageDown { }; if let Some(s) = movement { - cursor_move(s, context); + cursor_move(s, context)?; } Ok(()) } @@ -230,7 +232,8 @@ impl std::fmt::Display for CursorMoveHome { impl JoshutoRunnable for CursorMoveHome { fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> { - let movement: Option<usize> = match context.curr_tab_ref().curr_list_ref() { + let movement: Option<usize> = match context.tab_context_ref().curr_tab_ref().curr_list_ref() + { Some(curr_list) => { let len = curr_list.contents.len(); if len == 0 { @@ -243,7 +246,7 @@ impl JoshutoRunnable for CursorMoveHome { }; if let Some(s) = movement { - cursor_move(s, context); + cursor_move(s, context)?; } Ok(()) } @@ -271,7 +274,8 @@ impl std::fmt::Display for CursorMoveEnd { impl JoshutoRunnable for CursorMoveEnd { fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> { - let movement: Option<usize> = match context.curr_tab_ref().curr_list_ref() { + let movement: Option<usize> = match context.tab_context_ref().curr_tab_ref().curr_list_ref() + { Some(curr_list) => { let len = curr_list.contents.len(); if len == 0 { @@ -284,7 +288,7 @@ impl JoshutoRunnable for CursorMoveEnd { }; if let Some(s) = movement { - cursor_move(s, context); + cursor_move(s, context)?; } Ok(()) } diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs index 4f1c19a..69069fe 100644 --- a/src/commands/delete_files.rs +++ b/src/commands/delete_files.rs @@ -22,7 +22,7 @@ impl DeleteFiles { "delete_files" } - pub fn remove_files(paths: &[&path::PathBuf]) -> std::io::Result<()> { + pub fn remove_files(paths: &[&path::Path]) -> std::io::Result<()> { for path in paths { if let Ok(metadata) = fs::symlink_metadata(path) { if metadata.is_dir() { @@ -36,10 +36,10 @@ impl DeleteFiles { } fn delete_files(context: &mut JoshutoContext, backend: &mut TuiBackend) -> std::io::Result<()> { - let curr_tab = &context.tabs[context.curr_tab_index]; - let paths = match curr_tab.curr_list_ref() { + let tab_index = context.tab_context_ref().get_index(); + let paths = match context.tab_context_ref().curr_tab_ref().curr_list_ref() { Some(s) => s.get_selected_paths(), - None => Vec::new(), + None => vec![], }; let paths_len = paths.len(); @@ -65,13 +65,13 @@ impl DeleteFiles { }; if ch == Key::Char('y') { Self::remove_files(&paths)?; - ReloadDirList::reload(context.curr_tab_index, context)?; + ReloadDirList::reload(tab_index, context)?; let msg = format!("Deleted {} files", paths_len); context.message_queue.push_back(msg); } } else { Self::remove_files(&paths)?; - ReloadDirList::reload(context.curr_tab_index, context)?; + ReloadDirList::reload(tab_index, context)?; let msg = format!("Deleted {} files", paths_len); context.message_queue.push_back(msg); } @@ -92,10 +92,10 @@ impl JoshutoRunnable for DeleteFiles { fn execute(&self, context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> { Self::delete_files(context, backend)?; - let options = &context.config_t.sort_option; - let curr_path = context.tabs[context.curr_tab_index].curr_path.clone(); - for tab in context.tabs.iter_mut() { - tab.history.reload(&curr_path, options)?; + let options = context.config_t.sort_option.clone(); + let curr_path = context.tab_context_ref().curr_tab_ref().pwd().to_path_buf(); + for tab in context.tab_context_mut().iter_mut() { + tab.history.reload(&curr_path, &options)?; } LoadChild::load_child(context)?; Ok(()) diff --git a/src/commands/file_ops/copy.rs b/src/commands/file_ops/copy.rs index 7e05752..6632e2a 100644 --- a/src/commands/file_ops/copy.rs +++ b/src/commands/file_ops/copy.rs @@ -1,11 +1,11 @@ +use std::path; + use crate::commands::{JoshutoCommand, JoshutoRunnable}; -use crate::context::JoshutoContext; +use crate::context::{JoshutoContext, LocalStateContext}; use crate::error::JoshutoResult; use crate::io::FileOp; use crate::ui::TuiBackend; -use super::local_state::LocalState; - #[derive(Clone, Debug)] pub struct CopyFiles; @@ -28,10 +28,24 @@ impl std::fmt::Display for CopyFiles { impl JoshutoRunnable for CopyFiles { fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> { - let curr_tab = context.curr_tab_ref(); - if let Some(list) = curr_tab.curr_list_ref() { - LocalState::repopulate(list)?; - LocalState::set_file_op(FileOp::Copy); + if let Some(list) = context.tab_context_ref().curr_tab_ref().curr_list_ref() { + let mut selected: Vec<&path::Path> = list + .iter() + .filter(|e| e.is_selected()) + .map(|e| e.file_path()) + .collect(); + if selected.is_empty() { + selected = match list.get_curr_ref() { + Some(s) => vec![s.file_path()], + None => vec![], + } + } + + let mut local_state = LocalStateContext::new(); + local_state.set_paths(selected.into_iter()); + local_state.set_file_op(FileOp::Copy); + + context.set_local_state(local_state); } Ok(()) } diff --git a/src/commands/file_ops/cut.rs b/src/commands/file_ops/cut.rs index 772e091..0fcbc0e 100644 --- a/src/commands/file_ops/cut.rs +++ b/src/commands/file_ops/cut.rs @@ -1,11 +1,9 @@ use crate::commands::{JoshutoCommand, JoshutoRunnable}; -use crate::context::JoshutoContext; +use crate::context::{JoshutoContext, LocalStateContext}; use crate::error::JoshutoResult; use crate::io::FileOp; use crate::ui::TuiBackend; -use super::local_state::LocalState; - #[derive(Clone, Debug)] pub struct CutFiles; @@ -28,10 +26,17 @@ impl std::fmt::Display for CutFiles { impl JoshutoRunnable for CutFiles { fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> { - let curr_tab = context.curr_tab_ref(); - if let Some(list) = curr_tab.curr_list_ref() { - LocalState::repopulate(list)?; - LocalState::set_file_op(FileOp::Cut); + if let Some(list) = context.tab_context_ref().curr_tab_ref().curr_list_ref() { + let iter = list + .iter() + .filter(|e| e.is_selected()) + .map(|e| e.file_path()); + + let mut local_state = LocalStateContext::new(); + local_state.set_paths(iter); + local_state.set_file_op(FileOp::Cut); + + context.set_local_state(local_state); } Ok(()) } diff --git a/src/commands/file_ops/local_state.rs b/src/commands/file_ops/local_state.rs deleted file mode 100644 index f8a4782..0000000 --- a/src/commands/file_ops/local_state.rs +++ /dev/null @@ -1,52 +0,0 @@ -use lazy_static::lazy_static; - -use crate::fs::JoshutoDirList; -use crate::io::FileOp; -use std::path; -use std::sync::Mutex; - -lazy_static! { - static ref LOCAL_STATE: Mutex<LocalState> = Mutex::new(LocalState::new()); -} - -pub struct LocalState { - pub paths: Vec<path::PathBuf>, - pub file_op: FileOp, -} - -impl LocalState { - pub fn new() -> Self { - Self { - paths: Vec::new(), - file_op: FileOp::Copy, - } - } - - pub fn set_file_op(operation: FileOp) { - let mut data = LOCAL_STATE.lock().unwrap(); - (*data).file_op = operation; - } - - pub fn repopulate(dirlist: &JoshutoDirList) -> std::io::Result<()> { - let selected = dirlist.get_selected_paths(); - if selected.is_empty() { - Err(std::io::Error::new( - std::io::ErrorKind::Other, - "no files selected", - )) - } else { - let selected_clone: Vec<path::PathBuf> = - selected.iter().map(|p| (*p).clone()).collect(); - let mut data = LOCAL_STATE.lock().unwrap(); - (*data).paths = selected_clone; - Ok(()) - } - } - - pub fn take() -> LocalState { - let mut m = LOCAL_STATE.lock().unwrap(); - let mut v = LocalState::new(); - std::mem::swap(&mut (*m), &mut v); - v - } -} diff --git a/src/commands/file_ops/mod.rs b/src/commands/file_ops/mod.rs index 1ed8030..dfaeb6e 100644 --- a/src/commands/file_ops/mod.rs +++ b/src/commands/file_ops/mod.rs @@ -1,6 +1,5 @@ mod copy; mod cut; -mod local_state; mod paste; pub use copy::CopyFiles; diff --git a/src/commands/file_ops/paste.rs b/src/commands/file_ops/paste.rs index 019a558..36c7af5 100644 --- a/src/commands/file_ops/paste.rs +++ b/src/commands/file_ops/paste.rs @@ -4,8 +4,6 @@ use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult}; use crate::io::{IOWorkerOptions, IOWorkerThread}; use crate::ui::TuiBackend; -use super::local_state::LocalState; - pub struct PasteFiles { options: IOWorkerOptions, } @@ -32,20 +30,20 @@ impl std::fmt::Debug for PasteFiles { impl JoshutoRunnable for PasteFiles { fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> { - let state = LocalState::take(); - if state.paths.is_empty() { - Err(JoshutoError::new( + match context.take_local_state() { + Some(state) if !state.paths.is_empty() => { + let dest = context.tab_context_ref().curr_tab_ref().pwd().to_path_buf(); + let mut options = self.options.clone(); + options.kind = state.file_op; + let worker_thread = IOWorkerThread::new(options, state.paths, dest); + context.add_worker(worker_thread); + Ok(()) + } + _ => Err(JoshutoError::new( JoshutoErrorKind::IOInvalidData, "no files selected".to_string(), - ))?; + )), } - let tab_dest = context.curr_tab_index; - let dest = context.tabs[tab_dest].curr_path.clone(); - let mut options = self.options.clone(); - options.kind = state.file_op; - let worker_thread = IOWorkerThread::new(options, state.paths, dest); - context.push_worker_thread(worker_thread); - Ok(()) } } diff --git a/src/commands/new_directory.rs b/src/commands/new_directory.rs index f1ce5e4..401e2a2 100644 --- a/src/commands/new_directory.rs +++ b/src/commands/new_directory.rs @@ -33,10 +33,10 @@ impl JoshutoRunnable for NewDirectory { fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> { std::fs::create_dir_all(&self.path)?; - let options = &context.config_t.sort_option; - let curr_path = context.tabs[context.curr_tab_index].curr_path.clone(); - for tab in context.tabs.iter_mut() { - tab.history.reload(&curr_path, options)?; + let options = context.config_t.sort_option.clone(); + let curr_path = context.tab_context_ref().curr_tab_ref().pwd().to_path_buf(); + for tab in context.tab_context_mut().iter_mut() { + tab.history.reload(&curr_path, &options)?; } LoadChild::load_child(context)?; diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index f7af489..94e14dd 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -44,26 +44,23 @@ impl OpenFile { let mut dirpath = None; let mut selected_entries = None; - { - let curr_tab = context.curr_tab_ref(); - match curr_tab.curr_list_ref() { - None => return Ok(()), - Some(curr_list) => match curr_list.get_curr_ref() { - Some(entry) if entry.file_path().is_dir() => { - let path = entry.file_path().clone(); - dirpath = Some(path); - } - Some(entry) => { - let vec: Vec<&JoshutoDirEntry> = curr_list.selected_entries().collect(); - if vec.is_empty() { - selected_entries = Some(vec![entry]); - } else { - selected_entries = Some(vec); - } + match context.tab_context_ref().curr_tab_ref().curr_list_ref() { + None => return Ok(()), + Some(curr_list) => match curr_list.get_curr_ref() { + Some(entry) if entry.file_path().is_dir() => { + let path = entry.file_path().to_path_buf(); + dirpath = Some(path); + } + Some(entry) => { + let vec: Vec<&JoshutoDirEntry> = curr_list.selected_entries().collect(); + if vec.is_empty() { + selected_entries = Some(vec![entry]); + } else { + selected_entries = Some(vec); } - None => return Ok(()), - }, - } + } + None => return Ok(()), + }, } if let Some(path) = dirpath { @@ -134,11 +131,11 @@ impl OpenFileWith { let menu_options_str: Vec<&str> = menu_options.iter().map(|e| e.as_str()).collect(); let menu_widget = TuiMenu::new(&menu_options_str); - let mut textfield = TuiTextField::default() + TuiTextField::default() .prompt(":") .prefix(PROMPT) - .menu(menu_widget); - textfield.get_input(backend, &context) + .menu(menu_widget) + .get_input(backend, &context) }; let entry_paths: Vec<&str> = entries.iter().map(|e| e.file_name()).collect(); @@ -194,8 +191,7 @@ impl std::fmt::Display for OpenFileWith { impl JoshutoRunnable for OpenFileWith { fn execute(&self, context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> { let selected_entries = { - let curr_tab = context.curr_tab_ref(); - match curr_tab.curr_list_ref() { + match context.tab_context_ref().curr_tab_ref().curr_list_ref() { None => vec![], Some(curr_list) => match curr_list.get_curr_ref() { Some(entry) => { diff --git a/src/commands/parent_directory.rs b/src/commands/parent_directory.rs index d6d05bd..72d629c 100644 --- a/src/commands/parent_directory.rs +++ b/src/commands/parent_directory.rs @@ -15,11 +15,10 @@ impl ParentDirectory { } pub fn parent_directory(context: &mut JoshutoContext) -> std::io::Result<()> { - let curr_tab = &mut context.tabs[context.curr_tab_index]; - if !curr_tab.curr_path.pop() { - return Ok(()); + if context.tab_context_mut().curr_tab_mut().pwd_mut().pop() { + let path = context.tab_context_ref().curr_tab_ref().pwd(); + std::env::set_current_dir(path)?; } - std::env::set_current_dir(&curr_tab.curr_path)?; Ok(()) } } @@ -35,7 +34,7 @@ impl std::fmt::Display for ParentDirectory { impl JoshutoRunnable for ParentDirectory { fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> { Self::parent_directory(context)?; - ReloadDirList::soft_reload(context.curr_tab_index, context)?; + ReloadDirList::soft_reload(context.tab_context_ref().get_index(), context)?; Ok(()) } } diff --git a/src/commands/quit.rs b/src/commands/quit.rs index 2da6f6b..a22a883 100644 --- a/src/commands/quit.rs +++ b/src/commands/quit.rs @@ -15,7 +15,7 @@ impl Quit { } pub fn quit(context: &mut JoshutoContext) -> JoshutoResult<()> { - if context.worker.is_busy() { + if context.worker_is_busy() { Err(JoshutoError::new( JoshutoErrorKind::IOOther, String::from("operations running in background, use force_quit to quit"), diff --git a/src/commands/reload_dir.rs b/src/commands/reload_dir.rs index 0f37eec..e8cb24e 100644 --- a/src/commands/reload_dir.rs +++ b/src/commands/reload_dir.rs @@ -16,42 +16,40 @@ impl ReloadDirList { } pub fn soft_reload(index: usize, context: &mut JoshutoContext) -> std::io::Result<()> { - let curr_tab = &mut context.tabs[index]; - let sort_option = &context.config_t.sort_option; - - if let Some(curr_list) = curr_tab.curr_list_mut() { - if curr_list.need_update() { - curr_list.reload_contents(sort_option)?; + let sort_option = context.config_t.sort_option.clone(); + if let Some(curr_tab) = context.tab_context_mut().tab_mut(index) { + if let Some(curr_list) = curr_tab.curr_list_mut() { + if curr_list.need_update() { + curr_list.reload_contents(&sort_option)?; + } } - } - if let Some(curr_list) = curr_tab.parent_list_mut() { - if curr_list.need_update() { - curr_list.reload_contents(sort_option)?; + if let Some(curr_list) = curr_tab.parent_list_mut() { + if curr_list.need_update() { + curr_list.reload_contents(&sort_option)?; + } } - } - if let Some(curr_list) = curr_tab.child_list_mut() { - if curr_list.need_update() { - curr_list.reload_contents(sort_option)?; + if let Some(curr_list) = curr_tab.child_list_mut() { + if curr_list.need_update() { + curr_list.reload_contents(&sort_option)?; + } } } - Ok(()) } pub fn reload(index: usize, context: &mut JoshutoContext) -> std::io::Result<()> { - let curr_tab = &mut context.tabs[index]; - let sort_option = &context.config_t.sort_option; - - if let Some(curr_list) = curr_tab.curr_list_mut() { - curr_list.reload_contents(sort_option)?; - } - if let Some(curr_list) = curr_tab.parent_list_mut() { - curr_list.reload_contents(sort_option)?; - } - if let Some(curr_list) = curr_tab.child_list_mut() { - curr_list.reload_contents(sort_option)?; + let sort_option = context.config_t.sort_option.clone(); + if let Some(curr_tab) = context.tab_context_mut().tab_mut(index) { + if let Some(curr_list) = curr_tab.curr_list_mut() { + curr_list.reload_contents(&sort_option)?; + } + if let Some(curr_list) = curr_tab.parent_list_mut() { + curr_list.reload_contents(&sort_option)?; + } + if let Some(curr_list) = curr_tab.child_list_mut() { + curr_list.reload_contents(&sort_option)?; + } } - Ok(()) } } @@ -66,7 +64,7 @@ impl std::fmt::Display for ReloadDirList { impl JoshutoRunnable for ReloadDirList { fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> { - Self::reload(context.curr_tab_index, context)?; + Self::reload(context.tab_context_ref().get_index(), context)?; LoadChild::load_child(context)?; Ok(()) } diff --git a/src/commands/rename_file.rs b/src/commands/rename_file.rs index f8bb5c6..8581c14 100644 --- a/src/commands/rename_file.rs +++ b/src/commands/rename_file.rs @@ -31,9 +31,9 @@ impl RenameFile { return Err(err); } std::fs::rename(&path, &new_path)?; - let curr_tab = &mut context.tabs[context.curr_tab_index]; - if let Some(curr_list) = curr_tab.curr_list_mut() { - curr_list.reload_contents(&context.config_t.sort_option)?; + let options = context.config_t.sort_option.clone(); + if let Some(curr_list) = context.tab_context_mut().curr_tab_mut().curr_list_mut() { + curr_list.reload_contents(&options)?; } Ok(()) } @@ -51,10 +51,13 @@ impl JoshutoRunnable for RenameFile { fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> { let mut path: Option<path::PathBuf> = None; - if let Some(curr_list) = context.curr_tab_ref().curr_list_ref() { - if let Some(s) = curr_list.get_curr_ref() { - path = Some(s.file_path().clone()); - } + if let Some(s) = context + .tab_context_ref() + .curr_tab_ref() + .curr_list_ref() + .and_then(|s| s.get_curr_ref()) + { + path = Some(s.file_path().to_path_buf()); } if let Some(path) = path { @@ -109,7 +112,7 @@ impl JoshutoRunnable for RenameFileAppend { fn execute(&self, context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> { let mut file_name: Option<String> = None; - if let Some(curr_list) = context.curr_tab_ref().curr_list_ref() { + if let Some(curr_list) = context.tab_context_ref().curr_tab_ref().curr_list_ref() { file_name = curr_list.get_curr_ref().map(|s| s.file_name().to_string()); } @@ -158,7 +161,7 @@ impl JoshutoRunnable for RenameFilePrepend { fn execute(&self, context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> { let mut file_name: Option<String> = None; - if let Some(curr_list) = context.curr_tab_ref().curr_list_ref() { + |