Add yaml config loading

This commit is contained in:
Federico Terzi 2019-09-01 22:00:31 +02:00
parent 796b08b430
commit 94556d868f
7 changed files with 130 additions and 10 deletions

90
Cargo.lock generated
View File

@ -13,20 +13,110 @@ dependencies = [
"cc 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "dtoa"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "espanso"
version = "0.1.0"
dependencies = [
"cmake 0.1.42 (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)",
"widestring 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "linked-hash-map"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "proc-macro2"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quote"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde"
version = "1.0.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde_derive 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde_derive"
version = "1.0.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde_yaml"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"dtoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)",
"yaml-rust 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-xid"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "widestring"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "yaml-rust"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum cc 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)" = "8dae9c4b8fedcae85592ba623c4fd08cfdab3e3b72d6df780c6ead964a69bfff"
"checksum cmake 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "81fb25b677f8bf1eb325017cb6bb8452f87969db0fedb4f757b297bee78a7c62"
"checksum dtoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ea57b42383d091c85abcc2706240b94ab2a8fa1fc81c10ff23c4de06e2a90b5e"
"checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83"
"checksum proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "175a40b9cf564ce9bf050654633dbf339978706b8ead1a907bb970b63185dd95"
"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
"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 syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "66850e97125af79138385e9b88339cbcd037e3f28ceab8c5ad98e64f0f1f80bf"
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
"checksum widestring 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "effc0e4ff8085673ea7b9b2e3c73f6bd4d118810c9009ed8f1e16bd96c331db6"
"checksum yaml-rust 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "65923dd1784f44da1d2c3dbbc5e822045628c590ba72123e1c73d3c230c4434d"

View File

@ -7,6 +7,8 @@ build="build.rs"
[dependencies]
widestring = "0.4.0"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
[build-dependencies]
cmake = "0.1.31"

27
src/config.rs Normal file
View File

@ -0,0 +1,27 @@
use std::path::Path;
use crate::matcher::Match;
use std::fs::File;
use std::io::Read;
use serde::{Serialize, Deserialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Configs {
pub matches: Vec<Match>
}
impl Configs {
pub fn load(path: &Path) -> Configs {
let mut file_res = File::open(path);
if let Ok(mut file) = file_res {
let mut contents = String::new();
file.read_to_string(&mut contents);
let config: Configs = serde_yaml::from_str(&contents).unwrap();
config
}else{
panic!("Config file not found...")
}
}
}

View File

@ -14,6 +14,6 @@ impl <'a> Engine<'a> {
impl <'a> MatchReceiver for Engine<'a>{
fn on_match(&self, m: &Match) {
self.sender.delete_string(m.trigger.len() as i32);
self.sender.send_string(m.result.as_str());
self.sender.send_string(m.replace.as_str());
}
}

View File

@ -4,13 +4,16 @@ use crate::matcher::Matcher;
use crate::matcher::Match;
use crate::matcher::scrolling::ScrollingMatcher;
use crate::engine::Engine;
use crate::config::Configs;
use std::path::Path;
mod keyboard;
mod matcher;
mod engine;
mod config;
fn main() {
println!("espanso is running!");
let configs = Configs::load(Path::new("/home/freddy/.espanso"));
let (txc, rxc) = mpsc::channel();
@ -22,12 +25,8 @@ fn main() {
let engine = Engine::new(&sender);
let matches = vec![Match{trigger:"e'".to_owned(), result: "è".to_owned()},
Match{trigger:"e/".to_owned(), result: "é".to_owned()},
Match{trigger:":lol".to_owned(), result: "😂".to_owned()},
Match{trigger:":llol".to_owned(), result: "😂😂😂😂😂".to_owned()},
];
println!("espanso is running!");
let mut matcher = ScrollingMatcher::new(&matches, &engine);
let mut matcher = ScrollingMatcher::new(&configs.matches, &engine);
matcher.watch(&rxc);
}

View File

@ -1,10 +1,12 @@
use std::sync::mpsc::Receiver;
use serde::{Serialize, Deserialize};
pub(crate) mod scrolling;
#[derive(Debug, Serialize, Deserialize)]
pub struct Match {
pub trigger: String,
pub result: String
pub replace: String
}
pub trait MatchReceiver {

View File

@ -23,7 +23,7 @@ impl <'a> super::Matcher for ScrollingMatcher<'a> {
.filter(|&x| x.remaining.chars().nth(0).unwrap() == c)
.map(|x | MatchEntry{remaining: &x.remaining[1..], _match: &x._match})
.collect();
self.current_set = old_matches;
self.current_set.append(&mut new_matches);