summaryrefslogtreecommitdiffstats
path: root/src/config/mimetype.rs
blob: a55721114641098bd0bac00ac8cbf238fa1ce969 (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
use serde_derive::Deserialize;
use std::collections::HashMap;
use std::fmt;
use std::io::Read;
use std::path::PathBuf;
use std::process;

use super::{parse_config_file, ConfigStructure};
use crate::MIMETYPE_FILE;

const fn default_false() -> bool {
    false
}

#[derive(Debug, Deserialize)]
pub struct JoshutoMimetypeEntry {
    pub command: String,
    #[serde(default)]
    pub args: Vec<String>,
    #[serde(default = "default_false")]
    pub fork: bool,
    #[serde(default = "default_false")]
    pub silent: bool,
    #[serde(default = "default_false")]
    pub confirm_exit: bool,
}

impl JoshutoMimetypeEntry {
    pub fn get_command(&self) -> &str {
        &self.command
    }

    pub fn get_args(&self) -> &[String] {
        &self.args
    }

    pub fn get_fork(&self) -> bool {
        self.fork
    }

    pub fn get_silent(&self) -> bool {
        self.silent
    }

    pub fn get_confirm_exit(&self) -> bool {
        self.confirm_exit
    }

    pub fn execute_with(&self, paths: &[&PathBuf]) {
        let program = String::from(self.get_command());

        let mut command = process::Command::new(program);
        if self.get_silent() {
            command.stdout(process::Stdio::null());
            command.stderr(process::Stdio::null());
        }

        command.args(self.get_args());
        command.args(paths.iter().map(|path| path.as_os_str()));

        match command.spawn() {
            Ok(mut handle) => {
                if !self.get_fork() {
                    ncurses::savetty();
                    ncurses::endwin();
                    match handle.wait() {
                        Ok(_) => {
                            if self.get_confirm_exit() {
                                println!(" --- Press ENTER to continue --- ");
                                std::io::stdin().bytes().next();
                            }
                        }
                        Err(e) => eprintln!("{}", e),
                    }
                    ncurses::resetty();
                    ncurses::refresh();
                }
            }
            Err(e) => eprintln!("{}", e),
        };
    }
}

impl std::fmt::Display for JoshutoMimetypeEntry {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.get_command()).unwrap();
        self.get_args()
            .iter()
            .for_each(|arg| write!(f, " {}", arg).unwrap());

        f.write_str("\t[").unwrap();
        if self.get_fork() {
            f.write_str("fork,").unwrap();
        }
        if self.get_silent() {
            f.write_str("silent").unwrap();
        }
        f.write_str("]")
    }
}

#[derive(Debug, Deserialize)]
pub struct JoshutoMimetype {
    #[serde(default, skip)]
    empty_vec: Vec<JoshutoMimetypeEntry>,
    #[serde(default)]
    pub extension: HashMap<String, Vec<JoshutoMimetypeEntry>>,
    #[serde(default)]
    pub mimetype: HashMap<String, Vec<JoshutoMimetypeEntry>>,
}

impl JoshutoMimetype {
    pub fn get_entries_for_ext(&self, extension: &str) -> &[JoshutoMimetypeEntry] {
        match self.extension.get(extension) {
            Some(s) => s,
            None => &self.empty_vec,
        }
    }
    pub fn get_entries_for_mimetype(&self, mimetype: &str) -> &[JoshutoMimetypeEntry] {
        match self.mimetype.get(mimetype) {
            Some(s) => s,
            None => &self.empty_vec,
        }
    }
}

impl ConfigStructure for JoshutoMimetype {
    fn get_config() -> Self {
        parse_config_file::<JoshutoMimetype>(MIMETYPE_FILE).unwrap_or_else(Self::default)
    }
}

impl std::default::Default for JoshutoMimetype {
    fn default() -> Self {
        JoshutoMimetype {
            empty_vec: Vec::new(),
            mimetype: HashMap::new(),
            extension: HashMap::new(),
        }
    }
}