summaryrefslogtreecommitdiffstats
path: root/src/joshuto/preview.rs
blob: 6ec76d5f174e5f6fca7d34d464d227dc4a6c6796 (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
extern crate ncurses;
extern crate mime_detective;
extern crate mime;

use std::path;
use std::process;

use joshuto;
use joshuto::ui;
use joshuto::window;

pub fn preview_file(context: &mut joshuto::JoshutoContext)
{
    let curr_tab = &mut context.tabs[context.tab_index];

    if let Some(ref curr_list) = curr_tab.curr_list {
        if let Some(entry) = curr_list.get_curr_entry() {
            if entry.path.is_dir() {
                if let Some(dirlist) = curr_tab.history.get_mut_or_create(&entry.path, &context.config_t.sort_type) {
                    ui::display_contents(&context.config_t, &context.theme_t, &context.views.right_win, dirlist);
                } else {
                    ncurses::werase(context.views.right_win.win);
                    ncurses::waddstr(context.views.right_win.win, "Can't find direntry");
                    ncurses::wnoutrefresh(context.views.right_win.win);
                }
            } else {
                ncurses::werase(context.views.right_win.win);

                let detective = mime_detective::MimeDetective::new().unwrap();
                match detective.detect_filepath(&entry.path) {
                    Ok(mime_type) => {
                        match mime_type.type_() {
                            mime::TEXT => {
                                text_preview(&context.views.right_win, &entry.path);
                            },
                            _ => {},
                        }
                    },
                    Err(e) => {
                        ncurses::waddstr(context.views.right_win.win, e.to_string().as_str());
                    },
                }
                ncurses::wnoutrefresh(context.views.right_win.win);
            }
        }
    }
}

pub fn text_preview(win: &window::JoshutoPanel, path: &path::PathBuf)
{
/*
    let mut command = process::Command::new("bat");
    command.arg("--terminal-width");
    command.arg(win.cols.to_string());
//    command.arg("--wrap=never");
    command.arg("line-range");
    command.arg(format!("{}:{}", 0, win.rows));
    command.arg("--style=numbers");
    command.arg("--tabs");
    command.arg("4");
//    command.arg("--color");
//    command.arg("always");
    command.arg(path.as_os_str());
    command.stdout(process::Stdio::piped());
    // eprintln!("{:?}", command);

*/
    let mut command = process::Command::new("head");
    command.arg("-n");
    command.arg(win.cols.to_string());
    command.arg(path.as_os_str());
    command.stdout(process::Stdio::piped());

    match command.output() {
        Ok(s) => {
            let output = String::from_utf8_lossy(&s.stdout);
            ncurses::waddstr(win.win, &output);
        },
        Err(e) => {
            ncurses::waddstr(win.win, e.to_string().as_str());
        }
    }
    // bat joshuto.rs --terminal-width 20 --wrap=never --line-range 0:26 --style='numbers'
}