diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-08-31 11:11:06 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-31 11:11:06 -0400 |
commit | 0ca4ffd657f1a2884ca8422be8a78baa42f00de4 (patch) | |
tree | 408c91b138707942b4c7736893e4ecbefbe9a544 /src/commands | |
parent | cd838711107303e4c6076df5bc38b2b74c819480 (diff) |
use hashmap and uuid to store tabs (#194)
This is preliminary changes in order to track preview threads and progress.
The current setup is we just kick off a new thread to load
the given directory whenever we see the directory content
does not exist in history.
We don't track these threads or which tab these requests
came from. When the result is returned, we just assign it
to the current tab, instead of the tab that actually
initiated the request.
By adding uuid, we can now track which tab requested the
preview and assign it accordingly.
This will also allow us to track the status of the preview,
so we can display to the user a loading state,
when a directory is taking longer than usual to load.
This will also solve the problem of kicking off multiple
threads to read the same directory for the same tab.
Now these threads can be stored and tracked.
- side: fix reload not honouring tab sort options
- use tab specific options whenever we need to reload stuff
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/bulk_rename.rs | 2 | ||||
-rw-r--r-- | src/commands/change_directory.rs | 4 | ||||
-rw-r--r-- | src/commands/delete_files.rs | 12 | ||||
-rw-r--r-- | src/commands/flat.rs | 1 | ||||
-rw-r--r-- | src/commands/new_directory.rs | 8 | ||||
-rw-r--r-- | src/commands/open_file.rs | 2 | ||||
-rw-r--r-- | src/commands/reload.rs | 21 | ||||
-rw-r--r-- | src/commands/show_hidden.rs | 5 | ||||
-rw-r--r-- | src/commands/sort.rs | 2 | ||||
-rw-r--r-- | src/commands/sub_process.rs | 2 | ||||
-rw-r--r-- | src/commands/tab_ops.rs | 10 |
11 files changed, 38 insertions, 31 deletions
diff --git a/src/commands/bulk_rename.rs b/src/commands/bulk_rename.rs index 3a94f11..b98b0d4 100644 --- a/src/commands/bulk_rename.rs +++ b/src/commands/bulk_rename.rs @@ -127,6 +127,6 @@ pub fn bulk_rename(context: &mut AppContext, backend: &mut AppBackend) -> Joshut backend.terminal_drop(); let res = _bulk_rename(context); backend.terminal_restore()?; - reload::reload(context, context.tab_context_ref().index)?; + reload::soft_reload_curr_tab(context)?; res } diff --git a/src/commands/change_directory.rs b/src/commands/change_directory.rs index bc88afb..b612d34 100644 --- a/src/commands/change_directory.rs +++ b/src/commands/change_directory.rs @@ -51,7 +51,7 @@ pub fn parent_directory(context: &mut AppContext) -> JoshutoResult { .tab_context_mut() .curr_tab_mut() .set_cwd(parent.as_path()); - reload::soft_reload(context.tab_context_ref().index, context)?; + reload::soft_reload_curr_tab(context)?; } Ok(()) } @@ -65,7 +65,7 @@ pub fn previous_directory(context: &mut AppContext) -> JoshutoResult { .tab_context_mut() .curr_tab_mut() .set_cwd(path.as_path()); - reload::soft_reload(context.tab_context_ref().index, context)?; + reload::soft_reload_curr_tab(context)?; } Ok(()) } diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs index 59d2b5c..b9eb660 100644 --- a/src/commands/delete_files.rs +++ b/src/commands/delete_files.rs @@ -80,10 +80,10 @@ fn _delete_selected_files( let curr_tab = context.tab_context_ref().curr_tab_ref(); let options = context.config_ref().display_options_ref().clone(); let curr_path = curr_tab.cwd().to_path_buf(); - let tab_option = curr_tab.option_ref().clone(); - for tab in context.tab_context_mut().iter_mut() { + for (_, tab) in context.tab_context_mut().iter_mut() { + let tab_options = tab.option_ref().clone(); tab.history_mut() - .reload(&curr_path, &options, &tab_option)?; + .reload(&curr_path, &options, &tab_options)?; } Ok(()) } @@ -102,10 +102,10 @@ fn _delete_selected_files_background( let curr_tab = context.tab_context_ref().curr_tab_ref(); let options = context.config_ref().display_options_ref().clone(); let curr_path = curr_tab.cwd().to_path_buf(); - let tab_option = curr_tab.option_ref().clone(); - for tab in context.tab_context_mut().iter_mut() { + for (_, tab) in context.tab_context_mut().iter_mut() { + let tab_options = tab.option_ref().clone(); tab.history_mut() - .reload(&curr_path, &options, &tab_option)?; + .reload(&curr_path, &options, &tab_options)?; } Ok(()) } diff --git a/src/commands/flat.rs b/src/commands/flat.rs index b9d0a64..8e40151 100644 --- a/src/commands/flat.rs +++ b/src/commands/flat.rs @@ -75,7 +75,6 @@ pub fn flatten(depth: usize, context: &mut AppContext) -> JoshutoResult { } let sort_options = tab_options.sort_options_ref(); - contents.sort_by(|f1, f2| sort_options.compare(f1, f2)); let metadata = JoshutoMetadata::from(path.as_path())?; diff --git a/src/commands/new_directory.rs b/src/commands/new_directory.rs index 482c84e..7fb7190 100644 --- a/src/commands/new_directory.rs +++ b/src/commands/new_directory.rs @@ -7,13 +7,9 @@ use crate::history::DirectoryHistory; pub fn new_directory(context: &mut AppContext, p: &path::Path) -> JoshutoResult { std::fs::create_dir_all(p)?; let options = context.config_ref().display_options_ref().clone(); - let tab_options = context - .tab_context_ref() - .curr_tab_ref() - .option_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() { + for (_, tab) in context.tab_context_mut().iter_mut() { + let tab_options = tab.option_ref().clone(); tab.history_mut() .reload(&curr_path, &options, &tab_options)?; } diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index 1799f60..030f6ed 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -130,7 +130,7 @@ pub fn open(context: &mut AppContext, backend: &mut AppBackend) -> JoshutoResult Some(entry) if entry.file_path().is_dir() => { let path = entry.file_path().to_path_buf(); change_directory::cd(path.as_path(), context)?; - reload::soft_reload(context.tab_context_ref().index, context)?; + reload::soft_reload_curr_tab(context)?; } Some(entry) => { if context.args.file_chooser { diff --git a/src/commands/reload.rs b/src/commands/reload.rs index fa12565..f7197c1 100644 --- a/src/commands/reload.rs +++ b/src/commands/reload.rs @@ -2,10 +2,12 @@ use crate::context::AppContext; use crate::error::JoshutoResult; use crate::history::create_dirlist_with_history; +use uuid::Uuid; + // reload only if we have a queued reload -pub fn soft_reload(index: usize, context: &mut AppContext) -> std::io::Result<()> { +pub fn soft_reload(context: &mut AppContext, id: &Uuid) -> std::io::Result<()> { let mut paths = Vec::with_capacity(3); - if let Some(curr_tab) = context.tab_context_ref().tab_ref(index) { + 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()); @@ -32,7 +34,7 @@ pub fn soft_reload(index: usize, context: &mut AppContext) -> std::io::Result<() .clone(); if let Some(history) = context .tab_context_mut() - .tab_mut(index) + .tab_mut(id) .map(|t| t.history_mut()) { for path in paths { @@ -45,9 +47,14 @@ pub fn soft_reload(index: usize, context: &mut AppContext) -> std::io::Result<() Ok(()) } -pub fn reload(context: &mut AppContext, index: usize) -> std::io::Result<()> { +pub fn soft_reload_curr_tab(context: &mut AppContext) -> std::io::Result<()> { + let curr_tab_id = context.tab_context_ref().curr_tab_id(); + soft_reload(context, &curr_tab_id) +} + +pub fn reload(context: &mut AppContext, id: &Uuid) -> std::io::Result<()> { let mut paths = Vec::with_capacity(3); - if let Some(curr_tab) = context.tab_context_ref().tab_ref(index) { + 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()); } @@ -68,7 +75,7 @@ pub fn reload(context: &mut AppContext, index: usize) -> std::io::Result<()> { .clone(); if let Some(history) = context .tab_context_mut() - .tab_mut(index) + .tab_mut(id) .map(|t| t.history_mut()) { for path in paths { @@ -85,6 +92,6 @@ pub fn reload(context: &mut AppContext, index: usize) -> std::io::Result<()> { } pub fn reload_dirlist(context: &mut AppContext) -> JoshutoResult { - reload(context, context.tab_context_ref().index)?; + reload(context, &context.tab_context_ref().curr_tab_id())?; Ok(()) } diff --git a/src/commands/show_hidden.rs b/src/commands/show_hidden.rs index f0c437c..daad000 100644 --- a/src/commands/show_hidden.rs +++ b/src/commands/show_hidden.rs @@ -11,7 +11,7 @@ pub fn _toggle_hidden(context: &mut AppContext) { .display_options_mut() .set_show_hidden(opposite); - for tab in context.tab_context_mut().iter_mut() { + for (_, tab) in context.tab_context_mut().iter_mut() { tab.history_mut().depreciate_all_entries(); if let Some(s) = tab.curr_list_mut() { s.depreciate(); @@ -21,5 +21,6 @@ pub fn _toggle_hidden(context: &mut AppContext) { pub fn toggle_hidden(context: &mut AppContext) -> JoshutoResult { _toggle_hidden(context); - reload::reload_dirlist(context) + reload::soft_reload_curr_tab(context)?; + Ok(()) } diff --git a/src/commands/sort.rs b/src/commands/sort.rs index 51be534..fa350ff 100644 --- a/src/commands/sort.rs +++ b/src/commands/sort.rs @@ -24,6 +24,6 @@ pub fn toggle_reverse(context: &mut AppContext) -> JoshutoResult { } fn refresh(context: &mut AppContext) -> JoshutoResult { - reload::soft_reload(context.tab_context_ref().index, context)?; + reload::soft_reload_curr_tab(context)?; Ok(()) } diff --git a/src/commands/sub_process.rs b/src/commands/sub_process.rs index e2a33dc..fc31278 100644 --- a/src/commands/sub_process.rs +++ b/src/commands/sub_process.rs @@ -55,7 +55,7 @@ pub fn sub_process( ) -> JoshutoResult { backend.terminal_drop(); let res = execute_sub_process(context, words, spawn); - reload::soft_reload(context.tab_context_ref().index, context)?; + reload::soft_reload_curr_tab(context)?; context.message_queue_mut().push_info(format!( "{}: {}", if spawn { "Spawned" } else { "Finished" }, diff --git a/src/commands/tab_ops.rs b/src/commands/tab_ops.rs index 2a7f7fb..24a866f 100644 --- a/src/commands/tab_ops.rs +++ b/src/commands/tab_ops.rs @@ -1,5 +1,7 @@ use std::path; +use uuid::Uuid; + use crate::context::AppContext; use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult}; use crate::history::DirectoryHistory; @@ -37,6 +39,7 @@ fn _tab_switch(new_index: usize, context: &mut AppContext) -> std::io::Result<() .curr_tab_ref() .option_ref() .clone(); + let history = context.tab_context_mut().curr_tab_mut().history_mut(); if history .create_or_soft_update(cwd.as_path(), &options, &tab_options) @@ -103,13 +106,13 @@ pub fn new_tab_home_path(context: &AppContext) -> path::PathBuf { pub fn new_tab(context: &mut AppContext) -> JoshutoResult { let new_tab_path = new_tab_home_path(context); - + let id = Uuid::new_v4(); let tab = JoshutoTab::new( new_tab_path, context.ui_context_ref(), context.config_ref().display_options_ref(), )?; - context.tab_context_mut().push_tab(tab); + context.tab_context_mut().insert_tab(id, tab); let new_index = context.tab_context_ref().len() - 1; context.tab_context_mut().index = new_index; _tab_switch(new_index, context)?; @@ -120,9 +123,10 @@ pub fn close_tab(context: &mut AppContext) -> JoshutoResult { if context.tab_context_ref().len() <= 1 { return quit_with_action(context, QuitAction::Noop); } + let curr_tab_id = context.tab_context_ref().curr_tab_id(); let mut tab_index = context.tab_context_ref().index; - let _ = context.tab_context_mut().pop_tab(tab_index); + let _ = context.tab_context_mut().remove_tab(&curr_tab_id); let num_tabs = context.tab_context_ref().len(); if tab_index >= num_tabs { tab_index = num_tabs - 1; |