diff options
Diffstat (limited to 'src/preview/preview_file.rs')
-rw-r--r-- | src/preview/preview_file.rs | 107 |
1 files changed, 14 insertions, 93 deletions
diff --git a/src/preview/preview_file.rs b/src/preview/preview_file.rs index 9b4194a..ab73428 100644 --- a/src/preview/preview_file.rs +++ b/src/preview/preview_file.rs @@ -1,24 +1,20 @@ -use std::path; -use std::process::{Command, Output}; -use std::sync::Mutex; -use std::thread; -use std::time; - -use ratatui::layout::Rect; - -use crate::context::AppContext; -use crate::event::AppEvent; -use crate::lazy_static; -use crate::ui::{views, AppBackend}; - -lazy_static! { - static ref GUARD: Mutex<()> = Mutex::new(()); -} +use std::fmt::Debug; +use std::{process::Output, time}; pub enum PreviewFileState { Loading, - Error { message: String }, - Success { data: FilePreview }, + Error(String), + Success(FilePreview), +} + +impl Debug for PreviewFileState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Loading => f.debug_tuple("Loading").finish(), + Self::Error(message) => f.debug_tuple("Error").field(message).finish(), + Self::Success(_) => f.debug_tuple("Success").finish(), + } + } } #[derive(Clone, Debug)] @@ -43,78 +39,3 @@ impl std::convert::From<Output> for FilePreview { } } } - -pub struct Background {} - -impl Background { - pub fn preview_path_with_script( - context: &mut AppContext, - backend: &mut AppBackend, - path: path::PathBuf, - ) { - let preview_options = context.config_ref().preview_options_ref(); - if let Some(script) = preview_options.preview_script.as_ref() { - if let Ok(area) = backend.terminal_ref().size() { - let area = Rect { - y: area.top() + 1, - height: area.height - 2, - ..area - }; - - let config = context.config_ref(); - let display_options = config.display_options_ref(); - let constraints = &display_options.default_layout; - let layout = if display_options.show_borders() { - views::calculate_layout_with_borders(area, constraints) - } else { - views::calculate_layout(area, constraints) - }; - let layout_rect = layout[2]; - let preview_width = layout_rect.width; - let preview_height = layout_rect.height; - - if preview_width == 0 || preview_height == 0 { - return; - } - - let script = script.clone(); - let event_tx = context.clone_event_tx(); - context - .preview_context_mut() - .previews_mut() - .insert(path.clone(), PreviewFileState::Loading); - - let _ = thread::spawn(move || { - let _locked = GUARD.lock().unwrap(); - let file_full_path = path.as_path(); - - let res = Command::new(script) - .arg("--path") - .arg(file_full_path) - .arg("--preview-width") - .arg(preview_width.to_string()) - .arg("--preview-height") - .arg(preview_height.to_string()) - .output(); - match res { - Ok(output) => { - let preview = FilePreview::from(output); - let res = AppEvent::PreviewFile { - path, - res: Box::new(Ok(preview)), - }; - let _ = event_tx.send(res); - } - Err(e) => { - let res = AppEvent::PreviewFile { - path, - res: Box::new(Err(e)), - }; - let _ = event_tx.send(res); - } - } - }); - } - } - } -} |