diff --git a/Cargo.lock b/Cargo.lock index 6da9d74..a44a3f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,6 +38,11 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "autocfg" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "backtrace" version = "0.3.36" @@ -96,6 +101,17 @@ name = "cfg-if" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "chrono" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "clap" version = "2.33.0" @@ -172,9 +188,11 @@ dependencies = [ "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "cmake 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", "serde_yaml 0.8.9 (registry+https://github.com/rust-lang/crates.io-index)", + "simplelog 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "widestring 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -218,6 +236,14 @@ name = "linked-hash-map" version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "log" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "memchr" version = "2.2.1" @@ -228,6 +254,23 @@ name = "nodrop" version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "num-integer" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-traits" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "proc-macro2" version = "0.4.30" @@ -370,6 +413,16 @@ dependencies = [ "yaml-rust 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "simplelog" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "term 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "strsim" version = "0.8.0" @@ -406,6 +459,15 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "term" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -422,6 +484,16 @@ dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "time" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "unicode-width" version = "0.1.6" @@ -480,6 +552,7 @@ dependencies = [ "checksum arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0d382e583f07208808f6b1249e60848879ba3543f57c32277bf52d69c2f0f0ee" "checksum arrayvec 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b8d73f9beda665eaa98ab9e4f7442bd4e7de6652587de55b2525e52e29c1b0ba" "checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90" +"checksum autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "b671c8fb71b457dd4ae18c4ba1e59aa81793daacc361d82fcd410cef0d491875" "checksum backtrace 0.3.36 (registry+https://github.com/rust-lang/crates.io-index)" = "e43b13e6ad6177777669f60f642e26307b44676392fae3610676edb54073681d" "checksum backtrace-sys 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "82a830b4ef2d1124a711c71d263c5abdc710ef8e907bd508c88be475cebc422b" "checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e" @@ -488,6 +561,7 @@ dependencies = [ "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" "checksum cc 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)" = "8dae9c4b8fedcae85592ba623c4fd08cfdab3e3b72d6df780c6ead964a69bfff" "checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" +"checksum chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e8493056968583b0193c1bb04d6f7684586f3726992d6c573261941a895dbd68" "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum cmake 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "81fb25b677f8bf1eb325017cb6bb8452f87969db0fedb4f757b297bee78a7c62" @@ -502,8 +576,11 @@ dependencies = [ "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba" "checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83" +"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" "checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e" "checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945" +"checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09" +"checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32" "checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" "checksum proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "175a40b9cf564ce9bf050654633dbf339978706b8ead1a907bb970b63185dd95" "checksum quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" @@ -521,12 +598,15 @@ dependencies = [ "checksum serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)" = "fec2851eb56d010dc9a21b89ca53ee75e6528bab60c11e89d38390904982da9f" "checksum serde_derive 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)" = "cb4dc18c61206b08dc98216c98faa0232f4337e1e1b8574551d5bad29ea1b425" "checksum serde_yaml 0.8.9 (registry+https://github.com/rust-lang/crates.io-index)" = "38b08a9a90e5260fe01c6480ec7c811606df6d3a660415808c3c3fa8ed95b582" +"checksum simplelog 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ebbe8c881061cce7ee205784634eda7a61922925e7cc2833188467d3a560e027" "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" "checksum syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "66850e97125af79138385e9b88339cbcd037e3f28ceab8c5ad98e64f0f1f80bf" "checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f" +"checksum term 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c0863a3345e70f61d613eab32ee046ccd1bcc5f9105fe402c61fcd0c13eeb8b5" "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" +"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7007dbd421b92cc6e28410fe7362e2e0a2503394908f417b68ec8d1c364c4e20" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" diff --git a/Cargo.toml b/Cargo.toml index e544e1a..61e76b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,8 @@ serde_yaml = "0.8" dirs = "2.0.2" clap = "2.33.0" regex = "1.3.1" +log = "0.4.8" +simplelog = "0.7.1" [build-dependencies] cmake = "0.1.31" \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index c8b697b..6328eff 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,13 +10,14 @@ use crate::keyboard::KeyModifier; use crate::system::SystemManager; use std::collections::HashSet; use regex::Regex; +use std::process::exit; +use log::{debug, info, warn, error}; // TODO: add documentation link const DEFAULT_CONFIG_FILE_CONTENT : &str = include_str!("res/config.yaml"); const DEFAULT_CONFIG_FILE_NAME : &str = "default.yaml"; - // Default values for primitives fn default_name() -> String{ "default".to_owned() } fn default_filter_title() -> String{ "".to_owned() } @@ -85,7 +86,8 @@ pub struct ConfigSet { impl ConfigSet { // TODO: tests pub fn load(dir_path: &Path) -> ConfigSet { if !dir_path.is_dir() { - panic!("Invalid config directory"); + error!("Invalid config directory"); + exit(2); } let default_file = dir_path.join(DEFAULT_CONFIG_FILE_NAME); @@ -111,11 +113,13 @@ impl ConfigSet { // TODO: tests let config = Configs::load_config(path.as_path()); if config.name == "default" { - panic!(format!("Error while parsing {} : please a name", path.to_str().unwrap())) + error!("Error while parsing {}, please specify a 'name' field", path.to_str().unwrap_or("")); + continue; } if name_set.contains(&config.name) { - panic!(format!("Error while parsing {} : the specified name is already used, please specify another one", path.to_str().unwrap())) + error!("Error while parsing {} : the specified name is already used, please use another one", path.to_str().unwrap_or("")); + continue; } name_set.insert(config.name.clone()); @@ -150,15 +154,14 @@ impl ConfigSet { // TODO: tests } } - panic!("Could not generate default position for config file"); + error!("Could not generate default position for config file"); + exit(1); } } pub trait ConfigManager { - fn toggle_key(&self) -> &KeyModifier; - fn toggle_interval(&self) -> u32; - fn backspace_limit(&self) -> i32; - fn backend(&self) -> &BackendType; + fn active_config(&self) -> &Configs; + fn default_config(&self) -> &Configs; fn matches(&self) -> &Vec; } @@ -181,7 +184,7 @@ impl RuntimeConfigManager { if let Ok(regex) = res { Some(regex) }else{ - // TODO: log invalid regex error + warn!("Invalid regex in 'filter_title' field of configuration {}, ignoring it...", config.name); None } } @@ -196,7 +199,7 @@ impl RuntimeConfigManager { } } -impl RuntimeConfigManager { +impl ConfigManager for RuntimeConfigManager { fn active_config(&self) -> &Configs { // TODO: optimize performance by avoiding some of these checks if no Configs use the filters @@ -206,6 +209,9 @@ impl RuntimeConfigManager { for (i, regex) in self.title_regexps.iter().enumerate() { if let Some(regex) = regex { if regex.is_match(&title) { + debug!("Matched 'filter_title' for '{}' config, using custom settings.", + self.set.specific[i].name); + return &self.set.specific[i] } } @@ -213,25 +219,12 @@ impl RuntimeConfigManager { } // No matches, return the default mapping + debug!("No matches for custom configs, using default settings."); &self.set.default } -} -impl ConfigManager for RuntimeConfigManager { - fn toggle_key(&self) -> &KeyModifier { - &self.active_config().toggle_key - } - - fn toggle_interval(&self) -> u32 { - self.active_config().toggle_interval - } - - fn backspace_limit(&self) -> i32 { - self.active_config().backspace_limit - } - - fn backend(&self) -> &BackendType { - &self.active_config().backend + fn default_config(&self) -> &Configs { + &self.set.default } fn matches(&self) -> &Vec { diff --git a/src/engine.rs b/src/engine.rs index 9846f51..82ebfe4 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -18,9 +18,11 @@ impl <'a, S: KeyboardSender, C: ClipboardManager, M: ConfigManager> Engine<'a, S impl <'a, S: KeyboardSender, C: ClipboardManager, M: ConfigManager> MatchReceiver for Engine<'a, S, C, M>{ fn on_match(&self, m: &Match) { + let config = self.config_manager.default_config(); + self.sender.delete_string(m.trigger.len() as i32); - match self.config_manager.backend() { + match config.backend { BackendType::Inject => { // Send the expected string. On linux, newlines are managed automatically // while on windows and macos, we need to emulate a Enter key press. diff --git a/src/main.rs b/src/main.rs index 2b95557..72a3fb2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,8 @@ use std::thread; use clap::{App, Arg}; use std::path::Path; use std::sync::mpsc::Receiver; +use log::{info, LevelFilter}; +use simplelog::{CombinedLogger, TermLogger, TerminalMode}; mod ui; mod bridge; @@ -41,9 +43,31 @@ fn main() { .help("Sets the level of verbosity")) .get_matches(); + + // Setup logging + let log_level = match matches.occurrences_of("v") { + 0 => LevelFilter::Warn, + 1 => LevelFilter::Info, + 2 | _ => LevelFilter::Debug, + }; + CombinedLogger::init( + vec![ + TermLogger::new(log_level, simplelog::Config::default(), TerminalMode::Mixed).unwrap(), + //WriteLogger::new(LevelFilter::Info, Config::default(), File::create("my_rust_binary.log").unwrap()), + ] + ).unwrap(); + + info!("espanso is starting..."); + let config_set = match matches.value_of("config") { - None => {ConfigSet::load_default()}, - Some(path) => {ConfigSet::load(Path::new(path))}, + None => { + info!("loading configuration from default location..."); + ConfigSet::load_default() + }, + Some(path) => { + info!("loading configuration from custom location: {}", path); + ConfigSet::load(Path::new(path)) + }, }; if matches.is_present("dump") { diff --git a/src/matcher/scrolling.rs b/src/matcher/scrolling.rs index ee6f676..7b630ae 100644 --- a/src/matcher/scrolling.rs +++ b/src/matcher/scrolling.rs @@ -60,7 +60,7 @@ impl <'a, R: MatchReceiver, M: ConfigManager> super::Matcher for ScrollingMatche current_set_queue.push_back(combined_matches); - if current_set_queue.len() as i32 > (self.config_manager.backspace_limit() + 1) { + if current_set_queue.len() as i32 > (self.config_manager.default_config().backspace_limit + 1) { current_set_queue.pop_front(); } @@ -73,10 +73,12 @@ impl <'a, R: MatchReceiver, M: ConfigManager> super::Matcher for ScrollingMatche } fn handle_modifier(&self, m: KeyModifier) { - if m == *self.config_manager.toggle_key() { + let config = self.config_manager.default_config(); + + if m == config.toggle_key { let mut toggle_press_time = self.toggle_press_time.borrow_mut(); if let Ok(elapsed) = toggle_press_time.elapsed() { - if elapsed.as_millis() < self.config_manager.toggle_interval() as u128 { + if elapsed.as_millis() < config.toggle_interval as u128 { let mut is_enabled = self.is_enabled.borrow_mut(); *is_enabled = !(*is_enabled);