From 4127796a92e1d9a037a344094a4a5e43d6ceaac4 Mon Sep 17 00:00:00 2001 From: ngirard Date: Thu, 25 Jul 2019 16:50:12 +0200 Subject: [PATCH] Illustrate how to monitor the primary selection. (#15) Inspired by https://github.com/farseerfc/ydcv-rs/blob/master/src/main.rs#L143 Fixes #14 --- examples/monitor_primary_selection.rs | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 examples/monitor_primary_selection.rs diff --git a/examples/monitor_primary_selection.rs b/examples/monitor_primary_selection.rs new file mode 100644 index 0000000..e2222a9 --- /dev/null +++ b/examples/monitor_primary_selection.rs @@ -0,0 +1,29 @@ +extern crate x11_clipboard; + +use x11_clipboard::Clipboard; + + +fn main() { + let clipboard = Clipboard::new().unwrap(); + let mut last = String::new(); + + println!("Waiting for selection..."); + + loop { + if let Ok(curr) = clipboard.load_wait( + clipboard.getter.atoms.primary, + clipboard.getter.atoms.utf8_string, + clipboard.getter.atoms.property + ) { + let curr = String::from_utf8_lossy(&curr); + let curr = curr + .trim_matches('\u{0}') + .trim(); + if !curr.is_empty() && last != curr { + last = curr.to_owned(); + println!("Contents of primary selection: {}", last); + println!("Waiting for selection..."); + } + } + } +}