summaryrefslogtreecommitdiffstats
path: root/src/preview.rs
blob: 3aaf9ceb8835f2cb0ae24f58398cd85ec36809e4 (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
144
145
146
147
148
use std::collections::{hash_map::Entry, HashMap};
use std::io::BufRead;
use std::path;
use std::process;

use crate::config::{JoshutoConfig, JoshutoPreviewEntry};
use crate::structs::{JoshutoDirEntry, JoshutoDirList};
use crate::tab::JoshutoTab;
use crate::ui;
use crate::window::panel::JoshutoPanel;
use crate::PREVIEW_T;

pub fn preview_parent(curr_tab: &mut JoshutoTab, win: &JoshutoPanel, config_t: &JoshutoConfig) {
    if let Some(path) = curr_tab.curr_path.parent() {
        preview_directory(&mut curr_tab.history, path, win, config_t);
    } else {
        ncurses::werase(win.win);
        win.queue_for_refresh();
    }
}

pub fn preview_entry(curr_tab: &mut JoshutoTab, win: &JoshutoPanel, config_t: &JoshutoConfig) {
    ncurses::werase(win.win);
    if let Some(s) = curr_tab.curr_list.get_curr_ref() {
        if s.path.is_dir() {
            preview_directory(&mut curr_tab.history, s.path.as_path(), win, config_t);
        } else if s.metadata.file_type.is_file() {
            if s.metadata.len <= config_t.max_preview_size {
                preview_file(s, win);
            } else {
                ui::wprint_err(win, "File size exceeds max preview size");
            }
        } else {
            ui::wprint_err(win, "Not a regular file");
        }
    }
    win.queue_for_refresh();
}

fn preview_directory(
    history: &mut HashMap<path::PathBuf, JoshutoDirList>,
    path: &path::Path,
    win: &JoshutoPanel,
    config_t: &JoshutoConfig,
) {
    match history.entry(path.to_path_buf().clone()) {
        Entry::Occupied(mut entry) => {
            win.display_contents(entry.get_mut(), config_t.scroll_offset);
        }
        Entry::Vacant(entry) => {
            if let Ok(s) = JoshutoDirList::new(path.to_path_buf().clone(), &config_t.sort_option) {
                win.display_contents(entry.insert(s), config_t.scroll_offset);
            }
        }
    }
    win.queue_for_refresh();
}

fn preview_file(entry: &JoshutoDirEntry, win: &JoshutoPanel) {
    let path = &entry.path;
    match path.extension() {
        Some(file_ext) => match PREVIEW_T.extension.get(file_ext.to_str().unwrap()) {
            Some(s) => preview_with(path, win, &s),
            None => {}
            /*
                        None => if let Some(mimetype) = tree_magic::from_filepath(&path) {
                            match PREVIEW_T.mimetype.get(mimetype.as_str()) {
                                Some(s) => preview_with(path, win, &s),
                                None => if let Some(ind) = mimetype.find('/') {
                                    let supertype = &mimetype[..ind];
                                    if supertype == "text" {
                                        preview_text(path, win);
                                    } else if let Some(s) = PREVIEW_T.mimetype.get(supertype) {
                                        preview_with(path, win, &s);
                                    }
                                },
                            }
                        }
            */
        },
        None => {}
        /*
                if let Some(mimetype) = tree_magic::from_filepath(&path) {
                    match PREVIEW_T.mimetype.get(mimetype.as_str()) {
                        Some(s) => preview_with(path, win, &s),
                        None => if let Some(ind) = mimetype.find('/') {
                            let supertype = &mimetype[..ind];
                            if supertype == "text" {
                                preview_text(path, win);
                            } else if let Some(s) = PREVIEW_T.mimetype.get(supertype) {
                                preview_with(path, win, &s);
                            }
                        },
                    }
                }
        */
    }
}

fn preview_with(path: &path::Path, win: &JoshutoPanel, entry: &JoshutoPreviewEntry) {
    let mut command = process::Command::new(&entry.program);
    command
        .args(entry.args.as_ref().unwrap_or(&Vec::new()))
        .arg(path.as_os_str())
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped());

    match command.spawn() {
        Ok(child) => {
            if let Some(output) = child.stdout {
                let reader = std::io::BufReader::new(output);

                let mut i = 0;
                for line in reader.lines() {
                    if let Ok(line) = line {
                        ncurses::mvwaddnstr(win.win, i, 0, &line, win.cols);
                        i += 1;
                    }
                    if i == win.rows {
                        break;
                    }
                }
            }
        }
        Err(e) => {
            eprintln!("{:?}", e);
            ui::wprint_err(win, e.to_string().as_str());
        }
    }
}

fn preview_text(path: &path::Path, win: &JoshutoPanel) {
    match std::fs::File::open(path) {
        Err(e) => ui::wprint_err(win, e.to_string().as_str()),
        Ok(f) => {
            let reader = std::io::BufReader::new(f);
            for (i, line) in reader.lines().enumerate() {
                if let Ok(line) = line {
                    ncurses::mvwaddstr(win.win, i as i32, 0, &line);
                }
                if i >= win.rows as usize {
                    break;
                }
            }
        }
    }
}