Skip to content

Commit

Permalink
Update reedline to use the system-clipboard only for explicit cut/cop…
Browse files Browse the repository at this point in the history
…y/paste operation
  • Loading branch information
Tastaturtaste committed Feb 27, 2024
1 parent 6dd7c39 commit 998a2f7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/core_editor/clip_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ mod system_clipboard {

#[cfg(test)]
mod tests {
use super::{get_local_clipboard, get_system_clipboard, ClipboardMode};
#[cfg(feature = "system_clipboard")]
use super::get_system_clipboard;
use super::{get_local_clipboard, ClipboardMode};
#[test]
fn reads_back_local() {
let mut cb = get_local_clipboard();
Expand All @@ -132,6 +134,7 @@ mod tests {
cb.set(&previous_state, ClipboardMode::Normal);
}

#[cfg(feature = "system_clipboard")]
#[test]
fn reads_back_system() {
let mut cb = get_system_clipboard();
Expand Down
56 changes: 53 additions & 3 deletions src/core_editor/editor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::{edit_stack::EditStack, Clipboard, ClipboardMode, LineBuffer};
#[cfg(feature = "system_clipboard")]
use crate::core_editor::get_system_clipboard;
use crate::enums::{EditType, UndoBehavior};
use crate::{core_editor::get_local_clipboard, core_editor::get_system_clipboard, EditCommand};
use crate::{core_editor::get_local_clipboard, EditCommand};
use std::ops::DerefMut;

/// Stateful editor executing changes to the underlying [`LineBuffer`]
Expand Down Expand Up @@ -534,6 +536,9 @@ impl Editor {
}

fn cut_selection_to_system(&mut self) {
#[cfg(not(feature = "system_clipboard"))]
panic!("The OS clipboard is not enabled. Enable its use with the reedline/system_clipboard feature!");
#[cfg(feature = "system_clipboard")]
if let Some((start, end)) = self.get_selection() {
let cut_slice = &self.line_buffer.get_buffer()[start..end];
self.system_clipboard.set(cut_slice, ClipboardMode::Normal);
Expand All @@ -552,6 +557,9 @@ impl Editor {
}

fn copy_selection_to_system(&mut self) {
#[cfg(not(feature = "system_clipboard"))]
panic!("The OS clipboard is not enabled. Enable its use with the reedline/system_clipboard feature!");
#[cfg(feature = "system_clipboard")]
if let Some((start, end)) = self.get_selection() {
let cut_slice = &self.line_buffer.get_buffer()[start..end];
self.system_clipboard.set(cut_slice, ClipboardMode::Normal);
Expand Down Expand Up @@ -644,8 +652,16 @@ impl Editor {
}

fn paste_system_clipboard(&mut self) {
self.delete_selection();
insert_clipboard_content_before(&mut self.line_buffer, self.system_clipboard.deref_mut());
#[cfg(not(feature = "system_clipboard"))]
panic!("The OS clipboard is not enabled. Enable its use with the reedline/system_clipboard feature!");
#[cfg(feature = "system_clipboard")]
{
self.delete_selection();
insert_clipboard_content_before(
&mut self.line_buffer,
self.system_clipboard.deref_mut(),
);
}
}

fn paste_cut_buffer(&mut self) {
Expand Down Expand Up @@ -880,4 +896,38 @@ mod test {
editor.run_edit_command(&EditCommand::Undo);
assert_eq!(editor.get_buffer(), "This \r\n is a test");
}
#[cfg(not(feature = "system_clipboard"))]
mod without_system_clipboard {
use super::*;
#[test]
#[should_panic]
fn test_cut_selection_panic() {
let mut editor = editor_with("This is a test!");
editor.selection_anchor = Some(editor.line_buffer.len());
editor.line_buffer.set_insertion_point(0);
editor.run_edit_command(&EditCommand::CutSelection {
system_clipboard: true,
});
}
#[test]
#[should_panic]
fn test_copy_selection_panic() {
let mut editor = editor_with("This is a test!");
editor.selection_anchor = Some(editor.line_buffer.len());
editor.line_buffer.set_insertion_point(0);
editor.run_edit_command(&EditCommand::CopySelection {
system_clipboard: true,
});
}
#[test]
#[should_panic]
fn test_paste_selection_panic() {
let mut editor = editor_with("This is a test!");
editor.selection_anchor = Some(editor.line_buffer.len());
editor.line_buffer.set_insertion_point(0);
editor.run_edit_command(&EditCommand::Paste {
system_clipboard: true,
});
}
}
}

0 comments on commit 998a2f7

Please sign in to comment.