all: log matcher names and clipboard content when available

This commit is contained in:
Harsh Shandilya 2021-11-16 19:42:05 +05:30
parent e2abe50c03
commit e7a5ed6982
3 changed files with 94 additions and 0 deletions

78
Cargo.lock generated
View file

@ -17,6 +17,17 @@ version = "1.0.45"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee10e43ae4a853c0a3591d4e2ada1719e553be18199d9da9d4a83f5927c2f5c7" checksum = "ee10e43ae4a853c0a3591d4e2ada1719e553be18199d9da9d4a83f5927c2f5c7"
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
@ -55,6 +66,8 @@ dependencies = [
"anyhow", "anyhow",
"clipboard", "clipboard",
"dirs", "dirs",
"log",
"pretty_env_logger",
"regex", "regex",
"serde", "serde",
"serde_derive", "serde_derive",
@ -90,6 +103,19 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "env_logger"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.3" version = "0.2.3"
@ -101,6 +127,24 @@ dependencies = [
"wasi", "wasi",
] ]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "humantime"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
dependencies = [
"quick-error",
]
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.107" version = "0.2.107"
@ -160,6 +204,16 @@ dependencies = [
"objc", "objc",
] ]
[[package]]
name = "pretty_env_logger"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d"
dependencies = [
"env_logger",
"log",
]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.32" version = "1.0.32"
@ -169,6 +223,12 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "quick-error"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.10" version = "1.0.10"
@ -242,6 +302,15 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "termcolor"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "toml" name = "toml"
version = "0.5.8" version = "0.5.8"
@ -279,6 +348,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"

View file

@ -17,6 +17,8 @@ readme = "README.md"
anyhow = "1.0.45" anyhow = "1.0.45"
clipboard = "0.5.0" clipboard = "0.5.0"
dirs = "4.0.0" dirs = "4.0.0"
log = "0.4.14"
pretty_env_logger = "0.4.0"
regex = "1.5.4" regex = "1.5.4"
serde = "1.0.130" serde = "1.0.130"
serde_derive = "1.0.130" serde_derive = "1.0.130"

View file

@ -1,12 +1,16 @@
mod config; mod config;
use std::ops::Not;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use clipboard::{ClipboardContext, ClipboardProvider}; use clipboard::{ClipboardContext, ClipboardProvider};
use dirs::config_dir; use dirs::config_dir;
use log::debug;
use crate::config::{Act, Match, Replacements}; use crate::config::{Act, Match, Replacements};
fn main() -> Result<()> { fn main() -> Result<()> {
pretty_env_logger::init();
let mut config_path = config_dir().ok_or(anyhow!("Failed to get config dir"))?; let mut config_path = config_dir().ok_or(anyhow!("Failed to get config dir"))?;
config_path.push("substitutor"); config_path.push("substitutor");
config_path.push("config"); config_path.push("config");
@ -26,8 +30,18 @@ fn main() -> Result<()> {
.iter() .iter()
.find(|subst| subst.matcher.clone().check_match(&contents)) .find(|subst| subst.matcher.clone().check_match(&contents))
{ {
if subst.name.is_empty().not() {
debug!("{}: matched on {}...", &subst.name, truncate(&contents, 40));
}
let result = subst.action.clone().apply_action(contents); let result = subst.action.clone().apply_action(contents);
let _ = clipboard.set_contents(result); let _ = clipboard.set_contents(result);
}; };
} }
} }
fn truncate(s: &str, max_chars: usize) -> &str {
match s.char_indices().nth(max_chars) {
None => s,
Some((idx, _)) => &s[..idx],
}
}