Skip to content

Commit 7d08fc8

Browse files
authored
Clipboard graceful handling (#712)
* Gracefully handled lack of system clipboard If the `system_clipboard` feature is enabled, but we failed to initialize the `SystemClipboard`, default to `LocalClipboard` instead of panicking. * Remove `eprintln!()` when failing to access system clipboard
1 parent ede2def commit 7d08fc8

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/core_editor/clip_buffer.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,34 @@ pub use system_clipboard::SystemClipboard;
5656
#[cfg(feature = "system_clipboard")]
5757
/// Helper to get a clipboard based on the `system_clipboard` feature flag:
5858
///
59-
/// Enabled -> [`SystemClipboard`], which talks to the system
59+
/// Enabled -> [`SystemClipboard`], which talks to the system. If the system clipboard can't be
60+
/// accessed, it will default to [`LocalClipboard`].
6061
///
6162
/// Disabled -> [`LocalClipboard`], which supports cutting and pasting limited to the [`crate::Reedline`] instance
62-
pub fn get_default_clipboard() -> SystemClipboard {
63-
SystemClipboard::new()
63+
pub fn get_default_clipboard() -> Box<dyn Clipboard> {
64+
SystemClipboard::new().map_or_else(
65+
|_e| Box::new(LocalClipboard::new()) as Box<dyn Clipboard>,
66+
|cb| Box::new(cb),
67+
)
6468
}
6569

6670
#[cfg(not(feature = "system_clipboard"))]
6771
/// Helper to get a clipboard based on the `system_clipboard` feature flag:
6872
///
69-
/// Enabled -> `SystemClipboard`, which talks to the system
73+
/// Enabled -> `SystemClipboard`, which talks to the system. If the system clipboard can't be
74+
/// accessed, it will default to [`LocalClipboard`].
7075
///
7176
/// Disabled -> [`LocalClipboard`], which supports cutting and pasting limited to the [`crate::Reedline`] instance
72-
pub fn get_default_clipboard() -> LocalClipboard {
73-
LocalClipboard::new()
77+
pub fn get_default_clipboard() -> Box<dyn Clipboard> {
78+
Box::new(LocalClipboard::new())
7479
}
7580

7681
#[cfg(feature = "system_clipboard")]
7782
mod system_clipboard {
7883
use super::*;
7984
use arboard::Clipboard as Arboard;
8085

81-
/// Wrapper around [`clipboard`](https://docs.rs/clipboard) crate
86+
/// Wrapper around [`arboard`](https://docs.rs/arboard) crate
8287
///
8388
/// Requires that the feature `system_clipboard` is enabled
8489
pub struct SystemClipboard {
@@ -88,13 +93,12 @@ mod system_clipboard {
8893
}
8994

9095
impl SystemClipboard {
91-
pub fn new() -> Self {
92-
let cb = Arboard::new().unwrap();
93-
SystemClipboard {
94-
cb,
96+
pub fn new() -> Result<Self, arboard::Error> {
97+
Ok(SystemClipboard {
98+
cb: Arboard::new()?,
9599
local_copy: String::new(),
96100
mode: ClipboardMode::Normal,
97-
}
101+
})
98102
}
99103
}
100104

@@ -120,7 +124,7 @@ mod system_clipboard {
120124

121125
#[cfg(test)]
122126
mod tests {
123-
use super::{get_default_clipboard, Clipboard, ClipboardMode};
127+
use super::{get_default_clipboard, ClipboardMode};
124128
#[test]
125129
fn reads_back() {
126130
let mut cb = get_default_clipboard();

src/core_editor/editor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl Default for Editor {
1818
fn default() -> Self {
1919
Editor {
2020
line_buffer: LineBuffer::new(),
21-
cut_buffer: Box::new(get_default_clipboard()),
21+
cut_buffer: get_default_clipboard(),
2222
edit_stack: EditStack::new(),
2323
last_undo_behavior: UndoBehavior::CreateUndoPoint,
2424
selection_anchor: None,

0 commit comments

Comments
 (0)