refactor

linux support + transform
This commit is contained in:
JMARyA 2025-04-05 23:54:04 +02:00
parent 6f70194871
commit 0e44384e80
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
5 changed files with 569 additions and 69 deletions

View file

@ -3,7 +3,7 @@ use clap::{App, Arg};
pub fn parse_config() -> Config {
let args = App::new("Clipwatch")
.version(option_env!("CARGO_PKG_VERSION").unwrap())
.author("JMARyA <jmarya0@icloud.com>")
.author("JMARyA <jmarya@hydrar.de>")
.about("Keep an eye on the clipboard")
.arg(
Arg::with_name("ignore-first")
@ -16,9 +16,11 @@ pub fn parse_config() -> Config {
Arg::with_name("summary")
.short("s")
.long("summary")
.help("at termination of the programm a summary will be copied into the clipboard")
.help("at termination of the programm all captured content will be copied into the clipboard")
.takes_value(false),
)
.arg(Arg::with_name("quiet").short("q").long("quiet").help("Dont print to stdout").takes_value(false))
.arg(Arg::with_name("transform").short("t").long("transform").help("Apply a format string to each clipboard content. Any `{}` will be replaced by the content.").takes_value(true).required(false))
.get_matches();
return Config::new(args);
}
@ -27,6 +29,8 @@ pub fn parse_config() -> Config {
pub struct Config {
pub ignore_first: bool,
pub summary: bool,
pub quiet: bool,
pub transform: Option<String>,
}
impl Config {
@ -34,6 +38,8 @@ impl Config {
return Config {
ignore_first: args.is_present("ignore-first"),
summary: args.is_present("summary"),
quiet: args.is_present("quiet"),
transform: args.value_of("transform").map(|x| x.to_owned()),
};
}
}

View file

@ -1,12 +1,13 @@
mod config;
use clipboard_macos::*;
use std::sync::{Arc, Mutex};
use std::{process, thread, time};
use arboard::*;
fn main() {
let conf = config::parse_config();
let c = Clipboard::new().unwrap();
let mut c = Clipboard::new().unwrap();
let mut cstr = String::new();
let ten_millis = time::Duration::from_millis(10);
let stream: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![]));
@ -20,26 +21,35 @@ fn main() {
for i in 0..v.len() {
fstr.push_str(&*format!("{}\n", v[i]));
}
Clipboard::new().unwrap().write(fstr);
Clipboard::new().unwrap().set_text(fstr).unwrap();
}
process::exit(0);
})
.expect("Error setting Ctrl-C handler");
if conf.ignore_first {
cstr = c.read().unwrap();
cstr = c.get_text().unwrap();
}
loop {
let nstr = c.read();
let nstr = c.get_text();
if nstr.is_err() {
continue;
}
let nstr = nstr.unwrap();
if nstr != cstr {
stream.lock().unwrap().push(nstr.clone());
println!("{}", nstr);
let t_str = if let Some(transform) = conf.transform.as_ref() {
transform.replace("{}", &nstr)
} else {
nstr.clone()
};
cstr = nstr;
stream.lock().unwrap().push(t_str.clone());
if !conf.quiet {
println!("{}", t_str);
}
}
thread::sleep(ten_millis);
}