#clipboard #cross-platform #wayland #x11 #copy #paste #system

cli-clipboard

cli-clipboard is a cross-platform library for getting and setting the contents of the OS-level clipboard

5 releases (3 breaking)

0.4.0 Dec 13, 2022
0.3.0 Oct 12, 2022
0.2.1 Feb 28, 2022
0.2.0 Jul 31, 2020
0.1.0 Jul 20, 2020

#3 in #paste

Download history 5852/week @ 2024-07-22 3738/week @ 2024-07-29 6181/week @ 2024-08-05 4651/week @ 2024-08-12 4956/week @ 2024-08-19 3310/week @ 2024-08-26 4780/week @ 2024-09-02 4447/week @ 2024-09-09 6280/week @ 2024-09-16 7639/week @ 2024-09-23 6484/week @ 2024-09-30 7199/week @ 2024-10-07 5284/week @ 2024-10-14 7029/week @ 2024-10-21 6063/week @ 2024-10-28 5424/week @ 2024-11-04

23,986 downloads per month
Used in 45 crates (39 directly)

MIT/Apache

26KB
347 lines

CLI Clipboard

Rust

cli-clipboard is a fork of rust-clipboard that adds wayland support for terminal and window-less applications via wl-clipboard-rs. For terminal applications it supports copy and paste for both wayland and X11 linux environments, macOS and windows.

On Linux it will first attempt to setup a Wayland clipboard provider. If that fails it will then fallback to the X11 clipboard provider.

Note: On Linux, you'll need to have xorg-dev and libxcb-composite0-dev to compile. On Debian and Ubuntu you can install them with

sudo apt install xorg-dev libxcb-composite0-dev

Examples

Using ClipboardContext to create a clipboard provider:

use cli_clipboard::{ClipboardContext, ClipboardProvider};

let mut ctx = ClipboardContext::new().unwrap();
let the_string = "Hello, world!";
ctx.set_contents(the_string.to_owned()).unwrap();
assert_eq!(ctx.get_contents().unwrap(), the_string);
ctx.clear();
// clearing the clipboard causes get_contents to return Err on macos and windows
if cfg!(any(windows, target_os = "macos")) {
    if ctx.get_contents().is_ok() {
        panic!("Should be Err");
    }
} else {
    assert_eq!(ctx.get_contents(), "");
}

Using the helper functions:

use cli_clipboard;

let the_string = "Hello, world!";
cli_clipboard::set_contents(the_string.to_owned()).unwrap();
assert_eq!(cli_clipboard::get_contents().unwrap(), the_string);

API

ClipboardProvider

The ClipboardProvider trait has the following functions:

fn new() -> anyhow::Result<Self>;
fn get_contents(&mut self) -> anyhow::Result<String>;
fn set_contents(&mut self, String) -> anyhow::Result<()>;
fn clear(&mut self) -> anhow::Result<()>;

ClipboardContext

  • ClipboardContext is a type alias for one of {WindowsClipboardContext, OSXClipboardContext, LinuxClipboardContext}, all of which implement ClipboardProvider. Which concrete type is chosen for ClipboardContext depends on the OS (via conditional compilation).
  • WaylandClipboardContext and X11ClipboardContext are also available but generally the correct one will be chosen by LinuxClipboardContext.

Convenience Functions

get_contents and set_contents are convenience functions that create a context for you and call the respective function on it.

Alternatives

  1. copypasta - rust-clipboard fork adding wayland support for windowed applications
  2. The original rust-clipboard

License

cli-clipboard is dual-licensed under MIT and Apache2.

Dependencies

~0–10MB
~117K SLoC