diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-01-02 10:59:44 -0500 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-01-02 10:59:44 -0500 |
commit | 4ce295d11f4e9173006767c295f9e3ffafee7472 (patch) | |
tree | 268745380a68eea60b8752b652d5c9dac9610a27 | |
parent | 93f2057a00294e665668e707725ea6f78debef6b (diff) |
refactor and optimize launching applications
-rw-r--r-- | src/joshuto/unix.rs | 116 |
1 files changed, 59 insertions, 57 deletions
diff --git a/src/joshuto/unix.rs b/src/joshuto/unix.rs index dee5412..241308d 100644 --- a/src/joshuto/unix.rs +++ b/src/joshuto/unix.rs @@ -1,13 +1,13 @@ extern crate libc; extern crate toml; -extern crate tree_magic; extern crate ncurses; use std::fs; use std::path; -use std::collections::HashMap; +use std::process; use joshuto::ui; +use joshuto::mimetype; use joshuto::window; pub const BITMASK : u32 = 0o170000; @@ -19,10 +19,7 @@ pub const S_IFDIR : u32 = 0o040000; /* directory */ pub const S_IFCHR : u32 = 0o020000; /* character device */ pub const S_IFIFO : u32 = 0o010000; /* FIFO */ -pub fn is_reg(mode : u32) -> bool -{ - mode & BITMASK == S_IFREG -} +pub fn is_reg(mode : u32) -> bool { mode & BITMASK == S_IFREG } pub fn get_unix_filetype(mode : u32) -> &'static str { @@ -38,31 +35,6 @@ pub fn get_unix_filetype(mode : u32) -> &'static str } } -pub fn get_mime_type(path: &path::Path) -> String -{ - tree_magic::from_filepath(path) -} - -pub fn exec_with(program : String, args : Vec<String>) -{ - use std::process::Command; - - let mut child = Command::new(program); - child.args(args); - - match child.spawn() { - Ok(mut ch) => { - match ch.wait() { - Ok(exit_code) => {}, - Err(e) => eprintln!("{}", e), - } - }, - Err(e) => { - eprintln!("{:?}", e); - }, - } -} - pub fn is_executable(mode : u32) -> bool { const LIBC_PERMISSION_VALS : [ u32 ; 3] = [ @@ -122,48 +94,78 @@ pub fn stringify_mode(mode : u32) -> String mode_str } -pub fn open_file(mime_map: &HashMap<String, Vec<Vec<String>>>, +pub fn open_file(mimetype_t: &mimetype::JoshutoMimetype, win: &window::JoshutoPanel, path: &path::Path) { use std::os::unix::fs::PermissionsExt; if let Ok(metadata) = fs::metadata(path) { let permissions : fs::Permissions = metadata.permissions(); let mode = permissions.mode(); - if is_reg(mode) { - let mime_type: String = get_mime_type(path); - - if let Some(mime_args) = mime_map.get(mime_type.as_str()) { - let mime_args_len = mime_args.len(); - if mime_args_len > 0 { - ncurses::savetty(); - ncurses::endwin(); - open_with(path, &mime_args[0]); - ncurses::resetty(); - ncurses::refresh(); + if !is_reg(mode) { + ui::wprint_err(win, "Failed to read metadata, unable to determine filetype"); + ncurses::doupdate(); + return; + } + + let file_ext: Option<&str> = match path.extension() { + Some(s) => s.to_str(), + 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) { + Some(s) => s, + None => &empty_vec, } - } else { - ui::wprint_err(win, format!("Don't know how to open: {}", mime_type).as_str()); - } + }, + None => &empty_vec, + }; + + if mimetype_options.len() > 0 { + ncurses::savetty(); + ncurses::endwin(); + open_with(path, &mimetype_options[0]); + ncurses::resetty(); + ncurses::refresh(); } else { - ui::wprint_err(win, format!("Don't know how to open: {}", get_unix_filetype(mode)).as_str()); + ui::wprint_err(win, "Don't know how to open: "); + match mimetype { + Some(s) => ncurses::wprintw(win.win, s), + None => ncurses::wprintw(win.win, "Unknown file type"), + }; } - } else { - ui::wprint_err(win, "Failed to read metadata, unable to determine filetype"); + ncurses::doupdate(); } - ncurses::doupdate(); } pub fn open_with(path: &path::Path, args: &Vec<String>) { - let args_len = args.len(); let lossy_path: String = path.as_os_str().to_os_string().into_string().unwrap(); - let program_name = args[0].clone(); + let program = args[0].clone(); + let args_len = args.len(); - let mut args_list : Vec<String> = Vec::with_capacity(args_len); - for i in 1..args.len() { - args_list.push(args[i].clone()); + let mut command = process::Command::new(program); + for i in 1..args_len { + command.arg(args[i].clone()); } - args_list.push(lossy_path); + command.arg(path.as_os_str()); - exec_with(program_name, args_list); + match command.spawn() { + Ok(mut handle) => { + match handle.wait() { + Ok(exit_code) => {}, + Err(e) => eprintln!("{}", e), + } + }, + Err(e) => { + eprintln!("{:?}", e); + }, + } } |