diff options
Diffstat (limited to 'src/preview/preview_dir.rs')
-rw-r--r-- | src/preview/preview_dir.rs | 60 |
1 files changed, 31 insertions, 29 deletions
diff --git a/src/preview/preview_dir.rs b/src/preview/preview_dir.rs index bf1d151..03ae189 100644 --- a/src/preview/preview_dir.rs +++ b/src/preview/preview_dir.rs @@ -4,45 +4,47 @@ use std::thread; use crate::context::AppContext; use crate::event::AppEvent; +use crate::fs::JoshutoDirList; use crate::history::DirectoryHistory; -pub fn load_preview(context: &mut AppContext, p: path::PathBuf) -> io::Result<path::PathBuf> { - // get preview +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(p) + Ok(()) } -pub struct CursorDir {} - -impl CursorDir { - pub fn load(context: &mut AppContext) -> io::Result<()> { - 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()) +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(); + 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))); + } + Err(_) => {} } - } - - let res = match p { - Some(p) if p.is_dir() => load_preview(context, p), - Some(p) => Err(io::Error::new( - io::ErrorKind::InvalidData, - "Not a directory".to_string(), - )), - None => Err(io::Error::new( - io::ErrorKind::NotFound, - "No such file or directory".to_string(), - )), - }; - match res { - Ok(_) => Ok(()), - Err(e) => Err(e), - } + () + }); + handle + } else { + let handle = thread::spawn(|| ()); + handle } } |