edit/clipboard.rs
1//! Clipboard facilities for the editor.
2
3/// The builtin, internal clipboard of the editor.
4///
5/// This is useful particularly when the terminal doesn't support
6/// OSC 52 or when the clipboard contents are huge (e.g. 1GiB).
7#[derive(Default)]
8pub struct Clipboard {
9 data: Vec<u8>,
10 line_copy: bool,
11 wants_host_sync: bool,
12}
13
14impl Clipboard {
15 /// If true, we should emit a OSC 52 sequence to sync the clipboard
16 /// with the hosting terminal.
17 pub fn wants_host_sync(&self) -> bool {
18 self.wants_host_sync
19 }
20
21 /// Call this once the clipboard has been synchronized with the host.
22 pub fn mark_as_synchronized(&mut self) {
23 self.wants_host_sync = false;
24 }
25
26 /// The editor has a special behavior when you have no selection and press
27 /// Ctrl+C: It copies the current line to the clipboard. Then, when you
28 /// paste it, it inserts the line at *the start* of the current line.
29 /// This effectively prepends the current line with the copied line.
30 /// `clipboard_line_start` is true in that case.
31 pub fn is_line_copy(&self) -> bool {
32 self.line_copy
33 }
34
35 /// Returns the current contents of the clipboard.
36 pub fn read(&self) -> &[u8] {
37 &self.data
38 }
39
40 /// Fill the clipboard with the given data.
41 pub fn write(&mut self, data: Vec<u8>) {
42 if !data.is_empty() {
43 self.data = data;
44 self.line_copy = false;
45 self.wants_host_sync = true;
46 }
47 }
48
49 /// See [`Clipboard::is_line_copy`].
50 pub fn write_was_line_copy(&mut self, line_copy: bool) {
51 self.line_copy = line_copy;
52 }
53}