copypasta

repository·master·Indexed 43 days ago

https://github.com/alacritty/copypasta

A cross-platform Rust library for getting and setting the contents of the OS-level clipboard. A fork of rust-clipboard, version 0.10.2 adds support for the Wayland clipboard alongside Windows, OSX, and X11. It provides a ClipboardProvider trait and a ClipboardContext type alias to abstract platform-specific backends.

Tokens
1.3K
Snippets
5
Records
8
Agent score
53%

What's inside copypasta

  1. Understand ClipboardContext implementation

    master

    The ClipboardContext type is a platform-specific type alias. Depending on the target OS, it resolves to one of the following types, all of which implement ClipboardProvider:

    • WindowsClipboardContext
    • OSXClipboardContext
    • X11ClipboardContext
    • NopClipboardContext (No-op)

    This abstraction allows you to write cross-platform clipboard code without manually selecting the backend.

  2. Use copypasta to manage clipboard contents

    master

    To use the library, create a ClipboardContext using ClipboardContext::new(). This context implements the ClipboardProvider trait, allowing you to call set_contents to write to the clipboard and get_contents to read from it. Note that ClipboardContext is a type alias that automatically selects the appropriate backend (Windows, OSX, X11, or Wayland) based on your operating system.

    extern crate copypasta;
    
    use copypasta::{ClipboardContext, ClipboardProvider};
    
    fn main() {
        let mut ctx = ClipboardContext::new().unwrap();
    
        let msg = "Hello, world!";
        ctx.set_contents(msg.to_owned()).unwrap();
    
        let content = ctx.get_contents().unwrap();
    
        println!("{}", content);
    }
  3. Reference the ClipboardProvider trait

    master

    The ClipboardProvider trait defines the core interface for interacting with the clipboard. Any type implementing this trait can be used to read or write string data.

    trait ClipboardProvider {
        fn get_contents(&mut self) -> Result<String, Box<Error>>;
        fn set_contents(&mut self, String) -> Result<(), Box<Error>>;
    }
  4. Use the platform-specific ClipboardContext type alias

    master

    The ClipboardContext type alias provides a unified way to reference the underlying system clipboard context regardless of the target operating system. It automatically resolves to the correct implementation:

    • Linux (X11): x11_clipboard::X11ClipboardContext
    • Linux (Wayland): (Via wayland_clipboard module)
    • Windows: windows_clipboard::WindowsClipboardContext
    • macOS: osx_clipboard::OSXClipboardContext
    • Android/iOS/Other: nop_clipboard::NopClipboardContext (No-op implementation)
  5. Implement the ClipboardProvider trait

    master

    To integrate custom clipboard functionality into the copypasta ecosystem, implement the ClipboardProvider trait. This trait provides a platform-agnostic interface for retrieving and updating clipboard data as strings. All implementations must be Send to ensure thread safety.

    use copypasta::ClipboardProvider;
    use copypasta::Result;
    
    struct MyClipboard;
    
    impl ClipboardProvider for MyClipboard {
        fn get_contents(&mut self) -> Result<String> {
            // Implementation to retrieve clipboard content
            Ok("content".to_string())
        }
    
        fn set_contents(&mut self, contents: String) -> Result<()> {
            // Implementation to set clipboard content
            Ok(())
        }
    }
  6. Implement a custom clipboard provider using the ClipboardProvider trait

    master

    The copypasta crate provides the ClipboardProvider trait, which defines the interface for interacting with the system clipboard. To integrate a custom clipboard implementation, you must implement this trait. The crate also provides a platform-specific ClipboardContext type alias that resolves to the appropriate context for your current operating system (e.g., X11ClipboardContext on Linux/X11, WindowsClipboardContext on Windows, or OSXClipboardContext on macOS).

    use copypasta::ClipboardProvider;
    
    // Implementation would follow the trait definition provided by the crate
    // struct MyClipboard;
    // impl ClipboardProvider for MyClipboard { ... }
  7. Use the copypasta Result type

    master

    The copypasta::Result<T> type is a specialized alias for std::result::Result<T, Box<dyn Error + Send + Sync + 'static>>. It is used across the crate to handle errors that are thread-safe and have a static lifetime, making it suitable for asynchronous or multi-threaded clipboard operations.

    use copypasta::Result;
    
    fn do_something() -> Result<()> {
        // ...
        Ok(())
    }