Make polling intervals consistent

This commit is contained in:
Agatha Lovelace 2024-01-08 20:29:03 +01:00
parent 57f7985955
commit bd4850969b
Signed by: sorceress
GPG Key ID: 01D0B3AB10CED4F8
1 changed files with 6 additions and 7 deletions

View File

@ -1,7 +1,7 @@
use arboard::Clipboard; use arboard::Clipboard;
use memoize::memoize; use memoize::memoize;
use miette::{miette, IntoDiagnostic, Result}; use miette::{miette, IntoDiagnostic, Result};
use std::{env, fs}; use std::{env, fs, time::Duration};
use url::Url; use url::Url;
use wildmatch::WildMatch; use wildmatch::WildMatch;
@ -15,6 +15,9 @@ struct Category {
params: Vec<String>, params: Vec<String>,
} }
/// How often should clipboard be checked for changes (0 will result in high CPU usage)
const ITERATION_DELAY: Duration = Duration::from_millis(250);
fn main() -> Result<()> { fn main() -> Result<()> {
// Get filter file path // Get filter file path
let filters = env::args() let filters = env::args()
@ -44,10 +47,10 @@ fn main() -> Result<()> {
.map_err(|e| miette!(format!("Could not initialize clipboard context: {e}")))?; .map_err(|e| miette!(format!("Could not initialize clipboard context: {e}")))?;
let mut last_contents = clipboard.get_text().unwrap_or_else(|_| String::new()); let mut last_contents = clipboard.get_text().unwrap_or_else(|_| String::new());
loop { loop {
std::thread::sleep(ITERATION_DELAY);
if let Ok(contents) = clipboard.get_text() { if let Ok(contents) = clipboard.get_text() {
// Empty clipboard (Linux) // Empty clipboard (Linux)
if contents.is_empty() { if contents.is_empty() {
std::thread::sleep(std::time::Duration::from_millis(250));
continue; continue;
}; };
@ -62,11 +65,7 @@ fn main() -> Result<()> {
last_contents = url; last_contents = url;
}; };
}; };
} else { }
// Empty clipboard (Mac, Windows)
std::thread::sleep(std::time::Duration::from_millis(250));
continue;
};
} }
} }