@@ -8,9 +8,9 @@ use crate::{
88 strings, try_or_popup,
99 ui:: { self , calc_scroll_top, style:: SharedTheme } ,
1010} ;
11+ use anyhow:: Result ;
1112use asyncgit:: { hash, sync, DiffLine , DiffLineType , FileDiff , CWD } ;
1213use bytesize:: ByteSize ;
13- use clipboard:: { ClipboardContext , ClipboardProvider } ;
1414use crossterm:: event:: Event ;
1515use std:: { borrow:: Cow , cell:: Cell , cmp, path:: Path } ;
1616use tui:: {
@@ -21,8 +21,6 @@ use tui::{
2121 Frame ,
2222} ;
2323
24- use anyhow:: { anyhow, Result } ;
25-
2624#[ derive( Default ) ]
2725struct Current {
2826 path : String ,
@@ -244,18 +242,6 @@ impl DiffComponent {
244242 Ok ( ( ) )
245243 }
246244
247- fn copy_string ( string : String ) -> Result < ( ) > {
248- let mut ctx: ClipboardContext = ClipboardProvider :: new ( )
249- . map_err ( |_| {
250- anyhow ! ( "failed to get access to clipboard" )
251- } ) ?;
252- ctx. set_contents ( string) . map_err ( |_| {
253- anyhow ! ( "failed to set clipboard contents" )
254- } ) ?;
255-
256- Ok ( ( ) )
257- }
258-
259245 fn copy_selection ( & self ) -> Result < ( ) > {
260246 if let Some ( diff) = & self . diff {
261247 let lines_to_copy: Vec < & str > = diff
@@ -281,7 +267,9 @@ impl DiffComponent {
281267 try_or_popup ! (
282268 self ,
283269 "copy to clipboard error:" ,
284- Self :: copy_string( lines_to_copy. join( "\n " ) )
270+ crate :: clipboard:: copy_string(
271+ lines_to_copy. join( "\n " )
272+ )
285273 ) ;
286274 }
287275
@@ -616,11 +604,13 @@ impl Component for DiffComponent {
616604 self . focused ,
617605 ) ) ;
618606
619- out. push ( CommandInfo :: new (
620- strings:: commands:: copy ( & self . key_config ) ,
621- true ,
622- self . focused ,
623- ) ) ;
607+ if crate :: clipboard:: is_supported ( ) {
608+ out. push ( CommandInfo :: new (
609+ strings:: commands:: copy ( & self . key_config ) ,
610+ true ,
611+ self . focused ,
612+ ) ) ;
613+ }
624614
625615 out. push (
626616 CommandInfo :: new (
@@ -700,7 +690,9 @@ impl Component for DiffComponent {
700690 }
701691 }
702692 Ok ( true )
703- } else if e == self . key_config . copy {
693+ } else if e == self . key_config . copy
694+ && crate :: clipboard:: is_supported ( )
695+ {
704696 self . copy_selection ( ) ?;
705697 Ok ( true )
706698 } else {
0 commit comments