diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-04-19 19:02:35 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-04-19 19:02:35 -0400 |
commit | 55ca6e73b25b976ba157895cbc486ab9e067a76c (patch) | |
tree | 6388d065cae7b66c460991c42b9e8d26ad2b7fd8 /src/commands/open_file.rs | |
parent | 5ddbb6a3469b86177e59680c111323f5ca7ecc29 (diff) |
move back to tree_magic for mimetype parsing
Diffstat (limited to 'src/commands/open_file.rs')
-rw-r--r-- | src/commands/open_file.rs | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index a446a14..a673ff5 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -28,21 +28,26 @@ impl OpenFile { pub fn get_options<'a>(path: &Path) -> Vec<&'a mimetype::JoshutoMimetypeEntry> { let mut mimetype_options: Vec<&mimetype::JoshutoMimetypeEntry> = Vec::new(); + /* extensions have priority */ if let Some(file_ext) = path.extension() { if let Some(file_ext) = file_ext.to_str() { if let Some(s) = MIMETYPE_T.extension.get(file_ext) { - for option in s { - mimetype_options.push(&option); - } + mimetype_options.extend(s.iter()); } } } - let detective = mime_detective::MimeDetective::new().unwrap(); - if let Ok(mime_type) = detective.detect_filepath(path) { - if let Some(s) = MIMETYPE_T.mimetype.get(mime_type.type_().as_str()) { - for option in s { - mimetype_options.push(&option); - } + let mimetype_str = tree_magic::from_filepath(&path); + + /* mime subtype have second priority */ + if let Some(s) = MIMETYPE_T.mimetype.get(&mimetype_str) { + mimetype_options.extend(s.iter()); + } + + /* generic mime type have last priority */ + if let Some(s) = mimetype_str.find('/') { + let mimetype_type = &mimetype_str[..s]; + if let Some(s) = MIMETYPE_T.mimetype.get(mimetype_type) { + mimetype_options.extend(s.iter()); } } mimetype_options @@ -90,10 +95,10 @@ impl OpenFile { ncurses::savetty(); ncurses::endwin(); - if !mimetype_options.is_empty() { - unix::open_with_entry(paths, &mimetype_options[0]); - } else { + if mimetype_options.is_empty() { open::that(&paths[0]).unwrap(); + } else { + unix::open_with_entry(paths, &mimetype_options[0]); } ncurses::resetty(); ncurses::refresh(); |