new features

This commit is contained in:
JMARyA 2021-05-24 12:21:57 +02:00
parent 956746d3e3
commit 6f70194871
4 changed files with 203 additions and 2 deletions

39
src/config.rs Normal file
View file

@ -0,0 +1,39 @@
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>")
.about("Keep an eye on the clipboard")
.arg(
Arg::with_name("ignore-first")
.short("i")
.long("ignore-first")
.help("ignores the value stored in the clipboard at the start of the programm")
.takes_value(false),
)
.arg(
Arg::with_name("summary")
.short("s")
.long("summary")
.help("at termination of the programm a summary will be copied into the clipboard")
.takes_value(false),
)
.get_matches();
return Config::new(args);
}
#[derive(Debug)]
pub struct Config {
pub ignore_first: bool,
pub summary: bool,
}
impl Config {
fn new(args: clap::ArgMatches) -> Config {
return Config {
ignore_first: args.is_present("ignore-first"),
summary: args.is_present("summary"),
};
}
}

View file

@ -1,10 +1,34 @@
mod config;
use clipboard_macos::*;
use std::{thread, time};
use std::sync::{Arc, Mutex};
use std::{process, thread, time};
fn main() {
let conf = config::parse_config();
let 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![]));
let cp_stream = stream.clone();
let summary = conf.summary.clone();
ctrlc::set_handler(move || {
if summary {
let mut fstr = String::new();
let v = cp_stream.lock().unwrap();
for i in 0..v.len() {
fstr.push_str(&*format!("{}\n", v[i]));
}
Clipboard::new().unwrap().write(fstr);
}
process::exit(0);
})
.expect("Error setting Ctrl-C handler");
if conf.ignore_first {
cstr = c.read().unwrap();
}
loop {
let nstr = c.read();
@ -13,6 +37,7 @@ fn main() {
}
let nstr = nstr.unwrap();
if nstr != cstr {
stream.lock().unwrap().push(nstr.clone());
println!("{}", nstr);
cstr = nstr;
}