### Get Available Formats and Content Source: https://github.com/churchtao/clipboard-rs/blob/master/README.md Demonstrates how to initialize a clipboard context, check for available content formats (like RTF and HTML), and retrieve their content. Handles potential errors during retrieval. ```rust use clipboard_rs::{Clipboard, ClipboardContext, ContentFormat}; fn main() { let ctx = ClipboardContext::new().unwrap(); let types = ctx.available_formats().unwrap(); println!("{:?}", types); let has_rtf = ctx.has(ContentFormat::Rtf); println!("has_rtf={}", has_rtf); let rtf = ctx.get_rich_text().unwrap_or("".to_string()); println!("rtf={}", rtf); let has_html = ctx.has(ContentFormat::Html); println!("has_html={}", has_html); let html = ctx.get_html().unwrap_or("".to_string()); println!("html={}", html); let content = ctx.get_text().unwrap_or("".to_string()); println!("txt={}", content); } ``` -------------------------------- ### Setup Clipboard with Custom X11 Options Source: https://github.com/churchtao/clipboard-rs/blob/master/README.md Use `new_with_options` to configure X11 clipboard settings, such as disabling the read timeout by setting it to `None`. This function is conditional and only compiled on Unix-like systems. ```rust #[cfg(unix)] fn setup_clipboard() -> ClipboardContext { ClipboardContext::new_with_options(ClipboardContextX11Options { read_timeout: None }).unwrap() } ``` ```rust #[cfg(not(unix))] fn setup_clipboard(ctx: &mut ClipboardContext) -> ClipboardContext{ ClipboardContext::new().unwrap() } ``` -------------------------------- ### Set and Get Multiple Content Types Source: https://context7.com/churchtao/clipboard-rs/llms.txt The `set` and `get` methods allow simultaneous handling of multiple clipboard content types like Text, HTML, and RTF. This is useful for providing content in various pasteable formats. ```rust use clipboard_rs::{ common::ContentData, Clipboard, ClipboardContent, ClipboardContext, ContentFormat, }; fn main() { let ctx = ClipboardContext::new().unwrap(); // Set multiple content types at once let contents = vec![ ClipboardContent::Text("Hello, Rust!".to_string()), ClipboardContent::Html("Hello, Rust!".to_string()), ClipboardContent::Rtf(r"{\rtf1\ansi Hello, \b Rust\b0!}".to_string()), ]; ctx.set(contents).unwrap(); // Read multiple content types let formats_to_read = [ContentFormat::Text, ContentFormat::Html, ContentFormat::Rtf]; let read_contents = ctx.get(&formats_to_read).unwrap(); for content in read_contents { println!("Format: {:?}", content.get_format()); println!("Content: {}", content.as_str().unwrap()); println!("---"); } // Output: // Format: Text // Content: Hello, Rust! // --- // Format: Html // Content: Hello, Rust! // --- // Format: Rtf // Content: {\rtf1\ansi Hello, \b Rust\b0!} // --- } ``` -------------------------------- ### Check and List Available Clipboard Formats Source: https://context7.com/churchtao/clipboard-rs/llms.txt Use `has` to check if specific `ContentFormat`s are present in the clipboard, and `available_formats` to get a list of all supported formats. The `Image` format check is conditional on the `image` feature. ```rust use clipboard_rs::{Clipboard, ClipboardContext, ContentFormat}; fn main() { let ctx = ClipboardContext::new().unwrap(); // List all available formats let formats = ctx.available_formats().unwrap(); println!("All formats: {:?}", formats); // Check for specific content types if ctx.has(ContentFormat::Text) { println!("Clipboard has text"); } if ctx.has(ContentFormat::Html) { println!("Clipboard has HTML"); } if ctx.has(ContentFormat::Rtf) { println!("Clipboard has RTF"); } if ctx.has(ContentFormat::Files) { println!("Clipboard has files"); } #[cfg(feature = "image")] if ctx.has(ContentFormat::Image) { println!("Clipboard has image"); } } ``` -------------------------------- ### Multiple Content Types (get / set) Source: https://context7.com/churchtao/clipboard-rs/llms.txt Reads and writes multiple content types simultaneously. Useful for setting clipboard content that can be pasted in different formats. ```APIDOC ## POST /content ### Description Sets multiple content types to the clipboard simultaneously. ### Method POST ### Endpoint /content ### Request Body - **contents** (array[object]) - An array of objects, where each object represents a content type and its data. - **format** (string) - Required - The format of the content (e.g., "Text", "Html", "Rtf"). - **data** (string) - Required - The content data. ### Request Example { "contents": [ { "format": "Text", "data": "Hello, Rust!" }, { "format": "Html", "data": "Hello, Rust!" }, { "format": "Rtf", "data": "{\\rtf1\\ansi Hello, \\b Rust\\b0!}" } ] } ### Response #### Success Response (200) - **message** (string) - Confirmation message. #### Response Example { "message": "Multiple content types set to clipboard successfully." } ## GET /content ### Description Reads multiple content types from the clipboard. ### Method GET ### Endpoint /content ### Query Parameters - **formats** (array[string]) - Required - A comma-separated list of content formats to retrieve (e.g., "Text,Html,Rtf"). ### Response #### Success Response (200) - **contents** (array[object]) - An array of objects, where each object represents a content type and its data. - **format** (string) - The format of the content. - **data** (string) - The content data. #### Response Example { "contents": [ { "format": "Text", "data": "Hello, Rust!" }, { "format": "Html", "data": "Hello, Rust!" }, { "format": "Rtf", "data": "{\\rtf1\\ansi Hello, \\b Rust\\b0!}" } ] } ``` -------------------------------- ### Read and Write Image Data Source: https://context7.com/churchtao/clipboard-rs/llms.txt Interacts with image data in the clipboard, defaulting to PNG format. Requires the `image` feature. Provides methods to get image size, save to path, create thumbnails, and load from a file path. ```rust use clipboard_rs::{common::RustImage, Clipboard, ClipboardContext, RustImageData}; fn main() { let ctx = ClipboardContext::new().unwrap(); // Read image from clipboard and save to file match ctx.get_image() { Ok(img) => { // Get image dimensions let (width, height) = img.get_size(); println!("Image size: {}x{}", width, height); // Save original image img.save_to_path("/tmp/clipboard_image.png").unwrap(); // Create and save a thumbnail (preserves aspect ratio) let thumbnail = img.thumbnail(300, 300).unwrap(); thumbnail.save_to_path("/tmp/clipboard_thumbnail.png").unwrap(); println!("Images saved successfully"); } Err(e) => println!("No image in clipboard: {}", e), } // Write image to clipboard from file let image = RustImageData::from_path("/tmp/my_image.png").unwrap(); ctx.set_image(image).unwrap(); } ``` -------------------------------- ### Create Clipboard Context Source: https://context7.com/churchtao/clipboard-rs/llms.txt Initializes a new clipboard context. This is the primary entry point for all clipboard operations. Ensure error handling for context creation. ```rust use clipboard_rs::{Clipboard, ClipboardContext}; fn main() { // Create a new clipboard context let ctx = ClipboardContext::new().unwrap(); // List all available formats in the clipboard let formats = ctx.available_formats().unwrap(); println!("Available formats: {:?}", formats); // Output: Available formats: ["public.utf8-plain-text", "public.html", ...] } ``` -------------------------------- ### Write and Read Files to Clipboard Source: https://context7.com/churchtao/clipboard-rs/llms.txt Use `set_files` to write a vector of file URI strings to the clipboard and `get_files` to read them. Ensure the clipboard supports file content format before reading. ```rust use clipboard_rs::{Clipboard, ClipboardContext, ContentFormat}; fn main() { let ctx = ClipboardContext::new().unwrap(); // Write files to clipboard let files = vec![ "file:///home/user/document.pdf".to_string(), "file:///home/user/image.png".to_string(), ]; ctx.set_files(files).unwrap(); // Check and read files from clipboard if ctx.has(ContentFormat::Files) { let clipboard_files = ctx.get_files().unwrap(); for file in clipboard_files { println!("File: {}", file); } // Output: // File: file:///home/user/document.pdf // File: file:///home/user/image.png } } ``` -------------------------------- ### ClipboardContext::new Source: https://context7.com/churchtao/clipboard-rs/llms.txt Creates a new clipboard context, which is the primary entry point for all clipboard operations. ```APIDOC ## ClipboardContext::new ### Description Creates a new clipboard context for interacting with the system clipboard. This is the primary entry point for all clipboard operations. ### Method N/A (Constructor) ### Endpoint N/A ### Parameters None ### Request Example ```rust use clipboard_rs::{Clipboard, ClipboardContext}; fn main() { // Create a new clipboard context let ctx = ClipboardContext::new().unwrap(); // ... rest of the code } ``` ### Response #### Success Response (200) - **ClipboardContext** (object) - A new instance of the clipboard context. #### Response Example ```rust // Successful creation returns a ClipboardContext object let ctx = ClipboardContext::new().unwrap(); ``` ``` -------------------------------- ### Read and Save Images from Clipboard Source: https://github.com/churchtao/clipboard-rs/blob/master/README.md Shows how to retrieve an image from the clipboard, save it to a file, and create a thumbnail. Requires the `common::RustImage` type and handles potential errors during image processing. ```rust use clipboard_rs::{common::RustImage, Clipboard, ClipboardContext}; const TMP_PATH: &str = "/tmp/"; fn main() { let ctx = ClipboardContext::new().unwrap(); let types = ctx.available_formats().unwrap(); println!("{:?}", types); let img = ctx.get_image(); match img { Ok(img) => { img.save_to_path(format!("{}test.png", TMP_PATH).as_str()) .unwrap(); let resize_img = img.thumbnail(300, 300).unwrap(); resize_img .save_to_path(format!("{}test_thumbnail.png", TMP_PATH).as_str()) .unwrap(); } Err(err) => { println!("err={}", err); } } } ``` -------------------------------- ### Read and Write Raw Byte Buffers Source: https://context7.com/churchtao/clipboard-rs/llms.txt Utilize `get_buffer` and `set_buffer` for custom clipboard formats. Use `available_formats()` to discover format identifiers. Note that `get_buffer` returns raw bytes, which may need conversion (e.g., to String). ```rust use clipboard_rs::{Clipboard, ClipboardContext}; fn main() { let ctx = ClipboardContext::new().unwrap(); // List available formats to find the identifier you need let formats = ctx.available_formats().unwrap(); println!("Available formats: {:?}", formats); // Read raw buffer for a specific format (e.g., "public.html" on macOS) match ctx.get_buffer("public.html") { Ok(buffer) => { let content = String::from_utf8(buffer).unwrap(); println!("Raw HTML buffer: {}", content); } Err(e) => println!("Error: {}", e), } // Write custom format data let custom_data = b"my custom data".to_vec(); ctx.set_buffer("com.myapp.customformat", custom_data).unwrap(); } ``` -------------------------------- ### Read and Write HTML Content Source: https://context7.com/churchtao/clipboard-rs/llms.txt Manages HTML content in the clipboard. Use `set_html` to write and `get_html` to read. Check for HTML availability using `has(ContentFormat::Html)` before reading. ```rust use clipboard_rs::{Clipboard, ClipboardContext, ContentFormat}; fn main() { let ctx = ClipboardContext::new().unwrap(); // Write HTML to clipboard let html_content = "

Hello

This is bold text.

"; ctx.set_html(html_content.to_string()).unwrap(); // Check if clipboard has HTML content if ctx.has(ContentFormat::Html) { let html = ctx.get_html().unwrap(); println!("HTML content: {}", html); // Output: HTML content:

Hello

This is bold text.

} } ``` -------------------------------- ### Add clipboard-rs to Cargo.toml Source: https://github.com/churchtao/clipboard-rs/blob/master/README.md To use the clipboard-rs crate, add the following dependency to your Cargo.toml file. ```toml [dependencies] clipboard-rs = "0.3.3" ``` -------------------------------- ### get_html / set_html Source: https://context7.com/churchtao/clipboard-rs/llms.txt APIs for reading and writing HTML formatted content to and from the system clipboard. ```APIDOC ## GET /html and POST /html ### Description Reads and writes HTML formatted content from/to the clipboard, useful for rich content transfer between applications. ### Method GET (for `get_html`), POST (for `set_html`) ### Endpoint N/A (Methods on ClipboardContext) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body (for `set_html`) - **html** (string) - Required - The HTML content to set on the clipboard. ### Request Example ```rust use clipboard_rs::{Clipboard, ClipboardContext, ContentFormat}; fn main() { let ctx = ClipboardContext::new().unwrap(); // Write HTML to clipboard let html_content = "

Hello

This is bold text.

"; ctx.set_html(html_content.to_string()).unwrap(); // Check if clipboard has HTML content if ctx.has(ContentFormat::Html) { let html = ctx.get_html().unwrap(); println!("HTML content: {}", html); } } ``` ### Response #### Success Response (200) - **get_html**: (string) - The HTML content currently in the clipboard. - **set_html**: (void) - Indicates successful write operation. #### Response Example ```rust // For get_html: let html = ctx.get_html().unwrap(); // "

Hello

This is bold text.

" // For set_html: ctx.set_html(html_content.to_string()).unwrap(); // Success ``` ``` -------------------------------- ### get_text / set_text Source: https://context7.com/churchtao/clipboard-rs/llms.txt APIs for reading and writing plain text content to and from the system clipboard. ```APIDOC ## GET /text and POST /text ### Description Reads and writes plain text content from/to the clipboard. These are the most commonly used clipboard operations. ### Method GET (for `get_text`), POST (for `set_text`) ### Endpoint N/A (Methods on ClipboardContext) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body (for `set_text`) - **text** (string) - Required - The plain text content to set on the clipboard. ### Request Example ```rust use clipboard_rs::{Clipboard, ClipboardContext}; fn main() { let ctx = ClipboardContext::new().unwrap(); // Write text to clipboard ctx.set_text("Hello, clipboard-rs!".to_string()).unwrap(); // Read text from clipboard let text = ctx.get_text().unwrap_or_default(); println!("Clipboard text: {}", text); } ``` ### Response #### Success Response (200) - **get_text**: (string) - The plain text content currently in the clipboard. - **set_text**: (void) - Indicates successful write operation. #### Response Example ```rust // For get_text: let text = ctx.get_text().unwrap_or_default(); // "Hello, clipboard-rs!" // For set_text: ctx.set_text("Hello, clipboard-rs!".to_string()).unwrap(); // Success ``` ``` -------------------------------- ### File Path Operations (get_files / set_files) Source: https://context7.com/churchtao/clipboard-rs/llms.txt Reads and writes file paths from/to the clipboard. Files are represented as file URI strings. ```APIDOC ## GET /files ### Description Reads file paths from the clipboard. ### Method GET ### Endpoint /files ### Response #### Success Response (200) - **files** (array[string]) - An array of file URI strings. #### Response Example { "files": [ "file:///home/user/document.pdf", "file:///home/user/image.png" ] } ## POST /files ### Description Writes file paths to the clipboard. ### Method POST ### Endpoint /files ### Request Body - **files** (array[string]) - An array of file URI strings to be written to the clipboard. ### Request Example { "files": [ "file:///home/user/document.pdf", "file:///home/user/image.png" ] } ### Response #### Success Response (200) - **message** (string) - Confirmation message. #### Response Example { "message": "Files written to clipboard successfully." } ``` -------------------------------- ### Read Clipboard Content by Custom Type Source: https://github.com/churchtao/clipboard-rs/blob/master/README.md Demonstrates how to retrieve clipboard content using a specific format identifier, such as 'public.html'. The raw buffer is converted to a UTF-8 string. Assumes the content is valid UTF-8. ```rust use clipboard_rs::{Clipboard, ClipboardContext}; fn main() { let ctx = ClipboardContext::new().unwrap(); let types = ctx.available_formats().unwrap(); println!("{:?}", types); let buffer = ctx.get_buffer("public.html").unwrap(); let string = String::from_utf8(buffer).unwrap(); println!("{}", string); } ``` -------------------------------- ### Format Checking and Listing (has / available_formats) Source: https://context7.com/churchtao/clipboard-rs/llms.txt Checks for specific content types and lists all available formats in the clipboard. ```APIDOC ## GET /formats ### Description Retrieves a list of all available content formats currently in the clipboard. ### Method GET ### Endpoint /formats ### Response #### Success Response (200) - **formats** (array[string]) - An array of strings representing the available clipboard format identifiers. #### Response Example { "formats": ["Text", "Html", "Rtf", "Files"] } ## GET /has/{format} ### Description Checks if the clipboard contains content of a specific format. ### Method GET ### Endpoint /has/{format} ### Parameters #### Path Parameters - **format** (string) - Required - The content format to check for (e.g., "Text", "Html", "Files"). ### Response #### Success Response (200) - **has_format** (boolean) - True if the clipboard contains the specified format, false otherwise. #### Response Example { "has_format": true } ``` -------------------------------- ### Listen for Clipboard Changes Source: https://github.com/churchtao/clipboard-rs/blob/master/README.md Sets up a clipboard watcher to receive notifications when the clipboard content changes. Implements the `ClipboardHandler` trait to define the callback behavior. The watcher runs in a separate thread and can be shut down. ```rust use clipboard_rs::{ Clipboard, ClipboardContext, ClipboardHandler, ClipboardWatcher, ClipboardWatcherContext, }; use std::{thread, time::Duration}; struct Manager { ctx: ClipboardContext, } impl Manager { pub fn new() -> Self { let ctx = ClipboardContext::new().unwrap(); Manager { ctx } } } impl ClipboardHandler for Manager { fn on_clipboard_change(&mut self) { println!( "on_clipboard_change, txt = {}", self.ctx.get_text().unwrap() ); } } fn main() { let manager = Manager::new(); let mut watcher = ClipboardWatcherContext::new().unwrap(); let watcher_shutdown = watcher.add_handler(manager).get_shutdown_channel(); thread::spawn(move || { thread::sleep(Duration::from_secs(5)); println!("stop watch!"); watcher_shutdown.stop(); }); println!("start watch!"); watcher.start_watch(); } ``` -------------------------------- ### Read and Write Rich Text (RTF) Source: https://context7.com/churchtao/clipboard-rs/llms.txt Handles RTF content for formatted text. Use `set_rich_text` to write and `get_rich_text` to read. Verify RTF presence with `has(ContentFormat::Rtf)`. ```rust use clipboard_rs::{Clipboard, ClipboardContext, ContentFormat}; fn main() { let ctx = ClipboardContext::new().unwrap(); // Write RTF to clipboard let rtf_content = r"{\rtf1\ansi Hello \b World\b0!}"; ctx.set_rich_text(rtf_content.to_string()).unwrap(); // Check and read RTF content if ctx.has(ContentFormat::Rtf) { let rtf = ctx.get_rich_text().unwrap(); println!("RTF content: {}", rtf); } } ``` -------------------------------- ### Configure Linux X11 Clipboard Read Timeout Source: https://context7.com/churchtao/clipboard-rs/llms.txt Customize X11-specific options for clipboard operations, such as setting a read timeout. This is only applicable on Linux systems using X11. ```rust #[cfg(target_os = "linux")] use clipboard_rs::ClipboardContextX11Options; use clipboard_rs::{Clipboard, ClipboardContext}; #[cfg(target_os = "linux")] fn create_clipboard() -> ClipboardContext { // Disable read timeout (wait indefinitely) ClipboardContext::new_with_options(ClipboardContextX11Options { read_timeout: None }).unwrap() } ``` ```rust #[cfg(target_os = "linux")] fn create_clipboard_with_timeout() -> ClipboardContext { use std::time::Duration; // Set custom read timeout of 2 seconds ClipboardContext::new_with_options(ClipboardContextX11Options { read_timeout: Some(Duration::from_secs(2)) }).unwrap() } ``` ```rust #[cfg(not(target_os = "linux"))] fn create_clipboard() -> ClipboardContext { ClipboardContext::new().unwrap() } fn main() { let ctx = create_clipboard(); let text = ctx.get_text().unwrap_or_default(); println!("Clipboard: {}", text); } ``` -------------------------------- ### Read and Write Plain Text Source: https://context7.com/churchtao/clipboard-rs/llms.txt Handles plain text operations. Use `set_text` to write and `get_text` to read. `get_text` returns an empty string if the clipboard is empty or contains non-text data. ```rust use clipboard_rs::{Clipboard, ClipboardContext}; fn main() { let ctx = ClipboardContext::new().unwrap(); // Write text to clipboard ctx.set_text("Hello, clipboard-rs!".to_string()).unwrap(); // Read text from clipboard let text = ctx.get_text().unwrap_or_default(); println!("Clipboard text: {}", text); // Output: Clipboard text: Hello, clipboard-rs! } ``` -------------------------------- ### get_image / set_image Source: https://context7.com/churchtao/clipboard-rs/llms.txt APIs for reading and writing image data to and from the system clipboard. Requires the `image` feature. ```APIDOC ## GET /image and POST /image ### Description Reads and writes image data from/to the clipboard. Requires the `image` feature (enabled by default). Images are handled in PNG format internally. ### Method GET (for `get_image`), POST (for `set_image`) ### Endpoint N/A (Methods on ClipboardContext) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body (for `set_image`) - **image** (RustImageData) - Required - The image data to set on the clipboard. ### Request Example ```rust use clipboard_rs::{common::RustImage, Clipboard, ClipboardContext, RustImageData}; fn main() { let ctx = ClipboardContext::new().unwrap(); // Read image from clipboard and save to file match ctx.get_image() { Ok(img) => { let (width, height) = img.get_size(); println!("Image size: {}x{}", width, height); img.save_to_path("/tmp/clipboard_image.png").unwrap(); let thumbnail = img.thumbnail(300, 300).unwrap(); thumbnail.save_to_path("/tmp/clipboard_thumbnail.png").unwrap(); println!("Images saved successfully"); } Err(e) => println!("No image in clipboard: {}", e), } // Write image to clipboard from file let image = RustImageData::from_path("/tmp/my_image.png").unwrap(); ctx.set_image(image).unwrap(); } ``` ### Response #### Success Response (200) - **get_image**: (RustImage) - The image data currently in the clipboard. - **set_image**: (void) - Indicates successful write operation. #### Response Example ```rust // For get_image: match ctx.get_image() { Ok(img) => { println!("Image dimensions: {}x{}", img.get_size().0, img.get_size().1); } Err(e) => println!("Error reading image: {}", e), } // For set_image: let image = RustImageData::from_path("/tmp/my_image.png").unwrap(); ctx.set_image(image).unwrap(); // Success ``` ``` -------------------------------- ### Raw Buffer Operations (get_buffer / set_buffer) Source: https://context7.com/churchtao/clipboard-rs/llms.txt Reads and writes raw byte data for custom clipboard formats. Use `available_formats()` to discover format identifiers. ```APIDOC ## GET /buffer/{format} ### Description Reads raw byte data for a specific clipboard format. ### Method GET ### Endpoint /buffer/{format} ### Parameters #### Path Parameters - **format** (string) - Required - The identifier of the clipboard format (e.g., "public.html"). ### Response #### Success Response (200) - **buffer** (bytes) - The raw byte data for the specified format. #### Response Example { "buffer": "PGh0bWw+PC9ib2R5PkhlbGxvLCBSdXN0ITwvYm9keT4=" } ## POST /buffer/{format} ### Description Writes custom format data to the clipboard. ### Method POST ### Endpoint /buffer/{format} ### Parameters #### Path Parameters - **format** (string) - Required - The identifier for the custom clipboard format (e.g., "com.myapp.customformat"). ### Request Body - **data** (bytes) - The raw byte data to be written to the clipboard. ### Request Example { "data": "bXkgY3VzdG9tIGRhdGE=" } ### Response #### Success Response (200) - **message** (string) - Confirmation message. #### Response Example { "message": "Custom buffer data written to clipboard successfully." } ``` -------------------------------- ### Monitor Clipboard Changes in Real-time Source: https://context7.com/churchtao/clipboard-rs/llms.txt Implement the ClipboardHandler trait to receive notifications on clipboard changes. The watcher runs in a blocking loop and can be stopped via a shutdown channel. Requires a ClipboardContext to interact with the clipboard. ```rust use clipboard_rs::{ Clipboard, ClipboardContext, ClipboardHandler, ClipboardWatcher, ClipboardWatcherContext, }; use std::{thread, time::Duration}; struct ClipboardManager { ctx: ClipboardContext, } impl ClipboardManager { pub fn new() -> Self { let ctx = ClipboardContext::new().unwrap(); ClipboardManager { ctx } } } impl ClipboardHandler for ClipboardManager { fn on_clipboard_change(&mut self) { // Called whenever clipboard content changes match self.ctx.get_text() { Ok(text) => println!("Clipboard changed: {}", text), Err(_) => println!("Clipboard changed (non-text content)"), } } } fn main() { let manager = ClipboardManager::new(); let mut watcher = ClipboardWatcherContext::new().unwrap(); // Add handler and get shutdown channel let shutdown = watcher.add_handler(manager).get_shutdown_channel(); // Stop watching after 10 seconds (in a separate thread) thread::spawn(move || { thread::sleep(Duration::from_secs(10)); println!("Stopping clipboard watcher..."); shutdown.stop(); }); println!("Watching clipboard for 10 seconds..."); watcher.start_watch(); // Blocking call println!("Watcher stopped"); } ``` -------------------------------- ### Manipulate Images with RustImage Source: https://context7.com/churchtao/clipboard-rs/llms.txt Load, resize, create thumbnails, and convert image formats using the RustImage struct. This functionality requires the 'image' feature to be enabled. ```rust use clipboard_rs::{common::RustImage, RustImageData}; fn main() { // Load image from file let img = RustImageData::from_path("/tmp/input.png").unwrap(); // Get image dimensions let (width, height) = img.get_size(); println!("Original size: {}x{}", width, height); // Create thumbnail (maintains aspect ratio, fits within bounds) let thumbnail = img.thumbnail(200, 200).unwrap(); thumbnail.save_to_path("/tmp/thumbnail.png").unwrap(); // Resize to exact dimensions (may distort aspect ratio) use clipboard_rs::FilterType; let resized = img.resize(400, 300, FilterType::Lanczos3).unwrap(); resized.save_to_path("/tmp/resized.png").unwrap(); // Convert to different formats let png_buffer = img.to_png().unwrap(); png_buffer.save_to_path("/tmp/output.png").unwrap(); let jpeg_buffer = img.to_jpeg().unwrap(); jpeg_buffer.save_to_path("/tmp/output.jpg").unwrap(); // Load image from bytes let bytes = std::fs::read("/tmp/input.png").unwrap(); let img_from_bytes = RustImageData::from_bytes(&bytes).unwrap(); println!("Loaded from bytes: {:?}", img_from_bytes.get_size()); } ``` -------------------------------- ### get_rich_text / set_rich_text Source: https://context7.com/churchtao/clipboard-rs/llms.txt APIs for reading and writing RTF (Rich Text Format) content to and from the system clipboard. ```APIDOC ## GET /rich_text and POST /rich_text ### Description Reads and writes RTF (Rich Text Format) content from/to the clipboard for formatted text exchange. ### Method GET (for `get_rich_text`), POST (for `set_rich_text`) ### Endpoint N/A (Methods on ClipboardContext) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body (for `set_rich_text`) - **rtf** (string) - Required - The RTF content to set on the clipboard. ### Request Example ```rust use clipboard_rs::{Clipboard, ClipboardContext, ContentFormat}; fn main() { let ctx = ClipboardContext::new().unwrap(); // Write RTF to clipboard let rtf_content = r"{\rtf1\ansi Hello \b World\b0!}"; ctx.set_rich_text(rtf_content.to_string()).unwrap(); // Check and read RTF content if ctx.has(ContentFormat::Rtf) { let rtf = ctx.get_rich_text().unwrap(); println!("RTF content: {}", rtf); } } ``` ### Response #### Success Response (200) - **get_rich_text**: (string) - The RTF content currently in the clipboard. - **set_rich_text**: (void) - Indicates successful write operation. #### Response Example ```rust // For get_rich_text: let rtf = ctx.get_rich_text().unwrap(); // "{\rtf1\ansi Hello \b World\b0!}" // For set_rich_text: ctx.set_rich_text(rtf_content.to_string()).unwrap(); // Success ``` ``` -------------------------------- ### Clear Clipboard Content Source: https://context7.com/churchtao/clipboard-rs/llms.txt The `clear` method removes all content from the system clipboard. After clearing, `available_formats` will typically return an empty list. ```rust use clipboard_rs::{Clipboard, ClipboardContext}; fn main() { let ctx = ClipboardContext::new().unwrap(); // Set some content ctx.set_text("Some text".to_string()).unwrap(); println!("Before clear: {}", ctx.get_text().unwrap()); // Clear the clipboard ctx.clear().unwrap(); // Clipboard is now empty let formats = ctx.available_formats().unwrap(); println!("Formats after clear: {:?}", formats); // Output: Formats after clear: [] } ``` -------------------------------- ### Clear Clipboard (clear) Source: https://context7.com/churchtao/clipboard-rs/llms.txt Clears all content from the system clipboard. ```APIDOC ## DELETE /clear ### Description Clears all content from the system clipboard. ### Method DELETE ### Endpoint /clear ### Response #### Success Response (200) - **message** (string) - Confirmation message indicating the clipboard has been cleared. #### Response Example { "message": "Clipboard cleared successfully." } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.