a simple clipboard for complicated times
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//! implements different clipboard types
use std::{
    io::{Read, Write},
    process::{Command, Stdio},
};

pub trait Clipboard {
    fn copy(text: &str);
    fn paste() -> String;
}

macro_rules! c {
    ($p:ident $($args:ident)+) => {
        Command::new(stringify!($p)).args([$(stringify!($args),)+])
    };
    ($p:literal) => {
        Command::new($p)
    };
    ($p:literal $($args:literal)+) => {
        Command::new($p).args([$($args,)+])

    }
}

trait Eat {
    fn eat(&mut self) -> String;
}

impl Eat for Command {
    fn eat(&mut self) -> String {
        let mut s = String::new();
        self.stdout(Stdio::piped())
            .spawn()
            .expect("spawn ok")
            .stdout
            .take()
            .unwrap()
            .read_to_string(&mut s)
            .expect("read ok");
        s
    }
}

trait Put {
    fn put(&mut self, s: impl AsRef<[u8]>);
}

impl Put for Command {
    fn put(&mut self, s: impl AsRef<[u8]>) {
        let mut ch = self.stdin(Stdio::piped()).spawn().expect("spawn ok");
        ch.stdin
            .take()
            .unwrap()
            .write_all(s.as_ref())
            .expect("write ok");
        ch.wait().expect("proc ok");
    }
}

#[cfg(target_os = "macos")]
pub struct PbCopy {}
#[cfg(target_os = "macos")]
impl Clipboard for PbCopy {
    fn copy(text: &str) {
        c!(pbcopy w).put(text)
    }

    fn paste() -> String {
        c!(pbcopy r).eat()
    }
}

pub struct XClip {}
impl Clipboard for XClip {
    fn copy(text: &str) {
        c!("xclip" "-selection" "c").put(text);
    }

    fn paste() -> String {
        c!("xclip" "-selection" "c" "-o") // xcclip is complainy
            .stderr(Stdio::null())
            .stdout(Stdio::null())
            .eat()
    }
}

pub struct XSel {}
impl Clipboard for XSel {
    fn copy(text: &str) {
        c!("xsel" "-b" "-i").put(text);
    }

    fn paste() -> String {
        c!("xsel" "-b" "-o").eat()
    }
}

struct Wayland {}
impl Clipboard for Wayland {
    fn copy(text: &str) {
        match text {
            "" => assert!(
                c!("wl-copy" "--clear").status().unwrap().success(),
                "wl-copy fail"
            ),
            s => c!("wl-copy").put(s),
        }
    }

    fn paste() -> String {
        c!("wl-paste" "-n").eat()
    }
}

struct Klipper {}
impl Clipboard for Klipper {
    fn copy(text: &str) {
        c!("qdbus" "org.kde.klipper" "/klipper" "setClipboardContents").arg(text);
    }

    fn paste() -> String {
        let mut s = c!("qdbus" "org.kde.klipper" "/klipper" "getClipboardContents").eat();
        assert!(s.ends_with('\n'));
        s.truncate(s.len() - 1);
        s
    }
}

#[cfg(target_family = "windows")]
struct Windows {}
#[cfg(target_family = "windows")]
impl Clipboard for Windows {
    fn copy(text: &str) {
        clipboard_win::set_clipboard_string(text).expect("set clip ok")
    }

    fn paste() -> String {
        clipboard_win::get_clipboard_string().expect("get clip ok")
    }
}

struct Wsl {}

impl Clipboard for Wsl {
    fn copy(text: &str) {
        c!("clip.exe").put(text);
    }

    fn paste() -> String {
        let mut s = c!("powershell.exe" "-noprofile" "-command" "Get-Clipboard").eat();
        s.truncate(s.len() - 2); // \r\n
        s
    }
}

pub type Board = (for<'a> fn(&'a str), fn() -> String);

fn get<T: Clipboard>() -> Board {
    println!("{}", std::any::type_name::<T>());
    (T::copy, T::paste)
}

fn has(c: &str) -> bool {
    c!("which")
        .arg(c)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .expect("ok")
        .success()
}

fn wsl() -> bool {
    if let Ok(s) = std::fs::read_to_string("/proc/version") {
        if s.to_lowercase().contains("microsoft") {
            return true;
        }
    }
    false
}

pub fn provide() -> Board {
    #[cfg(target_family = "windows")]
    return get::<Windows>();
    #[cfg(target_os = "macos")]
    return get::<PbCopy>();

    if wsl() {
        return get::<Wsl>();
    }
    assert!(std::env::var("DISPLAY").is_ok(), "no clipboard available");
    if std::env::var("WAYLAND_DISPLAY").is_ok() && has("wl-copy") {
        get::<Wayland>()
    } else if has("xsel") {
        get::<XSel>()
    } else if has("xclip") {
        get::<XClip>()
    } else if has("klipper") && has("qdbus") {
        get::<Klipper>()
    } else {
        panic!("no clipboard available");
    }
}

#[test]
fn test() {
    macro_rules! test {
        ($clipboard:ty) => {
            <$clipboard>::copy("text");
            assert_eq!(<$clipboard>::paste(), "text");
            <$clipboard>::copy("");
        };
    }
    #[cfg(target_os = "macos")]
    test!(PbCopy);
    #[cfg(target_os = "linux")]
    test!(XClip);
    #[cfg(target_os = "linux")]
    test!(XSel);
    #[cfg(target_os = "linux")]
    if std::env::var("WAYLAND_DISPLAY").is_ok() {
        test!(Wayland);
    }
    #[cfg(target_os = "linux")]
    test!(Klipper);
    #[cfg(target_family = "windows")]
    test!(Windows);
    if wsl() {
        #[cfg(target_os = "linux")]
        test!(Wsl);
    }
}