stash: make watch loop async

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I6a6a69641a4f39f0368de1bcf5780afbe8e5c0b1
This commit is contained in:
raf 2025-08-12 19:40:47 +03:00
commit 6c5408abd1
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF

View file

@ -15,6 +15,7 @@ mod import;
use crate::db::ClipboardDb;
use smol::Timer;
use std::io::Read;
use std::time::Duration;
use wl_clipboard_rs::paste::{ClipboardType, Seat, get_contents};
@ -97,8 +98,8 @@ fn report_error<T>(result: Result<T, impl std::fmt::Display>, context: &str) ->
}
/// Watch clipboard and store changes
fn run_daemon(db: &db::SqliteClipboardDb, max_dedupe_search: u64, max_items: u64) {
log::info!("Starting clipboard watch daemon (Wayland)");
async fn run_daemon(db: &db::SqliteClipboardDb, max_dedupe_search: u64, max_items: u64) {
log::info!("Starting clipboard watch daemon");
let mut last_contents: Option<Vec<u8>> = None;
@ -112,7 +113,7 @@ fn run_daemon(db: &db::SqliteClipboardDb, max_dedupe_search: u64, max_items: u64
let mut buf = Vec::new();
if let Err(e) = reader.read_to_end(&mut buf) {
log::error!("Failed to read clipboard contents: {e}");
std::thread::sleep(Duration::from_millis(500));
Timer::after(Duration::from_millis(500)).await;
continue;
}
// Only store if changed and not empty
@ -131,14 +132,19 @@ fn run_daemon(db: &db::SqliteClipboardDb, max_dedupe_search: u64, max_items: u64
}
}
Err(e) => {
// Only log actual errors, not empty clipboard
let error_msg = e.to_string();
if !error_msg.contains("empty") {
log::error!("Failed to get clipboard contents: {e}");
}
}
std::thread::sleep(Duration::from_millis(500));
}
Timer::after(Duration::from_millis(500)).await;
}
}
fn main() {
smol::block_on(async {
let cli = Cli::parse();
env_logger::Builder::new()
.filter_level(cli.verbosity.into())
@ -234,7 +240,7 @@ fn main() {
}
}
Some(Command::Watch) => {
run_daemon(&db, cli.max_dedupe_search, cli.max_items);
run_daemon(&db, cli.max_dedupe_search, cli.max_items).await;
}
None => {
if let Err(e) = Cli::command().print_help() {
@ -243,4 +249,5 @@ fn main() {
println!();
}
}
});
}