Skip to content

Commit

Permalink
Gracefully handled lack of system clipboard
Browse files Browse the repository at this point in the history
If the `system_clipboard` feature is enabled, but we failed to
initialize the `SystemClipboard`, default to `LocalClipboard` instead of
panicking.
  • Loading branch information
abusch committed Jan 22, 2024
1 parent cbb56e2 commit 40221bc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
33 changes: 20 additions & 13 deletions src/core_editor/clip_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,37 @@ 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<dyn Clipboard> {
SystemClipboard::new().map_or_else(
|_e| {
eprintln!("Defaulting to local clipboard!");
Box::new(LocalClipboard::new()) as Box<dyn Clipboard>
},
|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<dyn Clipboard> {
Box::new(LocalClipboard::new())
}

#[cfg(feature = "system_clipboard")]
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 {
Expand All @@ -88,13 +96,12 @@ mod system_clipboard {
}

impl SystemClipboard {
pub fn new() -> Self {
let cb = Arboard::new().unwrap();
SystemClipboard {
cb,
pub fn new() -> Result<Self, arboard::Error> {
Ok(SystemClipboard {
cb: Arboard::new()?,
local_copy: String::new(),
mode: ClipboardMode::Normal,
}
})
}
}

Expand All @@ -120,7 +127,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();
Expand Down
2 changes: 1 addition & 1 deletion src/core_editor/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 40221bc

Please sign in to comment.