mirror of
https://github.com/msfjarvis/clipboard-substitutor
synced 2025-08-15 08:57:00 +05:30
feat: drop clipboard-master
dependency
It's just a busy loop this thing is completely unnecessary
This commit is contained in:
parent
32bc894f0a
commit
c698b9d509
4 changed files with 12 additions and 87 deletions
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue