refactor: use clipboard-master for events

This commit is contained in:
Harsh Shandilya 2022-04-26 22:18:21 +05:30
parent f252de7748
commit f0080559dd
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
5 changed files with 164 additions and 86 deletions

53
src/clipboard.rs Normal file
View file

@ -0,0 +1,53 @@
use std::io;
use std::ops::Not;
use clipboard_master::{CallbackResult, ClipboardHandler, Master};
use copypasta::{ClipboardContext, ClipboardProvider};
use tracing::{debug, error};
use crate::config::{Act, Match, Replacements};
struct Handler<'a> {
ctx: ClipboardContext,
config: Replacements<'a>,
}
impl<'a> ClipboardHandler for Handler<'a> {
fn on_clipboard_change(&mut self) -> CallbackResult {
if let Ok(contents) = self.ctx.get_contents() {
if let Some(subst) = self
.config
.substitutors
.iter()
.find(|subst| subst.matcher.check_match(&contents))
{
if subst.name.is_empty().not() {
debug!("{}: matched on {}...", &subst.name, truncate(&contents, 40));
}
let result = subst.action.apply_action(&contents);
if let Err(e) = self.ctx.set_contents(result.to_owned()) {
error!("{e}");
}
};
}
CallbackResult::Next
}
fn on_clipboard_error(&mut self, error: io::Error) -> CallbackResult {
error!("Error: {}", error);
CallbackResult::Next
}
}
fn truncate(s: &str, max_chars: usize) -> &str {
match s.char_indices().nth(max_chars) {
None => s,
Some((idx, _)) => &s[..idx],
}
}
pub fn monitor_clipboard<'a>(config: Replacements<'a>) {
let ctx = ClipboardContext::new().expect("Failed to acquire clipboard");
let handler = Handler { ctx, config };
let _ = Master::new(handler).run();
}

View file

@ -1,17 +1,16 @@
mod clipboard;
mod config;
#[cfg(test)]
mod test;
use std::error::Error;
use std::ops::{Deref, Not};
use std::ops::Deref;
use std::path::PathBuf;
use anyhow::{anyhow, Result};
use clipboard::{ClipboardContext, ClipboardProvider};
use dirs::config_dir;
use tracing::{debug, error};
use crate::config::{Act, Match, Replacements};
use crate::clipboard::monitor_clipboard;
use crate::config::Replacements;
const VERSION_ARGS: [&str; 3] = ["version", "-v", "--version"];
@ -24,7 +23,7 @@ fn main() -> Result<()> {
let config_str =
std::fs::read_to_string(config_path.as_path()).unwrap_or_default();
let config: Replacements<'_> = toml::from_str(&config_str)?;
loop_clipboard(config);
monitor_clipboard(config);
Ok(())
}
@ -79,43 +78,3 @@ fn get_config_path() -> Result<PathBuf> {
config_path.set_extension("toml");
Ok(config_path)
}
fn loop_clipboard(config: Replacements<'_>) {
let mut clipboard: ClipboardContext =
ClipboardProvider::new().expect("Failed to get clipboard");
let mut clipboard_contents = get_clipboard_contents(&mut clipboard);
while let Ok(contents) = clipboard_contents.as_deref() {
if let Some(subst) = config
.substitutors
.iter()
.find(|subst| subst.matcher.check_match(contents))
{
if subst.name.is_empty().not() {
debug!("{}: matched on {}...", &subst.name, truncate(contents, 40));
}
let result = subst.action.apply_action(contents);
if let Err(e) = clipboard.set_contents(result.to_owned()) {
error!("{e}");
}
};
while let Ok(new_contents) = get_clipboard_contents(&mut clipboard) {
if new_contents != contents {
clipboard_contents = Ok(new_contents);
break;
};
}
}
}
fn get_clipboard_contents(
clipboard: &mut ClipboardContext,
) -> Result<String, Box<dyn Error>> {
clipboard.get_contents()
}
fn truncate(s: &str, max_chars: usize) -> &str {
match s.char_indices().nth(max_chars) {
None => s,
Some((idx, _)) => &s[..idx],
}
}