1
1
use anyhow:: { anyhow, Result } ;
2
- use base64:: prelude:: { Engine , BASE64_STANDARD } ;
3
2
use std:: io:: Write ;
4
3
use std:: path:: PathBuf ;
5
4
use std:: process:: { Command , Stdio } ;
@@ -68,7 +67,9 @@ fn is_wsl() -> bool {
68
67
// This enables copying even if there is no Wayland or X socket available,
69
68
// e.g. via SSH, as long as it supported by the terminal.
70
69
// See https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
70
+ #[ cfg( all( target_family = "unix" , not( target_os = "macos" ) ) ) ]
71
71
fn copy_string_osc52 ( text : & str , out : & mut impl Write ) -> Result < ( ) > {
72
+ use base64:: prelude:: { Engine , BASE64_STANDARD } ;
72
73
const OSC52_DESTINATION_CLIPBOARD : char = 'c' ;
73
74
write ! (
74
75
out,
@@ -80,33 +81,47 @@ fn copy_string_osc52(text: &str, out: &mut impl Write) -> Result<()> {
80
81
}
81
82
82
83
#[ cfg( all( target_family = "unix" , not( target_os = "macos" ) ) ) ]
83
- pub fn copy_string ( text : & str ) -> Result < ( ) > {
84
- if std:: env:: var ( "WAYLAND_DISPLAY" ) . is_ok ( ) {
85
- if exec_copy_with_args ( "wl-copy" , & [ ] , text, false ) . is_err ( ) {
86
- copy_string_osc52 ( text, & mut std:: io:: stdout ( ) ) ?;
87
- }
84
+ fn copy_string_wayland ( text : & str ) -> Result < ( ) > {
85
+ if exec_copy_with_args ( "wl-copy" , & [ ] , text, false ) . is_ok ( ) {
86
+ return Ok ( ( ) ) ;
88
87
}
89
88
90
- if is_wsl ( ) {
91
- return exec_copy_with_args ( "clip.exe" , & [ ] , text, false ) ;
92
- }
89
+ copy_string_osc52 ( text, & mut std:: io:: stdout ( ) )
90
+ }
93
91
92
+ #[ cfg( all( target_family = "unix" , not( target_os = "macos" ) ) ) ]
93
+ fn copy_string_x ( text : & str ) -> Result < ( ) > {
94
94
if exec_copy_with_args (
95
95
"xclip" ,
96
96
& [ "-selection" , "clipboard" ] ,
97
97
text,
98
98
false ,
99
99
)
100
- . is_err ( )
100
+ . is_ok ( )
101
101
{
102
- if exec_copy_with_args ( "xsel" , & [ "--clipboard" ] , text, true )
103
- . is_err ( )
104
- {
105
- copy_string_osc52 ( text, & mut std:: io:: stdout ( ) ) ?;
106
- }
102
+ return Ok ( ( ) ) ;
107
103
}
108
104
109
- Ok ( ( ) )
105
+ if exec_copy_with_args ( "xsel" , & [ "--clipboard" ] , text, true )
106
+ . is_ok ( )
107
+ {
108
+ return Ok ( ( ) ) ;
109
+ }
110
+
111
+ copy_string_osc52 ( text, & mut std:: io:: stdout ( ) )
112
+ }
113
+
114
+ #[ cfg( all( target_family = "unix" , not( target_os = "macos" ) ) ) ]
115
+ pub fn copy_string ( text : & str ) -> Result < ( ) > {
116
+ if std:: env:: var ( "WAYLAND_DISPLAY" ) . is_ok ( ) {
117
+ return copy_string_wayland ( text) ;
118
+ }
119
+
120
+ if is_wsl ( ) {
121
+ return exec_copy_with_args ( "clip.exe" , & [ ] , text, false ) ;
122
+ }
123
+
124
+ copy_string_x ( text)
110
125
}
111
126
112
127
#[ cfg( any( target_os = "macos" , windows) ) ]
0 commit comments