feat: drop `clipboard-master` dependency

It's just a busy loop this thing is completely unnecessary
This commit is contained in:
Harsh Shandilya 2023-12-23 15:42:03 +05:30
parent 32bc894f0a
commit c698b9d509
4 changed files with 12 additions and 87 deletions

55
Cargo.lock generated
View File

@ -115,20 +115,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clipboard-master"
version = "3.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "459887701008d8ee21f8de7f45f0f0707417c7eea3311973f6e67222bd686b7a"
dependencies = [
"objc",
"objc-foundation",
"objc_id",
"winapi",
"windows-win",
"x11-clipboard 0.5.3",
]
[[package]]
name = "clipboard-substitutor"
version = "0.7.6"
@ -136,7 +122,6 @@ dependencies = [
"anyhow",
"argv",
"assay",
"clipboard-master",
"copypasta",
"dirs",
"regex",
@ -168,7 +153,7 @@ dependencies = [
"objc",
"objc-foundation",
"objc_id",
"x11-clipboard 0.8.1",
"x11-clipboard",
]
[[package]]
@ -477,15 +462,6 @@ version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "quick-xml"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8533f14c8382aaad0d592c812ac3b826162128b65662331e1127b45c3d18536b"
dependencies = [
"memchr",
]
[[package]]
name = "quote"
version = "1.0.33"
@ -922,15 +898,6 @@ dependencies = [
"windows_x86_64_msvc 0.52.0",
]
[[package]]
name = "windows-win"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d4243ec23afe4e9b4e668b3c0a0e973f1b8265f6a46223cfcbc16fd267480c0"
dependencies = [
"winapi",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
@ -1024,15 +991,6 @@ dependencies = [
"memchr",
]
[[package]]
name = "x11-clipboard"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "473068b7b80ac86a18328824f1054e5e007898c47b5bbc281bd7abe32bc3653c"
dependencies = [
"xcb",
]
[[package]]
name = "x11-clipboard"
version = "0.8.1"
@ -1064,17 +1022,6 @@ dependencies = [
"nix",
]
[[package]]
name = "xcb"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "771e2b996df720cd1c6dd9ff90f62d91698fd3610cc078388d0564bdd6622a9c"
dependencies = [
"libc",
"log",
"quick-xml",
]
[[package]]
name = "yansi"
version = "0.5.1"

View File

@ -15,7 +15,6 @@ include = ["src/**/*", "LICENSE-*", "README.md"]
[dependencies]
anyhow = "1.0.76"
argv = "0.1.10"
clipboard-master = "3.1.3"
copypasta = { version = "0.10.0", default-features = false, features = ["x11"] }
dirs = "5.0.1"
regex = "1.10.2"

View File

@ -1,46 +1,26 @@
use std::io;
use std::ops::Not;
use clipboard_master::{CallbackResult, ClipboardHandler, Master};
use crate::config::{Act, Match, Replacements};
use anyhow::Result;
use copypasta::{ClipboardContext, ClipboardProvider};
use tracing::{debug, error};
use crate::config::{Act, Match, Replacements};
struct Handler {
ctx: ClipboardContext,
config: Replacements,
}
impl ClipboardHandler for Handler {
fn on_clipboard_change(&mut self) -> CallbackResult {
if let Ok(contents) = self.ctx.get_contents() {
if let Some(subst) = self
.config
pub fn monitor(config: Replacements) -> Result<()> {
loop {
let mut clipboard =
ClipboardContext::new().expect("Failed to get clipboard");
if let Ok(contents) = clipboard.get_contents() {
if let Some(subst) = config
.substitutors
.iter()
.find(|subst| subst.matcher.check_match(&contents))
{
if subst.name.is_empty().not() {
if !subst.name.is_empty() {
debug!(?subst.name, ?contents);
}
let result = subst.action.apply_action(&contents);
if let Err(e) = self.ctx.set_contents(result) {
if let Err(e) = clipboard.set_contents(result) {
error!("{}", e);
}
};
}
CallbackResult::Next
}
fn on_clipboard_error(&mut self, error: io::Error) -> CallbackResult {
error!("Error: {}", error);
CallbackResult::Next
}
}
pub fn monitor(config: Replacements) {
let ctx = ClipboardContext::new().expect("Failed to acquire clipboard");
let handler = Handler { ctx, config };
let _master = Master::new(handler).run();
}

View File

@ -25,8 +25,7 @@ fn main() -> Result<()> {
std::fs::read_to_string(config_path.as_path()).unwrap_or_default();
let config: Replacements = toml::from_str(&config_str)?;
config.validate()?;
monitor(config);
Ok(())
monitor(config)
}
fn check_for_version_arg() -> bool {