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,12 @@ 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( any(
71
+ all( target_family = "unix" , not( target_os = "macos" ) ) ,
72
+ test
73
+ ) ) ]
71
74
fn copy_string_osc52 ( text : & str , out : & mut impl Write ) -> Result < ( ) > {
75
+ use base64:: prelude:: { Engine , BASE64_STANDARD } ;
72
76
const OSC52_DESTINATION_CLIPBOARD : char = 'c' ;
73
77
write ! (
74
78
out,
@@ -80,33 +84,47 @@ fn copy_string_osc52(text: &str, out: &mut impl Write) -> Result<()> {
80
84
}
81
85
82
86
#[ 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
- }
87
+ fn copy_string_wayland ( text : & str ) -> Result < ( ) > {
88
+ if exec_copy_with_args ( "wl-copy" , & [ ] , text, false ) . is_ok ( ) {
89
+ return Ok ( ( ) ) ;
88
90
}
89
91
90
- if is_wsl ( ) {
91
- return exec_copy_with_args ( "clip.exe" , & [ ] , text, false ) ;
92
- }
92
+ copy_string_osc52 ( text, & mut std:: io:: stdout ( ) )
93
+ }
93
94
95
+ #[ cfg( all( target_family = "unix" , not( target_os = "macos" ) ) ) ]
96
+ fn copy_string_x ( text : & str ) -> Result < ( ) > {
94
97
if exec_copy_with_args (
95
98
"xclip" ,
96
99
& [ "-selection" , "clipboard" ] ,
97
100
text,
98
101
false ,
99
102
)
100
- . is_err ( )
103
+ . is_ok ( )
101
104
{
102
- if exec_copy_with_args ( "xsel" , & [ "--clipboard" ] , text, true )
103
- . is_err ( )
104
- {
105
- copy_string_osc52 ( text, & mut std:: io:: stdout ( ) ) ?;
106
- }
105
+ return Ok ( ( ) ) ;
107
106
}
108
107
109
- Ok ( ( ) )
108
+ if exec_copy_with_args ( "xsel" , & [ "--clipboard" ] , text, true )
109
+ . is_ok ( )
110
+ {
111
+ return Ok ( ( ) ) ;
112
+ }
113
+
114
+ copy_string_osc52 ( text, & mut std:: io:: stdout ( ) )
115
+ }
116
+
117
+ #[ cfg( all( target_family = "unix" , not( target_os = "macos" ) ) ) ]
118
+ pub fn copy_string ( text : & str ) -> Result < ( ) > {
119
+ if std:: env:: var ( "WAYLAND_DISPLAY" ) . is_ok ( ) {
120
+ return copy_string_wayland ( text) ;
121
+ }
122
+
123
+ if is_wsl ( ) {
124
+ return exec_copy_with_args ( "clip.exe" , & [ ] , text, false ) ;
125
+ }
126
+
127
+ copy_string_x ( text)
110
128
}
111
129
112
130
#[ cfg( any( target_os = "macos" , windows) ) ]
0 commit comments