summaryrefslogtreecommitdiffstats
path: root/src/preview/preview_file.rs
blob: 7f0d1ffdec0b667fcf8268696378a587b7c4b341 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::io;
use std::path;
use std::process::{Command, Output};
use std::thread;

use tui::layout::Constraint;

use crate::context::AppContext;
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 status: std::process::ExitStatus,
    pub output: String,
    pub index: usize,
}

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 status = output.status;
        Self {
            status,
            output: s2,
            index: 0,
        }
    }
}

#[allow(dead_code)]
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();

        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 struct Background {}

impl Background {
    pub fn preview_path_with_script(
        context: &AppContext,
        backend: &mut TuiBackend,
        path: path::PathBuf,
    ) {
        let preview_options = context.config_ref().preview_options_ref();
        let config = context.config_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 script = script.clone();
            let event_tx = context.clone_event_tx();
            let _ = thread::spawn(move || {
                let file_full_path = path.as_path();

                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(output);
                        let _ = event_tx.send(AppEvent::PreviewFile(path, Ok(preview)));
                    }
                    Err(e) => {
                        let _ = event_tx.send(AppEvent::PreviewFile(path, Err(e)));
                    }
                }
            });
        }
    }
}