summaryrefslogtreecommitdiffstats
path: root/src/preview/preview_file.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/preview/preview_file.rs')
-rw-r--r--src/preview/preview_file.rs190
1 files changed, 123 insertions, 67 deletions
diff --git a/src/preview/preview_file.rs b/src/preview/preview_file.rs
index 5aabb13..1a2e5f9 100644
--- a/src/preview/preview_file.rs
+++ b/src/preview/preview_file.rs
@@ -1,86 +1,142 @@
use std::io;
use std::path;
use std::process::{Command, Output};
+use std::thread;
use tui::layout::{Constraint, Direction, Layout, Rect};
use tui::widgets::{Block, Borders};
use crate::context::AppContext;
-use crate::ui::TuiBackend;
-
-pub fn preview_path_with_script(
- context: &AppContext,
- backend: &mut TuiBackend,
- p: path::PathBuf,
-) -> io::Result<Output> {
- let preview_options = context.config_ref().preview_options_ref();
- let config = context.config_ref();
-
- match preview_options.preview_script.as_ref() {
- None => Err(io::Error::new(
- io::ErrorKind::Other,
- "No preview script specified",
- )),
- Some(script) => {
- let area = backend.terminal.as_ref().unwrap().size().unwrap();
+use crate::event::AppEvent;
+use crate::ui::{self, TuiBackend};
- let constraints: &[Constraint; 3] = &config.display_options_ref().default_layout;
-
- let layout_rect = if config.display_options_ref().show_borders() {
- let area = Rect {
- y: area.top() + 1,
- height: area.height - 2,
- ..area
- };
-
- let block = Block::default().borders(Borders::ALL);
- let inner = block.inner(area);
-
- let layout_rect = Layout::default()
- .direction(Direction::Horizontal)
- .constraints(constraints.as_ref())
- .split(inner);
-
- let block = Block::default().borders(Borders::LEFT);
- let inner3 = block.inner(layout_rect[2]);
- inner3
- } else {
- let layout_rect = Layout::default()
- .direction(Direction::Horizontal)
- .vertical_margin(1)
- .constraints(constraints.as_ref())
- .split(area);
- layout_rect[2]
- };
-
- let file_full_path = p.as_path();
- let preview_width = layout_rect.width;
- let preview_height = layout_rect.height;
- let image_cache = 0;
- let preview_image = if preview_options.preview_images { 1 } else { 0 };
+#[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 {
+ let s = String::from_utf8_lossy(&output.stdout).to_string();
+ let status = output.status;
+ Self {
+ _path: p,
+ status,
+ output: s,
+ }
+ }
+}
+
+pub struct Foreground {}
+
+impl Foreground {
+ pub fn preview_path_with_script(
+ context: &AppContext,
+ backend: &mut TuiBackend,
+ p: path::PathBuf,
+ ) -> io::Result<Output> {
+ let preview_options = context.config_ref().preview_options_ref();
+ let config = context.config_ref();
+
+ match preview_options.preview_script.as_ref() {
+ None => Err(io::Error::new(
+ io::ErrorKind::Other,
+ "No preview script specified",
+ )),
+ Some(script) => {
+ let area = backend.terminal.as_ref().unwrap().size().unwrap();
+ let display_options = config.display_options_ref();
+ let constraints: &[Constraint; 3] = &display_options.default_layout;
+
+ let layout_rect = ui::build_layout(area, constraints, display_options)[2];
+
+ let file_full_path = p.as_path();
+ let preview_width = layout_rect.width;
+ let preview_height = layout_rect.height;
+ let image_cache = 0;
+ let preview_image = if preview_options.preview_images { 1 } else { 0 };
+
+ // spawn preview process
+ Command::new(script)
+ .arg(file_full_path)
+ .arg(preview_width.to_string())
+ .arg(preview_height.to_string())
+ .arg(image_cache.to_string())
+ .arg(preview_image.to_string())
+ .output()
+ }
+ }
+ }
+
+ pub fn preview_with_script(
+ context: &AppContext,
+ backend: &mut TuiBackend,
+ ) -> io::Result<Output> {
+ 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();
- // spawn preview process
- Command::new(script)
- .arg(file_full_path)
- .arg(preview_width.to_string())
- .arg(preview_height.to_string())
- .arg(image_cache.to_string())
- .arg(preview_image.to_string())
- .output()
+ 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) => {
+ Self::preview_path_with_script(context, backend, entry.file_path().to_path_buf())
+ }
}
}
}
-pub fn preview_with_script(context: &AppContext, backend: &mut TuiBackend) -> io::Result<Output> {
- let curr_tab = context.tab_context_ref().curr_tab_ref();
- let child_list = curr_tab.child_list_ref();
+pub struct Background {}
+
+impl Background {
+ pub fn preview_path_with_script(
+ context: &AppContext,
+ backend: &mut TuiBackend,
+ p: path::PathBuf,
+ ) {
+ if let Some(preview) = context.preview_context_ref().get_preview(p.as_path()) {
+ return;
+ }
+
+ let preview_options = context.config_ref().preview_options_ref();
+ let config = context.config_ref();
- let preview_options = context.config_ref().preview_options_ref();
+ if let Some(script) = preview_options.preview_script.as_ref() {
+ let area = backend.terminal.as_ref().unwrap().size().unwrap();
+ let display_options = config.display_options_ref();
+ let constraints: &[Constraint; 3] = &display_options.default_layout;
+
+ let layout_rect = ui::build_layout(area, constraints, display_options)[2];
+
+ let preview_width = layout_rect.width;
+ let preview_height = layout_rect.height;
+ let image_cache = 0;
+ let preview_image = if preview_options.preview_images { 1 } else { 0 };
- let config = context.config_ref();
+ let script = script.clone();
+ let event_tx = context.clone_event_tx();
+ let thread = thread::spawn(move || {
+ let file_full_path = p.as_path();
- match child_list.and_then(|list| list.curr_entry_ref()) {
- None => Err(io::Error::new(io::ErrorKind::Other, "No file to preview")),
- Some(entry) => preview_path_with_script(context, backend, entry.file_path().to_path_buf()),
+ let res = Command::new(script)
+ .arg(file_full_path)
+ .arg(preview_width.to_string())
+ .arg(preview_height.to_string())
+ .arg(image_cache.to_string())
+ .arg(preview_image.to_string())
+ .output();
+ match res {
+ Ok(output) => {
+ let preview = FilePreview::from((p, output));
+ event_tx.send(AppEvent::PreviewFile(preview));
+ }
+ Err(_) => {}
+ }
+ });
+ }
}
}