rusty_bubbletea/clipboard.rs
1//! Cleanroom Rust port of upstream Go source file: `clipboard.go`
2//! Upstream Target Tag / Version: `v2.0.8`
3//!
4//! <public-docs>
5//! # Clipboard
6//!
7//! OSC52 terminal clipboard read/write command constructors (`SetClipboard`, `ReadClipboard`, `SetPrimaryClipboard`, `ReadPrimaryClipboard`) and `ClipboardMsg`.
8//! </public-docs>
9
10use crate::model::Cmd;
11use std::fmt;
12
13/// ClipboardMsg is a clipboard read message event. This message is emitted when
14/// a terminal receives an OSC52 clipboard read message event.
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct ClipboardMsg {
17 /// Content string.
18 pub content: String,
19 /// Selection byte ('c' system, 'p' primary).
20 pub selection: u8,
21}
22
23impl ClipboardMsg {
24 /// Returns the clipboard selection type. This will be one of the
25 /// following values:
26 ///
27 /// - `c`: System clipboard.
28 /// - `p`: Primary clipboard (X11/Wayland only).
29 pub fn clipboard(&self) -> u8 {
30 self.selection
31 }
32}
33
34impl fmt::Display for ClipboardMsg {
35 /// Returns the string representation of the clipboard message.
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 write!(f, "{}", self.content)
38 }
39}
40
41/// SetClipboardMsg is an internal message used to set the system clipboard
42/// using OSC52.
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub struct SetClipboardMsg(pub String);
45
46/// SetClipboard produces a command that sets the system clipboard using OSC52.
47/// Note that OSC52 is not supported in all terminals.
48pub fn set_clipboard(s: &str) -> Cmd {
49 let text = s.to_string();
50 Some(Box::new(move || Some(Box::new(SetClipboardMsg(text)))))
51}
52
53/// ReadClipboardMsg is an internal message used to read the system clipboard
54/// using OSC52.
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56pub struct ReadClipboardMsg;
57
58/// ReadClipboard produces a command that reads the system clipboard using OSC52.
59/// Note that OSC52 is not supported in all terminals.
60pub fn read_clipboard() -> Cmd {
61 Some(Box::new(|| Some(Box::new(ReadClipboardMsg))))
62}
63
64/// SetPrimaryClipboardMsg is an internal message used to set the primary
65/// clipboard using OSC52.
66#[derive(Debug, Clone, PartialEq, Eq)]
67pub struct SetPrimaryClipboardMsg(pub String);
68
69/// SetPrimaryClipboard produces a command that sets the primary clipboard using
70/// OSC52. Primary clipboard selection is a feature present in X11 and Wayland
71/// only.
72/// Note that OSC52 is not supported in all terminals.
73pub fn set_primary_clipboard(s: &str) -> Cmd {
74 let text = s.to_string();
75 Some(Box::new(move || {
76 Some(Box::new(SetPrimaryClipboardMsg(text)))
77 }))
78}
79
80/// ReadPrimaryClipboardMsg is an internal message used to read the primary
81/// clipboard using OSC52.
82#[derive(Debug, Clone, Copy, PartialEq, Eq)]
83pub struct ReadPrimaryClipboardMsg;
84
85/// ReadPrimaryClipboard produces a command that reads the primary clipboard
86/// using OSC52. Primary clipboard selection is a feature present in X11 and
87/// Wayland only.
88/// Note that OSC52 is not supported in all terminals.
89pub fn read_primary_clipboard() -> Cmd {
90 Some(Box::new(|| Some(Box::new(ReadPrimaryClipboardMsg))))
91}