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 | |
parent | 5ddbb6a3469b86177e59680c111323f5ca7ecc29 (diff) |
move back to tree_magic for mimetype parsing
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | src/commands/open_file.rs | 29 | ||||
-rw-r--r-- | src/run.rs | 3 |
3 files changed, 19 insertions, 16 deletions
@@ -12,8 +12,7 @@ chrono = "0.4.6" fs_extra = "1.1.0" lazy_static = "1.3.0" libc = "0.2.51" -mime = "0.3.13" -mime-detective = { git = "https://github.com/cjbassi/mime-detective", branch = "arch-linux" } +tree_magic = "0.2.1" notify = "4.0.10" open = "1.2.2" serde = "1.0.90" 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(); @@ -101,8 +101,7 @@ fn process_threads(context: &mut JoshutoContext, view: &JoshutoView) { Ok(progress_info) => { ui::draw_progress_bar( &view.bot_win, - progress_info.bytes_finished as f32 / - progress_info.total_bytes as f32, + progress_info.bytes_finished as f32 / progress_info.total_bytes as f32, ); } _ => {} |