### Get::text basic example Source: https://github.com/1password/arboard Shows how to create a Clipboard, get its text, and print it. Uses the ? operator to propagate errors. ```rust use arboard::Clipboard; fn main() -> Result<(), arboard::Error> { let mut clipboard = Clipboard::new()?; let text = clipboard.get().text()?; println!("{text}"); Ok(()) } ``` -------------------------------- ### Get example Source: https://github.com/1password/arboard Retrieves text and file list from the clipboard. ```rust use arboard::Clipboard; let mut clipboard = Clipboard::new().unwrap(); let text = clipboard.get().text().unwrap(); let paths = clipboard.get().file_list().unwrap(); ``` -------------------------------- ### Get::html example Source: https://github.com/1password/arboard Example of retrieving HTML from the clipboard and handling errors, including the ContentNotAvailable case. ```rust use arboard::Clipboard; let mut ctx = Clipboard::new().unwrap(); match ctx.get().html() { Ok(html) => println!("HTML: {html}"), Err(arboard::Error::ContentNotAvailable) => println!("no HTML on the clipboard"), Err(e) => eprintln!("{e}"), } ``` -------------------------------- ### Example: using Clipboard.set().file_list() Source: https://github.com/1password/arboard Demonstrates setting and getting a file list on the clipboard. The example creates a clipboard, sets the file list to two paths, retrieves it, and asserts equality. ```rust use arboard::Clipboard; fn main() -> Result<(), arboard::Error> { let mut clipboard = Clipboard::new()?; let paths = ["/tmp/a.txt", "/tmp/b.txt"]; clipboard.set().file_list(&paths)?; let roundtrip = clipboard.get().file_list()?; assert_eq!(roundtrip, paths); Ok(()) } ``` -------------------------------- ### Get::image example Source: https://github.com/1password/arboard Example of retrieving an image from the clipboard, asserting the byte length matches RGBA8 dimensions, and printing the size. ```rust use arboard::Clipboard; fn main() -> Result<(), arboard::Error> { let mut clipboard = Clipboard::new()?; let img = clipboard.get().image()?; assert_eq!(img.bytes.len(), img.width * img.height * 4); println!("got a {w}x{h} image", w = img.width, h = img.height); Ok(()) } ``` -------------------------------- ### Example: Retrieve file list from clipboard Source: https://github.com/1password/arboard Example usage of `Get::file_list` to retrieve file paths from the clipboard. Handles the `Error::ContentNotAvailable` case by printing a message. ```rust use arboard::Clipboard; let mut clipboard = Clipboard::new().unwrap(); match clipboard.get().file_list() { Ok(paths) => { for p in &paths { println!("{}", p.display()); } } Err(arboard::Error::ContentNotAvailable) => println!("no files on clipboard"), Err(e) => eprintln!("{e}"), } ``` -------------------------------- ### Clipboard::new example Source: https://github.com/1password/arboard Example usage: creates a Clipboard and retrieves text. Returns Result<(), arboard::Error>. ```rust use arboard::Clipboard; fn main() -> Result<(), arboard::Error> { let mut clipboard = Clipboard::new()?; println!("Clipboard text was: {:?}", clipboard.get_text()); Ok(()) } ``` -------------------------------- ### Get::text error handling example Source: https://github.com/1password/arboard Demonstrates matching on the error variants, specifically checking for ContentNotAvailable when the clipboard is empty or not text. ```rust use arboard::Clipboard; let mut clipboard = Clipboard::new().unwrap(); match clipboard.get().text() { Ok(text) => println!("clipboard contains: {text}"), Err(arboard::Error::ContentNotAvailable) => println!("clipboard is empty or not text"), Err(e) => eprintln!("failed: {e}"), } ``` -------------------------------- ### Set example Source: https://github.com/1password/arboard Sets text in the clipboard. ```rust use arboard::Clipboard; let mut clipboard = Clipboard::new().unwrap(); clipboard.set().text("insert in clipboard").unwrap(); ``` -------------------------------- ### get() method signature Source: https://github.com/1password/arboard Begins a 'get' operation. Returns a `#[must_use]` `Get` builder; choose its terminal method (`text`, `html`, `image`, `file_list`, possibly with `GetExtLinux::clipboard` on Linux). ```rust pub fn get(&mut self) -> Get<'_> ``` -------------------------------- ### Clipboard::get_text example Source: https://github.com/1password/arboard Example usage of get_text handling errors. ```rust use arboard::Clipboard; let mut clipboard = Clipboard::new().unwrap(); match clipboard.get_text() { Ok(text) => println!("got: {text}"), Err(arboard::Error::ContentNotAvailable) => println!("clipboard has no text"), Err(e) => eprintln!("other error: {e}"), } ``` -------------------------------- ### Usage example for to_owned_img Source: https://github.com/1password/arboard Example of using to_owned_img to get an owned ImageData for later use. ```rust use arboard::{Clipboard, ImageData}; let mut clipboard = Clipboard::new().unwrap(); let img = clipboard.get_image().unwrap(); // ImageData<'static> already let owned: ImageData<'static> = img.to_owned_img(); // Pass `owned` to another thread / store it for later use. ``` -------------------------------- ### Clipboard::set_text example Source: https://github.com/1password/arboard Example usage of set_text with &str and String. ```rust use arboard::Clipboard; fn main() -> Result<(), arboard::Error> { let mut clipboard = Clipboard::new()?; clipboard.set_text("Hello, world!")?; // &str clipboard.set_text(String::from("owned string"))?; // String Ok(()) } ``` -------------------------------- ### Basic clipboard usage with arboard Source: https://github.com/1password/arboard/blob/master/README.md Shows how to create a Clipboard instance, get and set text. Uses unwrap() on operations, which will panic if the clipboard is unavailable. Outputs the initial clipboard text and the newly set string. ```rust use arboard::Clipboard; fn main() { let mut clipboard = Clipboard::new().unwrap(); println!("Clipboard text was: {}", clipboard.get_text().unwrap()); let the_string = "Hello, world!"; clipboard.set_text(the_string).unwrap(); println!("But now the clipboard text should be: \"{}\"", the_string); } ``` -------------------------------- ### GetExtLinux usage example Source: https://github.com/1password/arboard Shows how to use GetExtLinux to read from the Primary Linux clipboard selection. Requires the arboard crate. ```rust use arboard::{Clipboard, GetExtLinux, LinuxClipboardKind}; let mut clipboard = Clipboard::new().unwrap(); let primary = clipboard.get().clipboard(LinuxClipboardKind::Primary).text().unwrap(); ``` -------------------------------- ### Usage example for into_owned_bytes Source: https://github.com/1password/arboard Example of using into_owned_bytes to get owned bytes from an image retrieved from the clipboard. ```rust use arboard::Clipboard; let mut clipboard = Clipboard::new().unwrap(); let img = clipboard.get_image().unwrap(); // Other code can now keep the pixels without the clipboard in scope. let owned: std::borrow::Cow<'static, [u8]> = img.into_owned_bytes(); ``` -------------------------------- ### Clipboard::get_image example Source: https://github.com/1password/arboard Example of using get_image to read an image from the clipboard, handling success and error cases such as no image available. ```rust use arboard::Clipboard; let mut ctx = Clipboard::new().unwrap(); match ctx.get_image() { Ok(img) => println!("{}x{} image, {} bytes", img.width, img.height, img.bytes.len()), Err(arboard::Error::ContentNotAvailable) => println!("no image on clipboard"), Err(e) => eprintln!("failed to read image: {e}"), } ``` -------------------------------- ### Clear clipboard example (Rust) Source: https://github.com/1password/arboard Example showing how to clear the clipboard using clear_with().default(). It is safe to clear when already empty. ```rust use arboard::Clipboard; fn main() -> Result<(), arboard::Error> { let mut clipboard = Clipboard::new()?; clipboard.set_text("temporary")?; clipboard.clear_with().default()?; // It is OK to clear when already empty. clipboard.clear_with().default()?; Ok(()) } ``` -------------------------------- ### Complete builder flow (Linux, wait-then-exit) Source: https://github.com/1password/arboard Shows a full example of using the Set builder on Linux: set clipboard text and then sleep briefly. ```rust use arboard::Clipboard; use std::{thread, time::Duration}; fn main() -> Result<(), arboard::Error> { let mut clipboard = Clipboard::new()?; // Block this thread until another app has requested AND received the data. clipboard.set().text("Hello, world!")?; // simple case thread::sleep(Duration::from_millis(50)); Ok(()) } ``` -------------------------------- ### Example: wait with a deadline Source: https://github.com/1password/arboard Sets a deadline of 30 seconds from now. Block until that deadline or until the clipboard contents are replaced, whichever first. Overrides any previously set wait state. ```rust use arboard::{Clipboard, SetExtLinux}; use std::time::{Duration, Instant}; let mut clipboard = Clipboard::new().unwrap(); clipboard .set() .wait_until(Instant::now() + Duration::from_secs(30)) .text("shared value") .unwrap(); ``` -------------------------------- ### Example usage of Clipboard::set_html Source: https://github.com/1password/arboard Creates a clipboard context, sets HTML with alternative text, and sleeps 5 seconds to keep ownership alive on Linux while other apps paste. Requires arboard crate. ```rust use arboard::Clipboard; let mut ctx = Clipboard::new().unwrap(); let html = "