crossterm/examples/copy-to-clipboard.rs
Johannes Agricola 7da7e31596
Add copying to clipboard using OSC52 (#974)
Many terminal emulators support copying text to clipboard using
ANSI OSC Ps; PT ST with Ps = 5 2, see
https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands

This enables copying even through SSH and terminal multiplexers.

Co-authored-by: Naseschwarz <naseschwarz@0x53a.de>
2025-04-05 16:38:59 +02:00

47 lines
1.5 KiB
Rust

//! Demonstrates copying a string to clipboard
//!
//! This example uses OSC control sequence `Pr = 5 2` (See
//! <https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands)>
//! to copy data to the terminal host clipboard.
//!
//! This only works if it is enabled on the respective terminal emulator. If a terminal multiplexer
//! is used, the multiplexer will likely need to support it, too.
//!
//! ```no_run
//! cargo run --example copy-to-clipboard -- --clipboard "Some String"
//! cargo run --example copy-to-clipboard -- --primary "Some String"
//! cargo run --example copy-to-clipboard -- "Some String"
//! ```
use std::io;
use crossterm::clipboard;
use crossterm::execute;
fn main() -> io::Result<()> {
let mut stdout = io::stdout();
let mut args = std::env::args();
args.next(); // Skip to first argument
let default_text = String::from("Example text");
let (text, dest) = match args.next().as_deref() {
Some("--clipboard") => (
args.next().unwrap_or(default_text),
clipboard::ClipboardType::Clipboard,
),
Some("--primary") => (
args.next().unwrap_or(default_text),
clipboard::ClipboardType::Primary,
),
Some(text) => (text.to_owned(), clipboard::ClipboardType::Clipboard),
None => (default_text, clipboard::ClipboardType::Clipboard),
};
execute!(
stdout,
clipboard::CopyToClipboard {
content: text,
destination: clipboard::ClipboardSelection(vec![dest])
}
)
}