diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-09-26 10:24:35 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-09-26 10:24:35 -0400 |
commit | 4a05a399fab43aba7c1872f9950e0e9663fdae0d (patch) | |
tree | ac3c1cc71c8ce0d2761b856afaa8dc63e4ed45bc /src/preview | |
parent | b4a62fcbddb870d11dcf72964c048ee6e195be40 (diff) |
file previews no longer continuously spawn threads for existing previews
- file previews also honor max_preview_size option
- removed unnecessary return from cursor_move
- parent_cursor_move now works properly with preview loading
- reloading now retain directory sizes
Diffstat (limited to 'src/preview')
-rw-r--r-- | src/preview/preview_default.rs | 44 | ||||
-rw-r--r-- | src/preview/preview_dir.rs | 48 | ||||
-rw-r--r-- | src/preview/preview_file.rs | 24 |
3 files changed, 64 insertions, 52 deletions
diff --git a/src/preview/preview_default.rs b/src/preview/preview_default.rs index d2fd95e..0c5142c 100644 --- a/src/preview/preview_default.rs +++ b/src/preview/preview_default.rs @@ -1,15 +1,23 @@ use std::path; use crate::context::AppContext; +use crate::fs::JoshutoMetadata; use crate::preview::{preview_dir, preview_file}; use crate::ui::TuiBackend; -pub fn load_preview_path(context: &mut AppContext, backend: &mut TuiBackend, p: path::PathBuf) { - if p.is_dir() { +pub fn load_preview_path( + context: &mut AppContext, + backend: &mut TuiBackend, + p: path::PathBuf, + metadata: JoshutoMetadata, +) { + let preview_options = context.config_ref().preview_options_ref(); + + if metadata.is_dir() { let need_to_load = context - .tab_context_mut() - .curr_tab_mut() - .history_mut() + .tab_context_ref() + .curr_tab_ref() + .history_ref() .get(p.as_path()) .map(|e| e.need_update()) .unwrap_or(true); @@ -17,7 +25,7 @@ pub fn load_preview_path(context: &mut AppContext, backend: &mut TuiBackend, p: if need_to_load { preview_dir::Background::load_preview(context, p); } - } else if p.is_file() { + } else if metadata.len() <= preview_options.max_preview_size { let need_to_load = context .preview_context_ref() .get_preview(p.as_path()) @@ -26,19 +34,29 @@ pub fn load_preview_path(context: &mut AppContext, backend: &mut TuiBackend, p: if need_to_load { preview_file::Background::preview_path_with_script(context, backend, p); } + } else { } } pub fn load_preview(context: &mut AppContext, backend: &mut TuiBackend) { - let mut p: Option<path::PathBuf> = None; - if let Some(curr_list) = context.tab_context_ref().curr_tab_ref().curr_list_ref() { - if let Some(index) = curr_list.index { - let entry = &curr_list.contents[index]; - p = Some(entry.file_path().to_path_buf()) + let mut load_list = Vec::with_capacity(2); + + let curr_tab = context.tab_context_ref().curr_tab_ref(); + match curr_tab.curr_list_ref() { + Some(curr_list) => { + if let Some(index) = curr_list.index { + let entry = &curr_list.contents[index]; + load_list.push((entry.file_path().to_path_buf(), entry.metadata.clone())); + } + } + None => { + if let Ok(metadata) = JoshutoMetadata::from(curr_tab.cwd()) { + load_list.push((curr_tab.cwd().to_path_buf(), metadata)); + } } } - if let Some(p) = p { - load_preview_path(context, backend, p); + for (path, metadata) in load_list { + load_preview_path(context, backend, path, metadata); } } diff --git a/src/preview/preview_dir.rs b/src/preview/preview_dir.rs index b25bd22..713ac0c 100644 --- a/src/preview/preview_dir.rs +++ b/src/preview/preview_dir.rs @@ -12,11 +12,13 @@ pub struct Foreground {} impl Foreground { 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)?; + let history = context.tab_context_mut().curr_tab_mut().history_mut(); + if history + .create_or_soft_update(p.as_path(), &options) + .is_err() + { + history.remove(p.as_path()); + } Ok(()) } } @@ -25,31 +27,17 @@ 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(_) => {} + let event_tx = context.events.event_tx.clone(); + let options = context.config_ref().display_options_ref().clone(); + let handle = thread::spawn(move || { + match JoshutoDirList::from_path(p, &options) { + Ok(dirlist) => { + let _ = event_tx.send(AppEvent::PreviewDir(Ok(dirlist))); } - () - }); - handle - } else { - let handle = thread::spawn(|| ()); - handle - } + Err(_) => {} + } + () + }); + handle } } diff --git a/src/preview/preview_file.rs b/src/preview/preview_file.rs index 4d454e7..cd8a6db 100644 --- a/src/preview/preview_file.rs +++ b/src/preview/preview_file.rs @@ -10,20 +10,24 @@ use crate::event::AppEvent; use crate::ui::{self, TuiBackend}; #[derive(Clone, Debug)] +pub enum PreviewState { + NoPreview, + SomePreview(FilePreview), +} + +#[derive(Clone, Debug)] pub struct FilePreview { - pub _path: path::PathBuf, pub status: std::process::ExitStatus, pub output: String, } -impl std::convert::From<(path::PathBuf, Output)> for FilePreview { - fn from((p, output): (path::PathBuf, Output)) -> Self { +impl std::convert::From<Output> for FilePreview { + fn from(output: Output) -> Self { let s = String::from_utf8_lossy(&output.stdout).to_string(); let s2 = s.replace('\t', " "); let s = s2.to_string(); let status = output.status; Self { - _path: p, status, output: s, } @@ -93,7 +97,7 @@ impl Background { pub fn preview_path_with_script( context: &AppContext, backend: &mut TuiBackend, - p: path::PathBuf, + path: path::PathBuf, ) { let preview_options = context.config_ref().preview_options_ref(); let config = context.config_ref(); @@ -113,7 +117,7 @@ impl Background { let script = script.clone(); let event_tx = context.clone_event_tx(); let _ = thread::spawn(move || { - let file_full_path = p.as_path(); + let file_full_path = path.as_path(); let res = Command::new(script) .arg(file_full_path) @@ -124,10 +128,12 @@ impl Background { .output(); match res { Ok(output) => { - let preview = FilePreview::from((p, output)); - let _ = event_tx.send(AppEvent::PreviewFile(preview)); + let preview = FilePreview::from(output); + let _ = event_tx.send(AppEvent::PreviewFile(path, Ok(preview))); + } + Err(e) => { + let _ = event_tx.send(AppEvent::PreviewFile(path, Err(e))); } - Err(_) => {} } }); } |