2019-09-07 14:13:13 +00:00
|
|
|
use std::sync::{mpsc};
|
|
|
|
use crate::keyboard::{KeyboardInterceptor, KeyEvent};
|
2019-08-30 19:24:03 +00:00
|
|
|
use crate::matcher::Matcher;
|
2019-08-31 14:07:45 +00:00
|
|
|
use crate::matcher::scrolling::ScrollingMatcher;
|
|
|
|
use crate::engine::Engine;
|
2019-09-07 14:13:13 +00:00
|
|
|
use crate::config::{ConfigSet, RuntimeConfigManager};
|
2019-09-06 20:30:20 +00:00
|
|
|
use crate::ui::UIManager;
|
2019-09-08 10:31:36 +00:00
|
|
|
use std::{thread, time};
|
2019-09-05 21:06:43 +00:00
|
|
|
use clap::{App, Arg};
|
|
|
|
use std::path::Path;
|
2019-09-07 14:13:13 +00:00
|
|
|
use std::sync::mpsc::Receiver;
|
2019-09-07 15:59:34 +00:00
|
|
|
use log::{info, LevelFilter};
|
|
|
|
use simplelog::{CombinedLogger, TermLogger, TerminalMode};
|
2019-08-30 16:32:10 +00:00
|
|
|
|
2019-09-07 11:35:45 +00:00
|
|
|
mod ui;
|
|
|
|
mod bridge;
|
2019-08-31 14:07:45 +00:00
|
|
|
mod engine;
|
2019-09-01 20:00:31 +00:00
|
|
|
mod config;
|
2019-09-07 11:35:45 +00:00
|
|
|
mod system;
|
|
|
|
mod matcher;
|
|
|
|
mod keyboard;
|
2019-09-06 22:38:13 +00:00
|
|
|
mod clipboard;
|
2019-08-30 12:33:40 +00:00
|
|
|
|
2019-09-05 21:06:43 +00:00
|
|
|
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
|
|
|
|
2019-08-30 12:33:40 +00:00
|
|
|
fn main() {
|
2019-09-05 21:06:43 +00:00
|
|
|
let matches = App::new("espanso")
|
|
|
|
.version(VERSION)
|
|
|
|
.author("Federico Terzi")
|
|
|
|
.about("Cross-platform Text Expander written in Rust")
|
|
|
|
.arg(Arg::with_name("config")
|
|
|
|
.short("c")
|
|
|
|
.long("config")
|
|
|
|
.value_name("FILE")
|
2019-09-07 11:35:45 +00:00
|
|
|
.help("Sets a custom config directory. If not specified, reads the default $HOME/.espanso/default.yaml file, creating it if not present.")
|
2019-09-05 21:06:43 +00:00
|
|
|
.takes_value(true))
|
|
|
|
.arg(Arg::with_name("dump")
|
|
|
|
.long("dump")
|
|
|
|
.help("Prints all current configuration options."))
|
|
|
|
.arg(Arg::with_name("v")
|
|
|
|
.short("v")
|
|
|
|
.multiple(true)
|
|
|
|
.help("Sets the level of verbosity"))
|
|
|
|
.get_matches();
|
|
|
|
|
2019-09-07 15:59:34 +00:00
|
|
|
|
|
|
|
// 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...");
|
|
|
|
|
2019-09-07 11:35:45 +00:00
|
|
|
let config_set = match matches.value_of("config") {
|
2019-09-07 15:59:34 +00:00
|
|
|
None => {
|
|
|
|
info!("loading configuration from default location...");
|
|
|
|
ConfigSet::load_default()
|
|
|
|
},
|
|
|
|
Some(path) => {
|
|
|
|
info!("loading configuration from custom location: {}", path);
|
|
|
|
ConfigSet::load(Path::new(path))
|
|
|
|
},
|
2019-09-05 21:06:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if matches.is_present("dump") {
|
2019-09-07 11:35:45 +00:00
|
|
|
println!("{:#?}", config_set);
|
2019-09-05 21:06:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-07 11:35:45 +00:00
|
|
|
espanso_main(config_set);
|
2019-09-05 21:06:43 +00:00
|
|
|
}
|
2019-08-30 12:33:40 +00:00
|
|
|
|
2019-09-07 11:35:45 +00:00
|
|
|
fn espanso_main(config_set: ConfigSet) {
|
2019-09-07 14:13:13 +00:00
|
|
|
let (txc, rxc) = mpsc::channel();
|
|
|
|
|
|
|
|
thread::spawn(move || {
|
|
|
|
espanso_background(rxc, config_set);
|
|
|
|
});
|
|
|
|
|
|
|
|
let interceptor = keyboard::get_interceptor(txc);
|
|
|
|
interceptor.initialize();
|
|
|
|
interceptor.start();
|
|
|
|
}
|
2019-09-06 14:06:41 +00:00
|
|
|
|
2019-09-07 14:13:13 +00:00
|
|
|
fn espanso_background(rxc: Receiver<KeyEvent>, config_set: ConfigSet) {
|
2019-09-07 11:35:45 +00:00
|
|
|
let system_manager = system::get_manager();
|
2019-09-07 14:13:13 +00:00
|
|
|
let config_manager = RuntimeConfigManager::new(config_set, system_manager);
|
2019-09-07 11:35:45 +00:00
|
|
|
|
2019-09-07 14:13:13 +00:00
|
|
|
let ui_manager = ui::get_uimanager();
|
2019-09-09 13:15:01 +00:00
|
|
|
ui_manager.notify("espanso is running!");
|
2019-09-06 22:38:13 +00:00
|
|
|
|
2019-09-07 14:13:13 +00:00
|
|
|
let clipboard_manager = clipboard::get_manager();
|
2019-08-30 16:32:10 +00:00
|
|
|
|
2019-08-31 14:07:45 +00:00
|
|
|
let sender = keyboard::get_sender();
|
|
|
|
|
2019-09-06 22:38:13 +00:00
|
|
|
let engine = Engine::new(sender,
|
2019-09-07 14:13:13 +00:00
|
|
|
&clipboard_manager,
|
2019-09-08 11:37:58 +00:00
|
|
|
&config_manager,
|
|
|
|
&ui_manager
|
|
|
|
);
|
2019-08-31 14:07:45 +00:00
|
|
|
|
2019-09-07 14:13:13 +00:00
|
|
|
let matcher = ScrollingMatcher::new(&config_manager, engine);
|
|
|
|
matcher.watch(rxc);
|
2019-08-30 12:33:40 +00:00
|
|
|
}
|