@@ -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" ) ]
7782mod 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) ]
122126mod 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 ( ) ;
0 commit comments