diff --git a/src/core_editor/clip_buffer.rs b/src/core_editor/clip_buffer.rs index d51ac1f..4a74664 100644 --- a/src/core_editor/clip_buffer.rs +++ b/src/core_editor/clip_buffer.rs @@ -56,21 +56,26 @@ pub use system_clipboard::SystemClipboard; #[cfg(feature = "system_clipboard")] /// Helper to get a clipboard based on the `system_clipboard` feature flag: /// -/// Enabled -> [`SystemClipboard`], which talks to the system +/// Enabled -> [`SystemClipboard`], which talks to the system. If the system clipboard can't be +/// accessed, it will default to [`LocalClipboard`]. /// /// Disabled -> [`LocalClipboard`], which supports cutting and pasting limited to the [`crate::Reedline`] instance -pub fn get_default_clipboard() -> SystemClipboard { - SystemClipboard::new() +pub fn get_default_clipboard() -> Box { + SystemClipboard::new().map_or_else( + |_e| Box::new(LocalClipboard::new()) as Box, + |cb| Box::new(cb), + ) } #[cfg(not(feature = "system_clipboard"))] /// Helper to get a clipboard based on the `system_clipboard` feature flag: /// -/// Enabled -> `SystemClipboard`, which talks to the system +/// Enabled -> `SystemClipboard`, which talks to the system. If the system clipboard can't be +/// accessed, it will default to [`LocalClipboard`]. /// /// Disabled -> [`LocalClipboard`], which supports cutting and pasting limited to the [`crate::Reedline`] instance -pub fn get_default_clipboard() -> LocalClipboard { - LocalClipboard::new() +pub fn get_default_clipboard() -> Box { + Box::new(LocalClipboard::new()) } #[cfg(feature = "system_clipboard")] @@ -78,7 +83,7 @@ mod system_clipboard { use super::*; use arboard::Clipboard as Arboard; - /// Wrapper around [`clipboard`](https://docs.rs/clipboard) crate + /// Wrapper around [`arboard`](https://docs.rs/arboard) crate /// /// Requires that the feature `system_clipboard` is enabled pub struct SystemClipboard { @@ -88,13 +93,12 @@ mod system_clipboard { } impl SystemClipboard { - pub fn new() -> Self { - let cb = Arboard::new().unwrap(); - SystemClipboard { - cb, + pub fn new() -> Result { + Ok(SystemClipboard { + cb: Arboard::new()?, local_copy: String::new(), mode: ClipboardMode::Normal, - } + }) } } @@ -120,7 +124,7 @@ mod system_clipboard { #[cfg(test)] mod tests { - use super::{get_default_clipboard, Clipboard, ClipboardMode}; + use super::{get_default_clipboard, ClipboardMode}; #[test] fn reads_back() { let mut cb = get_default_clipboard(); diff --git a/src/core_editor/editor.rs b/src/core_editor/editor.rs index e59f531..5e96b16 100644 --- a/src/core_editor/editor.rs +++ b/src/core_editor/editor.rs @@ -18,7 +18,7 @@ impl Default for Editor { fn default() -> Self { Editor { line_buffer: LineBuffer::new(), - cut_buffer: Box::new(get_default_clipboard()), + cut_buffer: get_default_clipboard(), edit_stack: EditStack::new(), last_undo_behavior: UndoBehavior::CreateUndoPoint, selection_anchor: None,