diff options
-rw-r--r-- | src/joshuto/config.rs | 53 | ||||
-rw-r--r-- | src/joshuto/keymap.rs | 65 | ||||
-rw-r--r-- | src/joshuto/mimetype.rs | 85 | ||||
-rw-r--r-- | src/main.rs | 92 |
4 files changed, 189 insertions, 106 deletions
diff --git a/src/joshuto/config.rs b/src/joshuto/config.rs index fc76894..154ceee 100644 --- a/src/joshuto/config.rs +++ b/src/joshuto/config.rs @@ -1,15 +1,18 @@ extern crate whoami; +extern crate toml; +extern crate xdg; + +use std::fs; +use std::process; use joshuto; use joshuto::sort; -use std::collections::HashMap; #[derive(Debug, Deserialize)] pub struct JoshutoRawConfig { show_hidden: Option<bool>, sort_type: Option<String>, column_ratio: Option<[usize; 3]>, - mimetypes: Option<HashMap<String, Vec<Vec<String>>>>, } impl JoshutoRawConfig { @@ -20,7 +23,6 @@ impl JoshutoRawConfig { show_hidden: Some(false), sort_type: Some(String::from("natural")), column_ratio: Some([1, 3, 4]), - mimetypes: None, } } @@ -57,17 +59,11 @@ impl JoshutoRawConfig { _ => sort::SortType::SortNatural(sort_struct), }; - let mimetypes = match self.mimetypes { - Some(s) => s, - None => HashMap::new(), - }; - JoshutoConfig { username, hostname, sort_type, column_ratio, - mimetypes, } } } @@ -78,7 +74,6 @@ pub struct JoshutoConfig { pub hostname: String, pub sort_type: joshuto::sort::SortType, pub column_ratio: (usize, usize, usize), - pub mimetypes: HashMap<String, Vec<Vec<String>>>, } impl JoshutoConfig { @@ -101,7 +96,43 @@ impl JoshutoConfig { hostname, sort_type, column_ratio: (1, 3, 4), - mimetypes: HashMap::new(), + } + } + + fn read_config() -> Option<JoshutoRawConfig> + { + let dirs = xdg::BaseDirectories::with_profile(::PROGRAM_NAME, "").unwrap(); + + let config_path = dirs.find_config_file(::CONFIG_FILE)?; + println!("config_path: {:?}", config_path); + match fs::read_to_string(&config_path) { + Ok(config_contents) => { + match toml::from_str(&config_contents) { + Ok(config) => { + Some(config) + }, + Err(e) => { + eprintln!("{}", e); + process::exit(1); + }, + } + }, + Err(e) => { + eprintln!("{}", e); + None + }, + } + } + + pub fn get_config() -> Self + { + match JoshutoConfig::read_config() { + Some(config) => { + config.flatten() + } + None => { + JoshutoConfig::new() + } } } } diff --git a/src/joshuto/keymap.rs b/src/joshuto/keymap.rs index 7c71d6b..f847011 100644 --- a/src/joshuto/keymap.rs +++ b/src/joshuto/keymap.rs @@ -1,4 +1,7 @@ -use std::str; +extern crate toml; +extern crate xdg; + +use std::fs; use std::collections::HashMap; use std::process; use std::slice; @@ -7,20 +10,20 @@ use joshuto::keymapll::JoshutoCommand; use joshuto::keymapll::Keycode; #[derive(Debug, Deserialize)] -pub struct JoshutoRawKeymaps { +pub struct JoshutoRawKeymap { keymaps: Option<HashMap<String, Vec<Vec<String>>>>, } -impl JoshutoRawKeymaps { +impl JoshutoRawKeymap { #[allow(dead_code)] pub fn new() -> Self { - JoshutoRawKeymaps { + JoshutoRawKeymap { keymaps: None, } } - pub fn flatten(self) -> JoshutoKeymaps + pub fn flatten(self) -> JoshutoKeymap { let keymaps = match self.keymaps { Some(s) => { @@ -31,8 +34,8 @@ impl JoshutoRawKeymaps { }, }; - JoshutoKeymaps { - keymaps: JoshutoRawKeymaps::unflatten_hashmap(keymaps) + JoshutoKeymap { + keymaps: JoshutoRawKeymap::unflatten_hashmap(keymaps) } } @@ -53,7 +56,7 @@ impl JoshutoRawKeymaps { process::exit(1); } }; - JoshutoRawKeymaps::insert_keycommand(&mut new_map, &mut keys, + JoshutoRawKeymap::insert_keycommand(&mut new_map, &mut keys, key, keybind.clone()); } } @@ -96,7 +99,7 @@ impl JoshutoRawKeymaps { } }; - JoshutoRawKeymaps::insert_keycommand(&mut new_map, keys, new_key, keycommand); + JoshutoRawKeymap::insert_keycommand(&mut new_map, keys, new_key, keycommand); map.insert(key as i32, JoshutoCommand::CompositeKeybind(new_map)); } None => { @@ -108,16 +111,52 @@ impl JoshutoRawKeymaps { } #[derive(Debug)] -pub struct JoshutoKeymaps { +pub struct JoshutoKeymap { pub keymaps: HashMap<i32, JoshutoCommand>, } -impl JoshutoKeymaps { - #[allow(dead_code)] +impl JoshutoKeymap { pub fn new() -> Self { - JoshutoKeymaps { + JoshutoKeymap { keymaps: HashMap::new(), } } + + fn read_config() -> Option<JoshutoRawKeymap> + { + let dirs = xdg::BaseDirectories::with_profile(::PROGRAM_NAME, "").unwrap(); + + let config_path = dirs.find_config_file(::KEYMAP_FILE)?; + println!("config_path: {:?}", config_path); + match fs::read_to_string(&config_path) { + Ok(config_contents) => { + match toml::from_str(&config_contents) { + Ok(config) => { + Some(config) + }, + Err(e) => { + eprintln!("{}", e); + process::exit(1); + }, + } + }, + Err(e) => { + eprintln!("{}", e); + None + }, + } + } + + pub fn get_config() -> JoshutoKeymap + { + match JoshutoKeymap::read_config() { + Some(config) => { + config.flatten() + } + None => { + JoshutoKeymap::new() + } + } + } } diff --git a/src/joshuto/mimetype.rs b/src/joshuto/mimetype.rs new file mode 100644 index 0000000..beeb40b --- /dev/null +++ b/src/joshuto/mimetype.rs @@ -0,0 +1,85 @@ +extern crate toml; +extern crate xdg; + +use std::fs; +use std::collections::HashMap; +use std::process; + +#[derive(Debug, Deserialize)] +pub struct JoshutoRawMimetype { + mimetypes: Option<HashMap<String, Vec<Vec<String>>>>, +} + +impl JoshutoRawMimetype { + #[allow(dead_code)] + pub fn new() -> Self + { + JoshutoRawMimetype { + mimetypes: None, + } + } + + pub fn flatten(self) -> JoshutoMimetype + { + let mimetypes = match self.mimetypes { + Some(s) => s, + None => HashMap::new(), + }; + + JoshutoMimetype { + mimetypes, + } + } +} + +#[derive(Debug)] +pub struct JoshutoMimetype { + pub mimetypes: HashMap<String, Vec<Vec<String>>>, +} + +impl JoshutoMimetype { + + pub fn new() -> Self + { + JoshutoMimetype { + mimetypes: HashMap::new(), + } + } + + fn read_config() -> Option<JoshutoRawMimetype> + { + let dirs = xdg::BaseDirectories::with_profile(::PROGRAM_NAME, "").unwrap(); + + let config_path = dirs.find_config_file(::MIMETYPE_FILE)?; + println!("config_path: {:?}", config_path); + match fs::read_to_string(&config_path) { + Ok(config_contents) => { + match toml::from_str(&config_contents) { + Ok(config) => { + Some(config) + }, + Err(e) => { + eprintln!("{}", e); + process::exit(1); + }, + } + }, + Err(e) => { + eprintln!("{}", e); + None + }, + } + } + + pub fn get_config() -> Self + { + match JoshutoMimetype::read_config() { + Some(config) => { + config.flatten() + } + None => { + JoshutoMimetype::new() + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 4749e09..7937925 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,99 +5,27 @@ extern crate xdg; // use std::collections::BTreeMap; use std::env; -use std::fs; -use std::process; mod joshuto; -const PROGRAM_NAME: &str = "joshuto"; -const CONFIG_FILE: &str = "joshuto.toml"; -const KEYMAP_FILE: &str = "keymap.toml"; - -fn read_config() -> Option<joshuto::config::JoshutoRawConfig> -{ - let dirs = xdg::BaseDirectories::with_profile(PROGRAM_NAME, "").unwrap(); - - let config_path = dirs.find_config_file(CONFIG_FILE)?; - println!("config_path: {:?}", config_path); - match fs::read_to_string(&config_path) { - Ok(config_contents) => { - match toml::from_str(&config_contents) { - Ok(config) => { - Some(config) - }, - Err(e) => { - eprintln!("{}", e); - process::exit(1); - }, - } - }, - Err(e) => { - eprintln!("{}", e); - None - }, - } -} - -fn get_config() -> joshuto::config::JoshutoConfig -{ - match read_config() { - Some(config) => { - config.flatten() - } - None => { - joshuto::config::JoshutoConfig::new() - } - } -} - -fn read_keymaps() -> Option<joshuto::keymap::JoshutoRawKeymaps> -{ - let dirs = xdg::BaseDirectories::with_profile(PROGRAM_NAME, "").unwrap(); - - let config_path = dirs.find_config_file(KEYMAP_FILE)?; - println!("config_path: {:?}", config_path); - match fs::read_to_string(&config_path) { - Ok(config_contents) => { - match toml::from_str(&config_contents) { - Ok(config) => { - Some(config) - }, - Err(e) => { - eprintln!("{}", e); - process::exit(1); - }, - } - }, - Err(e) => { - eprintln!("{}", e); - None - }, - } -} - -fn get_keymap() -> joshuto::keymap::JoshutoKeymaps -{ - match read_keymaps() { - Some(config) => { - config.flatten() - } - None => { - joshuto::keymap::JoshutoKeymaps::new() - } - } -} +pub const PROGRAM_NAME: &str = "joshuto"; +pub const CONFIG_FILE: &str = "joshuto.toml"; +pub const KEYMAP_FILE: &str = "keymap.toml"; +pub const MIMETYPE_FILE: &str = "mimetype.toml"; fn main() { let args: Vec<String> = env::args().collect(); println!("args: {:?}", args); - let config = get_config(); + let config = joshuto::config::JoshutoConfig::get_config(); println!("config:\n{:#?}", config); - let keymap = get_keymap(); + let keymap = joshuto::keymap::JoshutoKeymap::get_config(); println!("keymap:\n{:#?}", keymap); - joshuto::run(config, keymap); + let mimetype = joshuto::mimetype::JoshutoMimetype::get_config(); + println!("mimetype:\n{:#?}", mimetype); + + joshuto::run(config, keymap, mimetype); } |