diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-06-23 10:40:29 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-06-23 10:40:29 -0400 |
commit | bb758478e5c52bd9cee80d5eb7e92a394b07ae26 (patch) | |
tree | 87a80848f196bc736e53cb0691566161da822ac8 /src/preview | |
parent | 04489ccaa77273c16a0d26c1d15c88f7a721eb25 (diff) |
code cleanup
- directory loading is also on separate thread now
Diffstat (limited to 'src/preview')
-rw-r--r-- | src/preview/preview_default.rs | 2 | ||||
-rw-r--r-- | src/preview/preview_dir.rs | 77 | ||||
-rw-r--r-- | src/preview/preview_file.rs | 17 |
3 files changed, 50 insertions, 46 deletions
diff --git a/src/preview/preview_default.rs b/src/preview/preview_default.rs index 691cf72..a242c4f 100644 --- a/src/preview/preview_default.rs +++ b/src/preview/preview_default.rs @@ -6,7 +6,7 @@ use crate::ui::TuiBackend; pub fn load_preview_path(context: &mut AppContext, backend: &mut TuiBackend, p: path::PathBuf) { if p.is_dir() { - preview_dir::load_preview(context, p); + preview_dir::Background::load_preview(context, p); } else if p.is_file() { preview_file::Background::preview_path_with_script(context, backend, p); } diff --git a/src/preview/preview_dir.rs b/src/preview/preview_dir.rs index 03ae189..b25bd22 100644 --- a/src/preview/preview_dir.rs +++ b/src/preview/preview_dir.rs @@ -7,44 +7,49 @@ use crate::event::AppEvent; use crate::fs::JoshutoDirList; use crate::history::DirectoryHistory; -pub fn load_preview(context: &mut AppContext, p: path::PathBuf) -> io::Result<()> { - let options = context.config_ref().display_options_ref().clone(); - context - .tab_context_mut() - .curr_tab_mut() - .history_mut() - .create_or_soft_update(p.as_path(), &options)?; - Ok(()) -} +pub struct Foreground {} -pub fn background_load_preview( - context: &mut AppContext, - p: path::PathBuf, -) -> thread::JoinHandle<()> { - let need_to_load = match context - .tab_context_mut() - .curr_tab_mut() - .history_mut() - .get(p.as_path()) - { - Some(entry) => entry.need_update(), - None => true, - }; - if need_to_load { - let event_tx = context.events.event_tx.clone(); +impl Foreground { + pub fn load_preview(context: &mut AppContext, p: path::PathBuf) -> io::Result<()> { let options = context.config_ref().display_options_ref().clone(); - let handle = thread::spawn(move || { - match JoshutoDirList::new(p, &options) { - Ok(dirlist) => { - event_tx.send(AppEvent::PreviewDir(Ok(dirlist))); + context + .tab_context_mut() + .curr_tab_mut() + .history_mut() + .create_or_soft_update(p.as_path(), &options)?; + Ok(()) + } +} + +pub struct Background {} + +impl Background { + pub fn load_preview(context: &mut AppContext, p: path::PathBuf) -> thread::JoinHandle<()> { + let need_to_load = match context + .tab_context_mut() + .curr_tab_mut() + .history_mut() + .get(p.as_path()) + { + Some(entry) => entry.need_update(), + None => true, + }; + if need_to_load { + let event_tx = context.events.event_tx.clone(); + let options = context.config_ref().display_options_ref().clone(); + let handle = thread::spawn(move || { + match JoshutoDirList::new(p, &options) { + Ok(dirlist) => { + let _ = event_tx.send(AppEvent::PreviewDir(Ok(dirlist))); + } + Err(_) => {} } - Err(_) => {} - } - () - }); - handle - } else { - let handle = thread::spawn(|| ()); - handle + () + }); + handle + } else { + let handle = thread::spawn(|| ()); + handle + } } } diff --git a/src/preview/preview_file.rs b/src/preview/preview_file.rs index 1a2e5f9..3f13095 100644 --- a/src/preview/preview_file.rs +++ b/src/preview/preview_file.rs @@ -3,8 +3,7 @@ use std::path; use std::process::{Command, Output}; use std::thread; -use tui::layout::{Constraint, Direction, Layout, Rect}; -use tui::widgets::{Block, Borders}; +use tui::layout::Constraint; use crate::context::AppContext; use crate::event::AppEvent; @@ -77,10 +76,6 @@ impl Foreground { let curr_tab = context.tab_context_ref().curr_tab_ref(); let child_list = curr_tab.child_list_ref(); - let preview_options = context.config_ref().preview_options_ref(); - - let config = context.config_ref(); - match child_list.and_then(|list| list.curr_entry_ref()) { None => Err(io::Error::new(io::ErrorKind::Other, "No file to preview")), Some(entry) => { @@ -98,7 +93,11 @@ impl Background { backend: &mut TuiBackend, p: path::PathBuf, ) { - if let Some(preview) = context.preview_context_ref().get_preview(p.as_path()) { + if context + .preview_context_ref() + .get_preview(p.as_path()) + .is_some() + { return; } @@ -119,7 +118,7 @@ impl Background { let script = script.clone(); let event_tx = context.clone_event_tx(); - let thread = thread::spawn(move || { + let _ = thread::spawn(move || { let file_full_path = p.as_path(); let res = Command::new(script) @@ -132,7 +131,7 @@ impl Background { match res { Ok(output) => { let preview = FilePreview::from((p, output)); - event_tx.send(AppEvent::PreviewFile(preview)); + let _ = event_tx.send(AppEvent::PreviewFile(preview)); } Err(_) => {} } |