diff options
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/change_directory.rs | 26 | ||||
-rw-r--r-- | src/commands/delete_files.rs | 14 | ||||
-rw-r--r-- | src/commands/new_directory.rs | 12 | ||||
-rw-r--r-- | src/commands/reload.rs | 122 | ||||
-rw-r--r-- | src/commands/rename_file.rs | 15 | ||||
-rw-r--r-- | src/commands/tab_ops.rs | 119 | ||||
-rw-r--r-- | src/commands/touch_file.rs | 16 |
7 files changed, 170 insertions, 154 deletions
diff --git a/src/commands/change_directory.rs b/src/commands/change_directory.rs index 220ca59..6ac2575 100644 --- a/src/commands/change_directory.rs +++ b/src/commands/change_directory.rs @@ -3,7 +3,7 @@ use std::path; use crate::commands::reload; use crate::context::AppContext; use crate::error::AppResult; -use crate::history::DirectoryHistory; +use crate::history::{generate_entries_to_root, DirectoryHistory}; use crate::util::cwd; // ChangeDirectory command @@ -28,25 +28,19 @@ pub fn change_directory(context: &mut AppContext, mut path: &path::Path) -> AppR }; cd(new_cwd.as_path(), context)?; - let config = context.config_ref().clone(); - let options = context.config_ref().display_options_ref().clone(); - let ui_context = context.ui_context_ref().clone(); - let tab_options = context - .tab_context_ref() - .curr_tab_ref() - .option_ref() - .clone(); + let dirlists = generate_entries_to_root( + new_cwd.as_path(), + context.config_ref(), + context.tab_context_ref().curr_tab_ref().history_ref(), + context.ui_context_ref(), + context.config_ref().display_options_ref(), + context.tab_context_ref().curr_tab_ref().option_ref(), + )?; context .tab_context_mut() .curr_tab_mut() .history_mut() - .populate_to_root( - new_cwd.as_path(), - &config, - &ui_context, - &options, - &tab_options, - )?; + .insert_entries(dirlists); Ok(()) } diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs index 755edb8..7076e80 100644 --- a/src/commands/delete_files.rs +++ b/src/commands/delete_files.rs @@ -5,11 +5,12 @@ use termion::event::Key; use crate::context::AppContext; use crate::error::{AppError, AppErrorKind, AppResult}; -use crate::history::DirectoryHistory; use crate::io::{FileOperation, FileOperationOptions, IoWorkerThread}; use crate::ui::widgets::TuiPrompt; use crate::ui::AppBackend; +use super::tab_ops; + fn prompt(context: &mut AppContext, backend: &mut AppBackend, paths_len: usize) -> bool { let ch = { let prompt_str = format!("Delete {} files? (Y/n)", paths_len); @@ -92,14 +93,7 @@ pub fn delete_selected_files( delete_files(context, paths, background, permanently)?; } - let curr_tab = context.tab_context_ref().curr_tab_ref(); - let config = context.config_ref().clone(); - let options = context.config_ref().display_options_ref().clone(); - let curr_path = curr_tab.cwd().to_path_buf(); - for (_, tab) in context.tab_context_mut().iter_mut() { - let tab_options = tab.option_ref().clone(); - tab.history_mut() - .reload(&curr_path, &config, &options, &tab_options)?; - } + let curr_path = context.tab_context_ref().curr_tab_ref().cwd().to_path_buf(); + tab_ops::reload_all_tabs(context, curr_path.as_path())?; Ok(()) } diff --git a/src/commands/new_directory.rs b/src/commands/new_directory.rs index d4bd68d..1d6e2e8 100644 --- a/src/commands/new_directory.rs +++ b/src/commands/new_directory.rs @@ -3,18 +3,14 @@ use std::path; use crate::commands::cursor_move; use crate::context::AppContext; use crate::error::AppResult; -use crate::history::DirectoryHistory; + +use super::tab_ops; pub fn new_directory(context: &mut AppContext, p: &path::Path) -> AppResult { std::fs::create_dir_all(p)?; - let config = context.config_ref().clone(); - let options = context.config_ref().display_options_ref().clone(); + let curr_path = context.tab_context_ref().curr_tab_ref().cwd().to_path_buf(); - for (_, tab) in context.tab_context_mut().iter_mut() { - let tab_options = tab.option_ref().clone(); - tab.history_mut() - .reload(&curr_path, &config, &options, &tab_options)?; - } + tab_ops::reload_all_tabs(context, curr_path.as_path())?; if context.config_ref().focus_on_create { cursor_move::to_path(context, p)?; diff --git a/src/commands/reload.rs b/src/commands/reload.rs index bee7754..4134272 100644 --- a/src/commands/reload.rs +++ b/src/commands/reload.rs @@ -1,55 +1,44 @@ use crate::context::AppContext; use crate::error::AppResult; -use crate::history::create_dirlist_with_history; +use crate::history::{create_dirlist_with_history, DirectoryHistory}; use uuid::Uuid; // reload only if we have a queued reload pub fn soft_reload(context: &mut AppContext, id: &Uuid) -> std::io::Result<()> { - let mut paths = Vec::with_capacity(3); + let mut dirlists = Vec::with_capacity(3); if let Some(curr_tab) = context.tab_context_ref().tab_ref(id) { - if let Some(curr_list) = curr_tab.curr_list_ref() { - if curr_list.need_update() { - paths.push(curr_list.file_path().to_path_buf()); - } - } - if let Some(curr_list) = curr_tab.parent_list_ref() { - if curr_list.need_update() { - paths.push(curr_list.file_path().to_path_buf()); - } - } - if let Some(curr_list) = curr_tab.child_list_ref() { - if curr_list.need_update() { - paths.push(curr_list.file_path().to_path_buf()); - } - } - } - - if !paths.is_empty() { - let config = context.config_ref().clone(); - let options = context.config_ref().display_options_ref().clone(); - let tab_options = context - .tab_context_ref() - .curr_tab_ref() - .option_ref() - .clone(); - if let Some(history) = context - .tab_context_mut() - .tab_mut(id) - .map(|t| t.history_mut()) + let config = context.config_ref(); + let display_options = context.config_ref().display_options_ref(); + let tab_options = context.tab_context_ref().curr_tab_ref().option_ref(); + let history = curr_tab.history_ref(); + for curr_list in [ + curr_tab.parent_list_ref(), + curr_tab.curr_list_ref(), + curr_tab.child_list_ref(), + ] + .into_iter() + .flatten() { - for path in paths { + if curr_list.need_update() { let new_dirlist = create_dirlist_with_history( history, - path.as_path(), - &config, - &options, - &tab_options, + curr_list.file_path(), + display_options, + tab_options, )?; - history.insert(path, new_dirlist); + dirlists.push(new_dirlist); } } } + + if let Some(history) = context + .tab_context_mut() + .tab_mut(id) + .map(|t| t.history_mut()) + { + history.insert_entries(dirlists); + } Ok(()) } @@ -59,43 +48,36 @@ pub fn soft_reload_curr_tab(context: &mut AppContext) -> std::io::Result<()> { } pub fn reload(context: &mut AppContext, id: &Uuid) -> std::io::Result<()> { - let mut paths = Vec::with_capacity(3); + let mut dirlists = Vec::with_capacity(3); if let Some(curr_tab) = context.tab_context_ref().tab_ref(id) { - if let Some(curr_list) = curr_tab.curr_list_ref() { - paths.push(curr_list.file_path().to_path_buf()); - } - if let Some(curr_list) = curr_tab.parent_list_ref() { - paths.push(curr_list.file_path().to_path_buf()); - } - if let Some(curr_list) = curr_tab.child_list_ref() { - paths.push(curr_list.file_path().to_path_buf()); + let config = context.config_ref(); + let display_options = context.config_ref().display_options_ref(); + let tab_options = context.tab_context_ref().curr_tab_ref().option_ref(); + let history = curr_tab.history_ref(); + for curr_list in [ + curr_tab.parent_list_ref(), + curr_tab.curr_list_ref(), + curr_tab.child_list_ref(), + ] + .into_iter() + .flatten() + { + let new_dirlist = create_dirlist_with_history( + history, + curr_list.file_path(), + display_options, + tab_options, + )?; + dirlists.push(new_dirlist); } } - if !paths.is_empty() { - let config = context.config_ref().clone(); - let options = context.config_ref().display_options_ref().clone(); - let tab_options = context - .tab_context_ref() - .curr_tab_ref() - .option_ref() - .clone(); - if let Some(history) = context - .tab_context_mut() - .tab_mut(id) - .map(|t| t.history_mut()) - { - for path in paths { - let new_dirlist = create_dirlist_with_history( - history, - path.as_path(), - &config, - &options, - &tab_options, - )?; - history.insert(path, new_dirlist); - } - } + if let Some(history) = context + .tab_context_mut() + .tab_mut(id) + .map(|t| t.history_mut()) + { + history.insert_entries(dirlists); } context .message_queue_mut() diff --git a/src/commands/rename_file.rs b/src/commands/rename_file.rs index 45ad9fe..3cecc38 100644 --- a/src/commands/rename_file.rs +++ b/src/commands/rename_file.rs @@ -27,16 +27,13 @@ pub fn _rename_file( .map(|lst| lst.file_path().to_path_buf()); if let Some(path) = path { - let config = context.config_ref().clone(); - let options = context.config_ref().display_options_ref().clone(); - let tab_options = context - .tab_context_ref() - .curr_tab_ref() - .option_ref() - .clone(); + let new_dirlist = { + let display_options = context.config_ref().display_options_ref(); + let tab_options = context.tab_context_ref().curr_tab_ref().option_ref(); + let history = context.tab_context_ref().curr_tab_ref().history_ref(); + create_dirlist_with_history(history, path.as_path(), display_options, tab_options)? + }; let history = context.tab_context_mut().curr_tab_mut().history_mut(); - let new_dirlist = - create_dirlist_with_history(history, path.as_path(), &config, &options, &tab_options)?; history.insert(path, new_dirlist); } Ok(()) diff --git a/src/commands/tab_ops.rs b/src/commands/tab_ops.rs index cbba359..f1adfe2 100644 --- a/src/commands/tab_ops.rs +++ b/src/commands/tab_ops.rs @@ -1,11 +1,15 @@ -use std::path; +use std::collections::HashMap; +use std::path::Path; +use std::{io, path}; use uuid::Uuid; use crate::config::clean::app::display::new_tab::NewTabMode; use crate::context::AppContext; use crate::error::{AppError, AppErrorKind, AppResult}; -use crate::history::DirectoryHistory; +use crate::history::{ + create_dirlist_with_history, generate_entries_to_root, DirectoryHistory, JoshutoHistory, +}; use crate::tab::{JoshutoTab, TabHomePage}; use crate::util::{cwd, unix}; @@ -35,39 +39,46 @@ fn _tab_switch(new_index: usize, context: &mut AppContext) -> std::io::Result<() None => None, }; - let config = context.config_ref().clone(); - let options = context.config_ref().display_options_ref().clone(); - let tab_options = context - .tab_context_ref() - .curr_tab_ref() - .option_ref() - .clone(); + let display_options = context.config_ref().display_options_ref(); + let tab_options = context.tab_context_ref().curr_tab_ref().option_ref(); - let history = context.tab_context_mut().curr_tab_mut().history_mut(); - if history - .create_or_soft_update(cwd.as_path(), &config, &options, &tab_options) - .is_err() - { - history.remove(cwd.as_path()); - } + let history = context.tab_context_ref().curr_tab_ref().history_ref(); - if let Some(cwd_parent) = cwd.parent() { - if history - .create_or_soft_update(cwd_parent, &config, &options, &tab_options) - .is_err() - { - history.remove(cwd_parent); + let mut dirlists = Vec::with_capacity(3); + for curr_path in [ + Some(cwd.as_path().to_path_buf()), + cwd.parent().map(|p| p.to_path_buf()), + entry_path, + ] + .into_iter() + .flatten() + { + match history.get(&curr_path) { + Some(list) => { + if list.need_update() { + let dirlist = create_dirlist_with_history( + history, + cwd.as_path(), + display_options, + tab_options, + )?; + dirlists.push(dirlist); + } + } + None => { + let dirlist = create_dirlist_with_history( + history, + cwd.as_path(), + display_options, + tab_options, + )?; + dirlists.push(dirlist); + } } } - if let Some(file_path) = entry_path { - if history - .create_or_soft_update(file_path.as_path(), &config, &options, &tab_options) - .is_err() - { - history.remove(file_path.as_path()); - } - } + let history = context.tab_context_mut().curr_tab_mut().history_mut(); + history.insert_entries(dirlists); Ok(()) } @@ -140,12 +151,28 @@ pub fn new_tab(context: &mut AppContext, mode: &NewTabMode) -> AppResult { }?; if new_tab_path.exists() && new_tab_path.is_dir() { let id = Uuid::new_v4(); - let tab = JoshutoTab::new( - new_tab_path, + let mut new_tab_history = JoshutoHistory::new(); + let tab_display_options = context + .config_ref() + .display_options_ref() + .default_tab_display_option + .clone(); + let dirlists = generate_entries_to_root( + new_tab_path.as_path(), context.config_ref(), + &new_tab_history, context.ui_context_ref(), context.config_ref().display_options_ref(), + &tab_display_options, )?; + new_tab_history.insert_entries(dirlists); + + let tab_display_options = context + .config_ref() + .display_options_ref() + .default_tab_display_option + .clone(); + let tab = JoshutoTab::new(new_tab_path, new_tab_history, tab_display_options)?; context.tab_context_mut().insert_tab(id, tab); let new_index = context.tab_context_ref().len() - 1; context.tab_context_mut().index = new_index; @@ -179,3 +206,31 @@ pub fn close_tab(context: &mut AppContext) -> AppResult { _tab_switch(tab_index, context)?; Ok(()) } + +pub fn reload_all_tabs(context: &mut AppContext, curr_path: &Path) -> io::Result<()> { + let mut map = HashMap::new(); + { + let display_options = context.config_ref().display_options_ref(); + + for (id, tab) in context.tab_context_ref().iter() { + let tab_options = tab.option_ref(); + let history = tab.history_ref(); + let dirlist = + create_dirlist_with_history(history, curr_path, display_options, tab_options)?; + map.insert(*id, dirlist); + } + } + + for (id, dirlist) in map { + if let Some(tab) = context.tab_context_mut().tab_mut(&id) { + tab.history_mut().insert(curr_path.to_path_buf(), dirlist); + } + } + Ok(()) +} + +pub fn remove_entry_from_all_tabs(context: &mut AppContext, curr_path: &Path) { + for (_, tab) in context.tab_context_mut().iter_mut() { + tab.history_mut().remove(curr_path); + } +} diff --git a/src/commands/touch_file.rs b/src/commands/touch_file.rs index 41ba1c3..5fbf039 100644 --- a/src/commands/touch_file.rs +++ b/src/commands/touch_file.rs @@ -48,16 +48,14 @@ pub fn touch_file(context: &mut AppContext, arg: &str) -> AppResult { .map(|lst| lst.file_path().to_path_buf()); if let Some(path) = path { - let config = context.config_ref().clone(); - let options = context.config_ref().display_options_ref().clone(); - let tab_options = context - .tab_context_ref() - .curr_tab_ref() - .option_ref() - .clone(); + let new_dirlist = { + let options = context.config_ref().display_options_ref(); + let tab_options = context.tab_context_ref().curr_tab_ref().option_ref(); + let history = context.tab_context_ref().curr_tab_ref().history_ref(); + + create_dirlist_with_history(history, path.as_path(), options, tab_options)? + }; let history = context.tab_context_mut().curr_tab_mut().history_mut(); - let new_dirlist = - create_dirlist_with_history(history, path.as_path(), &config, &options, &tab_options)?; history.insert(path, new_dirlist); } |