diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-01-06 22:15:11 -0500 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-01-06 22:15:11 -0500 |
commit | 2c68eb2baaab046280b8667925a4129ecd48f20a (patch) | |
tree | 0a21a8097c26ec9afe69e852a3e296c0194bc53f | |
parent | b3781dae43c1eb673251f4522c6d9104cd046fd3 (diff) |
change mimetype.toml format to extensions for now
- might go back to actual mimetypes later
-rw-r--r-- | config/mimetype.toml | 127 | ||||
-rw-r--r-- | src/joshuto/command/open_file.rs | 41 | ||||
-rw-r--r-- | src/joshuto/config/mimetype.rs | 40 | ||||
-rw-r--r-- | src/joshuto/unix.rs | 33 | ||||
-rw-r--r-- | src/main.rs | 3 |
5 files changed, 153 insertions, 91 deletions
diff --git a/config/mimetype.toml b/config/mimetype.toml index 2a6e3e3..7a96a5f 100644 --- a/config/mimetype.toml +++ b/config/mimetype.toml @@ -1,70 +1,79 @@ # specify which applications to use to open certain file types [mimetypes] -"application/pdf" = [ - ["evince"] -] -"application/x-7z-compressed" = [ - ["file-roller"] -] +[[mimetypes.pdf]] +program = "evince" +exec_type = "forking" -"application/x-shellscript" = [ - ["nano"], - ["gedit"] -] +[[mimetypes.7z]] +program = "file-roller" +exec_type = "forking" -"audio/mpeg" = [ - ["mpv"] -] +[[mimetypes.jpg]] +program = "qimgv" +exec_type = "forking" +[[mimetypes.jpg]] +program = "krita" +exec_type = "forking" -"image/jpeg" = [ - ["qimgv"], - ["feh"], - ["krita"] -] -"image/png" = [ - ["qimgv"], - ["feh", "-F"], - ["krita"] -] -"text/plain" = [ - ["nano"] -] +[[mimetypes.png]] +program = "qimgv" +exec_type = "forking" +[[mimetypes.png]] +program = "krita" +exec_type = "forking" -"text/x-rust" = [ - ["nano"] -] +# Audio +[[mimetypes.flac]] +program = "mpv" -"text/x-csrc" = [ - ["nano"] -] +[[mimetypes.wav]] +program = "mpv" -"text/x-markdown" = [ - ["nano"] -] +# Videos +[[mimetypes.mkv]] +program = "mpv" +exec_type = "forking" -"text/x-toml" = [ - ["nano"] -] +[[mimetypes.mp4]] +program = "mpv" +exec_type = "forking" + +[[mimetypes.webm]] +program = "mpv" +exec_type = "forking" + +[[mimetypes.wmv]] +program = "mpv" +exec_type = "forking" + +# text +[[mimetypes.c]] +program = "nano" +[[mimetypes.cpp]] +program = "nano" +[[mimetypes.py]] +program = "nano" +[[mimetypes.conf]] +program = "nano" +[[mimetypes.java]] +program = "nano" +[[mimetypes.html]] +program = "nano" +[[mimetypes.md]] +program = "nano" +[[mimetypes.rs]] +program = "nano" +[[mimetypes.toml]] +program = "nano" +[[mimetypes.xml]] +program = "nano" + +[[mimetypes.txt]] +program = "nano" + + +[[mimetypes.odt]] +program = "libreoffice" +exec_type = "forking" -"audio/flac" = [ - [ "mpv" ] -] -"audio/x-wav" = [ - [ "mpv" ] -] -"video/mp4" = [ - ["mpv"] -] -"video/webm" = [ - ["mpv"] -] -"video/x-matroska" = [ - ["mpv"] -] -"video/x-ms-wmv" = [ - ["mpv"] -] -"application/vnd.oasis.opendocument.text" = [ - ["libreoffice"] -] diff --git a/src/joshuto/command/open_file.rs b/src/joshuto/command/open_file.rs index 26ef400..a1c03b4 100644 --- a/src/joshuto/command/open_file.rs +++ b/src/joshuto/command/open_file.rs @@ -55,15 +55,10 @@ impl command::Runnable for OpenFile { None => None, }; - let mimetype: Option<&str> = match file_ext { - Some(extstr) => mime_guess::get_mime_type_str(extstr), - None => None, - }; - - let empty_vec: Vec<Vec<String>> = Vec::new(); - let mimetype_options: &Vec<Vec<String>> = match mimetype { - Some(mimetype) => { - match context.mimetype_t.mimetypes.get(mimetype) { + let empty_vec: Vec<mimetype::JoshutoMimetypeEntry> = Vec::new(); + let mimetype_options = match file_ext { + Some(file_ext) => { + match context.mimetype_t.mimetypes.get(file_ext) { Some(s) => s, None => &empty_vec, } @@ -76,16 +71,11 @@ impl command::Runnable for OpenFile { if mimetype_options.len() > 0 { ncurses::savetty(); ncurses::endwin(); - unix::open_with(path.as_path(), &mimetype_options[0]); + unix::open_with_entry(path.as_path(), &mimetype_options[0]); ncurses::resetty(); ncurses::refresh(); } else { - match mimetype { - Some(s) => ui::wprint_err(&context.views.bot_win, - format!("Don't know how to open: {}", s).as_str()), - None => ui::wprint_err(&context.views.bot_win, - "Uh oh, mime_guess says unknown file type :("), - }; + ui::wprint_err(&context.views.bot_win, "Don't know how to open file :("); } ncurses::doupdate(); @@ -169,15 +159,10 @@ impl OpenFileWith { None => None, }; - let mimetype: Option<&str> = match file_ext { - Some(extstr) => mime_guess::get_mime_type_str(extstr), - None => None, - }; - - let empty_vec: Vec<Vec<String>> = Vec::new(); - let mimetype_options: &Vec<Vec<String>> = match mimetype { - Some(mimetype) => { - match mimetype_t.mimetypes.get(mimetype) { + let empty_vec: Vec<mimetype::JoshutoMimetypeEntry> = Vec::new(); + let mimetype_options = match file_ext { + Some(file_ext) => { + match mimetype_t.mimetypes.get(file_ext) { Some(s) => s, None => &empty_vec, } @@ -192,7 +177,7 @@ impl OpenFileWith { let mut display_vec: Vec<String> = Vec::with_capacity(option_size); for (i, val) in mimetype_options.iter().enumerate() { - display_vec.push(format!(" {}\t{}", i, val.join(" "))); + display_vec.push(format!(" {}\t{}", i, val)); } display_vec.sort(); @@ -220,7 +205,7 @@ impl OpenFileWith { if s < mimetype_options.len() { ncurses::savetty(); ncurses::endwin(); - unix::open_with(pathbuf.as_path(), &mimetype_options[s]); + unix::open_with_entry(pathbuf.as_path(), &mimetype_options[s]); ncurses::resetty(); ncurses::refresh(); } @@ -229,7 +214,7 @@ impl OpenFileWith { let args: Vec<String> = user_input.split_whitespace().map(|x| String::from(x)).collect(); ncurses::savetty(); ncurses::endwin(); - unix::open_with(pathbuf.as_path(), &args); + unix::open_with_args(pathbuf.as_path(), &args); ncurses::resetty(); ncurses::refresh(); } diff --git a/src/joshuto/config/mimetype.rs b/src/joshuto/config/mimetype.rs index 5520791..f8f9287 100644 --- a/src/joshuto/config/mimetype.rs +++ b/src/joshuto/config/mimetype.rs @@ -1,13 +1,49 @@ extern crate toml; extern crate xdg; +use std::fmt; use std::fs; use std::collections::HashMap; use std::process; +#[allow(non_camel_case_types)] +#[derive(Debug, Deserialize)] +pub enum ExecType { + forking +} + +#[derive(Debug, Deserialize)] +pub struct JoshutoMimetypeEntry { + pub program: String, + pub args: Option<Vec<String>>, + pub exec_type: Option<String>, +} + +impl std::fmt::Display for JoshutoMimetypeEntry { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result + { + let mut fmt_result = f.write_str(self.program.as_str()); + match self.args.as_ref() { + Some(s) => { + for arg in s { + fmt_result = write!(f, "{} ", arg); + } + }, + None => {}, + } + match self.exec_type.as_ref() { + Some(s) => { + fmt_result = f.write_str(&s); + }, + None => {}, + } + fmt_result + } +} + #[derive(Debug, Deserialize)] pub struct JoshutoRawMimetype { - mimetypes: Option<HashMap<String, Vec<Vec<String>>>>, + mimetypes: Option<HashMap<String, Vec<JoshutoMimetypeEntry>>>, } impl JoshutoRawMimetype { @@ -34,7 +70,7 @@ impl JoshutoRawMimetype { #[derive(Debug)] pub struct JoshutoMimetype { - pub mimetypes: HashMap<String, Vec<Vec<String>>>, + pub mimetypes: HashMap<String, Vec<JoshutoMimetypeEntry>>, } impl JoshutoMimetype { diff --git a/src/joshuto/unix.rs b/src/joshuto/unix.rs index ea8b4dd..d593624 100644 --- a/src/joshuto/unix.rs +++ b/src/joshuto/unix.rs @@ -4,6 +4,8 @@ extern crate ncurses; use std::path; use std::process; +use joshuto::config::mimetype; + pub const BITMASK : u32 = 0o170000; pub const S_IFSOCK : u32 = 0o140000; /* socket */ pub const S_IFLNK : u32 = 0o120000; /* symbolic link */ @@ -95,7 +97,36 @@ pub fn stringify_mode(mode: u32) -> String mode_str } -pub fn open_with(path: &path::Path, args: &Vec<String>) +pub fn open_with_entry(path: &path::Path, entry: &mimetype::JoshutoMimetypeEntry) +{ + let program = entry.program.clone(); + + let mut command = process::Command::new(program); + if let Some(args) = entry.args.as_ref() { + let args_len = args.len(); + for i in 1..args_len { + command.arg(args[i].clone()); + } + } + command.arg(path.as_os_str()); + + match command.spawn() { + Ok(mut handle) => { + if let Some(_) = entry.exec_type { + } else { + match handle.wait() { + Ok(_) => {}, + Err(e) => eprintln!("{}", e), + } + } + }, + Err(e) => { + eprintln!("{:?}", e); + }, + } +} + +pub fn open_with_args(path: &path::Path, args: &Vec<String>) { let program = args[0].clone(); let args_len = args.len(); diff --git a/src/main.rs b/src/main.rs index 750fd3d..5bcc57e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,9 +32,10 @@ fn main() // println!("{:#?}", config); let mimetype = joshuto::config::JoshutoMimetype::get_config(); +// println!("{:#?}", mimetype); + let keymap = joshuto::config::JoshutoKeymap::get_config(); // println!("{:#?}", keymap); -// println!("{:#?}", keymap.keymaps); let theme = joshuto::config::JoshutoTheme::get_config(); // println!("{:#?}", theme); |