diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-08-05 10:24:34 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-08-05 10:25:05 -0400 |
commit | 79f99847fe4c3c1086ab6ce943969e01fd15b587 (patch) | |
tree | 4776732f3bcdda9a393719a98f1760e4c1b9839b /src | |
parent | 8105aa532c7fca7160ba3ce9303ba7613c59d277 (diff) |
rework how mimetype configuration works
- config file is now easier to visually parse and edit
- fix opening files via open_with
Diffstat (limited to 'src')
-rw-r--r-- | src/commands/open_file.rs | 4 | ||||
-rw-r--r-- | src/config/mimetype.rs | 68 | ||||
-rw-r--r-- | src/config/theme.rs | 4 | ||||
-rw-r--r-- | src/unix.rs | 18 |
4 files changed, 21 insertions, 73 deletions
diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index 419cefe..bb33c85 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -7,7 +7,6 @@ use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult}; use crate::history::DirectoryHistory; use crate::textfield::JoshutoTextField; use crate::ui; -use crate::unix; use crate::window; use crate::window::JoshutoView; @@ -191,10 +190,9 @@ impl OpenFileWith { let command = String::from(s); let args = args_iter.map(String::from).collect(); let entry = JoshutoMimetypeEntry { - id: 0, command, args, - fork: true, + fork: false, silent: false, confirm_exit: true, }; diff --git a/src/config/mimetype.rs b/src/config/mimetype.rs index 891a201..a557211 100644 --- a/src/config/mimetype.rs +++ b/src/config/mimetype.rs @@ -5,7 +5,7 @@ use std::io::Read; use std::path::PathBuf; use std::process; -use super::{parse_to_config_file, ConfigStructure, Flattenable}; +use super::{parse_config_file, ConfigStructure}; use crate::MIMETYPE_FILE; const fn default_false() -> bool { @@ -14,7 +14,6 @@ const fn default_false() -> bool { #[derive(Debug, Deserialize)] pub struct JoshutoMimetypeEntry { - pub id: usize, pub command: String, #[serde(default)] pub args: Vec<String>, @@ -27,10 +26,6 @@ pub struct JoshutoMimetypeEntry { } impl JoshutoMimetypeEntry { - pub fn get_id(&self) -> usize { - self.id - } - pub fn get_command(&self) -> &str { &self.command } @@ -105,69 +100,40 @@ impl std::fmt::Display for JoshutoMimetypeEntry { } #[derive(Debug, Deserialize)] -pub struct JoshutoRawMimetype { - #[serde(default)] - entry: Vec<JoshutoMimetypeEntry>, +pub struct JoshutoMimetype { + #[serde(default, skip)] + empty_vec: Vec<JoshutoMimetypeEntry>, #[serde(default)] - extension: HashMap<String, Vec<usize>>, + pub extension: HashMap<String, Vec<JoshutoMimetypeEntry>>, #[serde(default)] - mimetype: HashMap<String, Vec<usize>>, -} - -impl Flattenable<JoshutoMimetype> for JoshutoRawMimetype { - fn flatten(self) -> JoshutoMimetype { - let mut entries = HashMap::with_capacity(self.entry.len()); - for entry in self.entry { - entries.insert(entry.get_id(), entry); - } - JoshutoMimetype { - entries, - extension: self.extension, - mimetype: self.mimetype, - } - } -} - -#[derive(Debug)] -pub struct JoshutoMimetype { - pub entries: HashMap<usize, JoshutoMimetypeEntry>, - pub extension: HashMap<String, Vec<usize>>, - pub mimetype: HashMap<String, Vec<usize>>, + pub mimetype: HashMap<String, Vec<JoshutoMimetypeEntry>>, } impl JoshutoMimetype { - pub fn get_entries_for_ext(&self, extension: &str) -> Vec<&JoshutoMimetypeEntry> { - Self::get_entries(&self.extension, &self.entries, extension) - } - pub fn get_entries_for_mimetype(&self, mimetype: &str) -> Vec<&JoshutoMimetypeEntry> { - Self::get_entries(&self.mimetype, &self.entries, mimetype) + pub fn get_entries_for_ext(&self, extension: &str) -> &[JoshutoMimetypeEntry] { + match self.extension.get(extension) { + Some(s) => s, + None => &self.empty_vec, + } } - fn get_entries<'a>( - map: &HashMap<String, Vec<usize>>, - entry_map: &'a HashMap<usize, JoshutoMimetypeEntry>, - key: &str, - ) -> Vec<&'a JoshutoMimetypeEntry> { - match map.get(key) { - Some(entry_ids) => entry_ids - .iter() - .filter_map(|id| entry_map.get(id)) - .collect(), - None => Vec::new(), + 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_to_config_file::<JoshutoRawMimetype, JoshutoMimetype>(MIMETYPE_FILE) - .unwrap_or_else(JoshutoMimetype::default) + parse_config_file::<JoshutoMimetype>(MIMETYPE_FILE).unwrap_or_else(Self::default) } } impl std::default::Default for JoshutoMimetype { fn default() -> Self { JoshutoMimetype { - entries: HashMap::new(), + empty_vec: Vec::new(), mimetype: HashMap::new(), extension: HashMap::new(), } diff --git a/src/config/theme.rs b/src/config/theme.rs index ef5e9ba..2de2882 100644 --- a/src/config/theme.rs +++ b/src/config/theme.rs @@ -1,6 +1,8 @@ use serde_derive::Deserialize; use std::collections::HashMap; +use crate::THEME_FILE; + use super::{parse_config_file, ConfigStructure}; const fn default_zero() -> i16 { @@ -82,7 +84,7 @@ pub struct JoshutoTheme { impl ConfigStructure for JoshutoTheme { fn get_config() -> Self { - parse_config_file::<JoshutoTheme>(crate::THEME_FILE).unwrap_or_else(JoshutoTheme::default) + parse_config_file::<JoshutoTheme>(THEME_FILE).unwrap_or_else(Self::default) } } diff --git a/src/unix.rs b/src/unix.rs index 7904ee9..03c20b1 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -1,8 +1,6 @@ use std::path::{Path, PathBuf}; use std::process; -use crate::config::mimetype; - pub fn is_executable(mode: u32) -> bool { const LIBC_PERMISSION_VALS: [libc::mode_t; 3] = [libc::S_IXUSR, libc::S_IXGRP, libc::S_IXOTH]; @@ -66,19 +64,3 @@ pub fn set_mode(path: &Path, mode: u32) { } } } - -pub fn open_with_args(paths: &[&PathBuf], args: &[String]) { - let program = args[0].clone(); - - let mut command = process::Command::new(program); - command.args(args[1..].iter().cloned()); - command.args(paths.iter().map(|path| path.as_os_str())); - - match command.spawn() { - Ok(mut handle) => match handle.wait() { - Ok(_) => {} - Err(e) => eprintln!("{}", e), - }, - Err(e) => eprintln!("{}", e), - } -} |