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 | |
parent | 8105aa532c7fca7160ba3ce9303ba7613c59d277 (diff) |
rework how mimetype configuration works
- config file is now easier to visually parse and edit
- fix opening files via open_with
-rw-r--r-- | config/mimetype.toml | 339 | ||||
-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 |
5 files changed, 224 insertions, 209 deletions
diff --git a/config/mimetype.toml b/config/mimetype.toml index a30100e..e6c801a 100644 --- a/config/mimetype.toml +++ b/config/mimetype.toml @@ -1,140 +1,207 @@ -[[entry]] -id = 1 -commands = "nano" - -[[entry]] -id = 2 -commands = "gedit" -fork = true -silent = true - -[[entry]] -id = 3 -commands = "bat" - -[[entry]] -id = 10 -commands = "mpv" -fork = true -silent = true - -[[entry]] -id = 11 -commands = "mpv" - -[[entry]] -id = 20 -commands = "qimgv" -fork = true -silent = true - -[[entry]] -id = 21 -commands = "krita" -fork = true -silent = true - -[[entry]] -id = 50 -commands = "pdflatex" - -[[entry]] -id = 55 -commands = "evince" -fork = true -silent = true - -[[entry]] -id = 60 -commands = "tar" -args = [ "-xvzf" ] -[[entry]] -id = 61 -commands = "tar" -args = [ "-xvJf" ] -[[entry]] -id = 62 -commands = "tar" -args = [ "-xvjf" ] - -[[entry]] -id = 70 -commands = "unrar" -args = [ "e" ] -[[entry]] -id = 80 -commands = "unzip" - -[[entry]] -id = 100 -commands = "libreoffice" -fork = true -silent = true - -# specify which applications to use to open certain file types [extension] -## Image -bmp = [ 20, 21 ] -gif = [ 20, 21 ] -jpg = [ 20, 21 ] -jpeg = [ 20, 21 ] -png = [ 20, 21 ] -tiff = [ 20, 21 ] - -## Audio -mp3 = [ 11 ] -flac = [ 11 ] - -## Video -avi = [ 10 ] -av1 = [ 10 ] -mkv = [ 10 ] -mp4 = [ 10 ] -webm = [ 10 ] -wmv = [ 10 ] -# Text -build = [ 1, 2, 3 ] -c = [ 1, 2, 3 ] -cmake = [ 1, 2, 3 ] -conf = [ 1, 2, 3 ] -cpp = [ 1, 2, 3 ] -css = [ 1, 2, 3 ] -go = [ 1, 2, 3 ] -h = [ 1, 2, 3 ] -hpp = [ 1, 2, 3 ] -hs = [ 1, 2, 3 ] -html = [ 1, 2, 3 ] -ini = [ 1, 2, 3 ] -java = [ 1, 2, 3 ] -js = [ 1, 2, 3 ] -kt = [ 1, 2, 3 ] -log = [ 1, 2, 3 ] -md = [ 1, 2, 3 ] -ninja = [ 1, 2, 3 ] -py = [ 1, 2, 3 ] -rs = [ 1, 2, 3 ] -sh = [ 1, 2, 3 ] -toml = [ 1, 2, 3 ] -xml = [ 1, 2, 3 ] - -tex = [ 1, 50 ] - -# Archives -bz2 = [ 62 ] -gz = [ 60 ] -rar = [ 70 ] -xz = [ 61 ] -zip = [ 80 ] - -# Misc -odt = [ 100 ] -odf = [ 100 ] -pdf = [ 55 ] +## image formats +bmp = [ + { command = "qimgv", fork = true, silent = true }, + { command = "krita", fork = true, silent = true } ] +gif = [ + { command = "qimgv", fork = true, silent = true }, + { command = "krita", fork = true, silent = true } ] +jpeg = [ + { command = "qimgv", fork = true, silent = true }, + { command = "krita", fork = true, silent = true } ] +jpg = [ + { command = "qimgv", fork = true, silent = true }, + { command = "krita", fork = true, silent = true } ] +png = [ + { command = "qimgv", fork = true, silent = true }, + { command = "krita", fork = true, silent = true } ] +svg = [ + { command = "qimgv", fork = true, silent = true }, + { command = "krita", fork = true, silent = true } ] +tiff = [ + { command = "qimgv", fork = true, silent = true }, + { command = "krita", fork = true, silent = true } ] + +## audio formats +flac = [ + { command = "mpv" }, + { command = "mediainfo", confirm_exit = true } ] +mp3 = [ + { command = "mpv" }, + { command = "mediainfo", confirm_exit = true } ] +ogg = [ + { command = "mpv" }, + { command = "mediainfo", confirm_exit = true } ] + +## video formats +avi = [ + { command = "mpv", fork = true, silent = true }, + { command = "mediainfo", confirm_exit = true } ] +av1 = [ + { command = "mpv", fork = true, silent = true }, + { command = "mediainfo", confirm_exit = true } ] +mkv = [ + { command = "mpv", fork = true, silent = true }, + { command = "mediainfo", confirm_exit = true } ] +mp4 = [ + { command = "mpv", fork = true, silent = true }, + { command = "mediainfo", confirm_exit = true } ] +webm = [ + { command = "mpv", fork = true, silent = true }, + { command = "mediainfo", confirm_exit = true } ] +wmv = [ + { command = "mpv", fork = true, silent = true }, + { command = "mediainfo", confirm_exit = true } ] + +## text formats +build = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +c = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +cmake = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +conf = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +cpp = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +css = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +eex = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +ex = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +exs = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +go = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +h = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +hpp = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +hs = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +html = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +ini = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +java = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +js = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +kt = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +log = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +md = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +ninja = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +py = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +rs = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +sh = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +toml = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +txt = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +xml = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] + +# archive formats +bz2 = [ + { command = "tar", args = [ "-xvjf" ], confirm_exit = true }, + { command = "file-roller", fork = true, silent = true } ] +gz = [ + { command = "tar", args = [ "-xvzf" ], confirm_exit = true }, + { command = "file-roller", fork = true, silent = true } ] +rar = [ + { command = "unrar", args = [ "e" ], confirm_exit = true }, + { command = "file-roller", fork = true, silent = true } ] +xz = [ + { command = "tar", args = [ "-xvJf" ], confirm_exit = true }, + { command = "file-roller", fork = true, silent = true } ] +zip = [ + { command = "unzip", confirm_exit = true }, + { command = "file-roller", fork = true, silent = true } ] + +# misc formats +m3u = [ + { command = "nano" }, + { command = "mpv" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] +odt = [ + { command = "libreoffice", fork = true, silent = true } ] +odf = [ + { command = "libreoffice", fork = true, silent = true } ] +pdf = [ + { command = "evince", fork = true, silent = true } ] + +tex = [ + { command = "nano" }, + { command = "pdflatex" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] [mimetype] - -text = [ 1, 2, 3 ] -audio = [ 11 ] -video = [ 10 ] -image = [ 20, 21 ] +text = [ + { command = "nano" }, + { command = "gedit", fork = true, silent = true }, + { command = "bat", confirm_exit = true } ] 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), - } -} |