gistit-cli 0.1.1

Cli tool for gistit
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Clipboard module
//!
//! The clipboard feature in Gistit is nothing more than a quality of life bonus to automatically store the
//! Gistit hash into your system clipboard. Since we're interested in persisting the Gistit hash
//! after the program exists we have to rely on not so reliable methods to achieve this behaviour.
//!
//! Here we do our best efforts look for the most common clipboard binaries, spawn a child process, and pipe the
//! contents into it's 'stdin'. If no binary was found we'll fallback to OSC52 escape sequence.
//! [OSC52](https://www.reddit.com/r/vim/comments/k1ydpn/a_guide_on_how_to_copy_text_from_anywhere/)
//!
//! credits: this implementation is heavily inspired on
//! [copypasta](https://docs.rs/copypasta/0.7.1/copypasta/)
//!
//! **note** we're not interested in 'paste' functionallity
//!
//! # Linux/BSD
//!
//! On Linux/BSD we'll match the display server and attempt to find related
//! clipboard binaries.
//!
//! ## WSL
//!
//! Will use `clip.exe` to pipe content into.
//!
//! ## X11
//!
//! Will look for `xclip`, `xsel` and use it in this order of preference.
//!
//! ## Wayland
//!
//! Will look for `wl-copy` binary.
//!
//! ## Tty (SSH session)
//!
//! Under this condition we'll do a couple of extra checks to ensure X11 Passthrough is
//! working, otherwise clipboard usage is unlikely to succeed (?).
//!
//! 1. checks for `xauth` binary, utility to manage X11 session cookies.
//! 2. reads `DISPLAY` env variable to ensure it's set with 'localhost:' something something.
//!
//! If the above are ok we check for X11 clipboard binaries to use.
//!
//! # Mac OS
//!
//! We check for `pbcopy` binary but it's absence is not a showstopper since we can still try
//! OSC52 escape sequence.
//!
//! # Windows
//!
//! Doesn't make sense to check for `clip.exe` because it's default installation. Anyhow, we're
//! not using it under this platform. This can change in the future
use std::env;
use std::ffi::OsString;
use std::io::Write;
use std::path::PathBuf;
use std::process::{Command, Stdio};

use which::which;

use crate::{error, Result};

/// The clipboard structure, holds the content string
#[derive(Clone, Debug)]
pub struct Clipboard {
    content: String,
}

/// The clipboard with the display server figured out
#[derive(Clone, Debug)]
pub struct Selected {
    display: DisplayKind,
    content: String,
}

/// The clipboard that attempts the external binary approach
#[derive(Clone, Debug)]
pub struct Binary {
    bin: OsString,
    selected: Selected,
    program: ClipboardBinProgram,
}

/// The clipboard that attempts OSC52 escape sequence approach
#[derive(Clone, Debug)]
pub struct EscapeSequence {
    selected: Selected,
}

/// The display server type
#[derive(Clone, Debug)]
#[cfg_attr(test, derive(Eq, PartialEq))]
enum DisplayKind {
    X11,
    Wayland,
    Wsl,
    SshTty,
    Unknown,
    #[cfg(target_is = "macos")]
    MacOs,
    #[cfg(target_os = "windows")]
    Windows,
}

/// Returns the current display server
fn select_display() -> DisplayKind {
    #[cfg(target_os = "windows")]
    return DisplayKind::Windows;

    #[cfg(target_is = "macos")]
    return DisplayKind::MacOs;

    // Linux/BSD only
    if is_wsl() {
        DisplayKind::Wsl
    } else if is_wayland() {
        DisplayKind::Wayland
    } else if is_x11() {
        DisplayKind::X11
    } else if is_ssh_tty() {
        DisplayKind::SshTty
    } else {
        DisplayKind::Unknown
    }
}

/// Checks whether we're under windows subsystem for linux
#[cfg(target_family = "unix")]
fn is_wsl() -> bool {
    env::var("WSL_DISTRO_NAME").is_ok()
        || env::var("WT_SESSION").is_ok()
        || env::var("WSL_INTEROP").is_ok()
}

/// Check whether or not in Wayland environment
/// This function is avaiable only under Linux/BSD environment so no extra checks are needed.
/// **note** that this is best to run before checking for X11 because `DISPLAY` var can also be set
/// under Wayland.
#[cfg(all(
    target_family = "unix",
    not(all(target_os = "macos", target_os = "ios", target_os = "android"))
))]
fn is_wayland() -> bool {
    let mut score = 0;
    match env::var("XDG_SESSION_TYPE").ok().as_deref() {
        Some("wayland") => score += 1,
        Some(_) | None => (),
    }
    if env::var("WAYLAND_DISPLAY").is_ok() {
        score += 1;
    }
    score > 0
}

/// Check whether or not in X11
/// This function is avaiable only under Linux/BSD environment so no extra checks are needed.
#[cfg(all(
    target_family = "unix",
    not(all(target_os = "macos", target_os = "ios", target_os = "android"))
))]
fn is_x11() -> bool {
    let mut score = 0;
    match env::var("XDG_SESSION_TYPE").ok().as_deref() {
        Some("x11") => score += 1,
        Some(_) | None => (),
    }
    if env::var("DISPLAY").is_ok() {
        score += 1;
    }
    score > 0
}

/// Checks whether or not in TTY.
/// The default session type under SSH is `tty` so we make sure to assert both things
/// since we're not supporting clipboard under raw tty sessions.
#[cfg(all(
    target_family = "unix",
    not(all(target_os = "macos", target_os = "ios", target_os = "android"))
))]
fn is_ssh_tty() -> bool {
    let tty = env::var("XDG_SESSION_TYPE").as_deref() == Ok("tty");
    let ssh = env::var("SSH_CLIENT").is_ok();
    tty && ssh
}

impl Clipboard {
    /// Creates a new Clipboard instance with the content string
    #[must_use]
    pub fn new(content: &str) -> Self {
        Self {
            content: content.to_owned(),
        }
    }

    /// Tries to select the current display
    ///
    /// # Errors
    ///
    /// Fails with [`ClipboardError`] error
    pub fn try_into_selected(self) -> Result<Selected> {
        match select_display() {
            DisplayKind::Unknown => Err(error::Clipboard::UnsupportedPlatform.into()),
            valid => Ok(Selected {
                display: valid,
                content: self.content,
            }),
        }
    }
}

/// The trait that a ready-to-use clipboard implements
pub trait Provider {
    /// Attempt to set the contents into the system clipboard
    ///
    /// # Errors
    ///
    /// Fails with [`ClipboardError`]
    fn set_contents(&self) -> Result<()>;
}

impl Provider for Binary {
    fn set_contents(&self) -> Result<()> {
        let mut command = Command::new(&self.bin);
        match self.program {
            ClipboardBinProgram::Xclip => {
                command.arg("-sel").arg("clip");
            }
            ClipboardBinProgram::Xsel => {
                command.arg("--clipboard");
            }
            ClipboardBinProgram::WlCopy | ClipboardBinProgram::ClipExe => (),
        };
        let mut process = command
            .stdin(Stdio::piped())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()?;

        process
            .stdin
            .as_mut()
            .expect("to access stdin")
            .write_all(self.selected.content.as_bytes())?;

        let _status = process.wait()?;

        Ok(())
    }
}

impl Provider for EscapeSequence {
    fn set_contents(&self) -> Result<()> {
        print!("\x1B]52;c;{}\x07", base64::encode(&self.selected.content));
        Ok(())
    }
}

impl Selected {
    /// Transforms this clipboard into a ready-to-use kind
    /// First checks for binaries and fallbacks to the ANSI escape sequence approach.
    #[must_use]
    pub fn into_provider(self) -> Box<dyn Provider> {
        match self.try_into_bin() {
            Ok(bin_clipboard) => {
                return Box::new(bin_clipboard);
            }
            Err(err) => {
                println!("{:?}", err);
            }
        }
        Box::new(EscapeSequence { selected: self })
    }
}

/// Currently supported clipboard programs
#[non_exhaustive]
#[derive(Clone, Debug)]
enum ClipboardBinProgram {
    Xclip,
    Xsel,
    ClipExe,
    WlCopy,
    #[cfg(all(target_os = "macos", target_os = "ios"))]
    PbCopy,
}

#[cfg(all(
    target_family = "unix",
    not(all(target_os = "macos", target_os = "ios", target_os = "android"))
))]
impl Selected {
    /// Checks for supported clipboard binaries and attempts to convert the selected clipboard into
    /// the binary implementation.
    ///
    /// # Errors
    ///
    /// Will fail with [`ClipboardError`] when any matched display server misses it's supported
    /// clipboard binaries.
    fn try_into_bin(&self) -> Result<Binary> {
        let (bin, program) = match self.display {
            DisplayKind::X11 => {
                let mut binaries = [
                    (which("xclip"), ClipboardBinProgram::Xclip),
                    (which("xsel"), ClipboardBinProgram::Xsel),
                    // TODO: Add more supported clipboard programs here
                ]
                .into_iter();

                let (bin, program) = binaries
                    .find(|(bin, _)| bin.is_ok())
                    .ok_or(error::Clipboard::MissingBinary)?;
                // Safe to unwrap since we previously checked `bin.is_ok()`
                (bin.unwrap(), program)
            }
            DisplayKind::Wayland => {
                let bin = which("wl-copy")?;
                let program = ClipboardBinProgram::WlCopy;
                (bin, program)
            }
            DisplayKind::SshTty => {
                //`xauth` missing most likely mean display passthrough isn't working
                let _xauth = which("xauth")?;

                // DISPALY variable different than `localhost:...` is a bad sign as well
                env::var("DISPLAY").map_err(|_| error::Clipboard::DisplayNotSet)?;

                let mut binaries = [
                    (which("xclip"), ClipboardBinProgram::Xclip),
                    (which("xsel"), ClipboardBinProgram::Xsel),
                    // TODO: Add more supported clipboard programs here
                ]
                .into_iter();

                let (bin, program) = binaries
                    .find(|(bin, _)| bin.is_ok())
                    .ok_or(error::Clipboard::MissingBinary)?;
                // Safe to unwrap since we previously checked `bin.is_ok()`
                (bin.unwrap(), program)
            }
            DisplayKind::Wsl => {
                let bin = PathBuf::from("clip.exe");
                let program = ClipboardBinProgram::ClipExe;
                (bin, program)
            }
            DisplayKind::Unknown => panic!("clipboard feature not supported"),
        };
        Ok(Binary {
            bin: bin.as_os_str().to_owned(),
            selected: self.clone(),
            program,
        })
    }
}

#[cfg(all(target_os = "macos", target_os = "ios"))]
impl Selected {
    /// Checks for supported clipboard binaries and attempts to convert the selected clipboard into
    /// the binary implementation.
    ///
    /// # Errors
    ///
    /// Will fail with [`ClipboardError`] when any matched display server misses it's supported
    /// clipboard binaries.
    fn try_into_bin(&self) -> Result<Binary> {
        let bin = match self.display {
            DisplayKind::MacOs => which("pbcopy")
                .ok()
                .map(|t| t.as_os_str().to_owned())
                .ok_or(ErrorKind::MissingClipboardBinary)?,
            DisplayKind::Unknown => panic!("clipboard feature not supported"),
        };
        let program = ClipboardBinProgram::PbCopy;
        Ok(Binary {
            bin,
            program,
            selected: self.clone(),
        })
    }
}

/// Not supported
#[cfg(target_os = "windows")]
impl Selected {
    fn try_into_bin(&self) -> Result<Binary> {
        Err(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env;

    #[test]
    pub fn clipboard_test_selection_order() {
        env::remove_var("DISPLAY");
        env::remove_var("WSL_DISTRO_NAME");
        env::remove_var("WAYLAND_DISPLAY");
        env::remove_var("SSH_CLIENT");
        env::remove_var("WT_SESSION");
        env::remove_var("WSL_INTEROP");

        env::set_var("DISPLAY", "localhost");
        let clip1 = Clipboard::new("foo").try_into_selected().unwrap();
        assert_eq!(clip1.display, DisplayKind::X11);

        env::set_var("WAYLAND_DISPLAY", "wayland");
        let clip2 = Clipboard::new("bar").try_into_selected().unwrap();
        assert_eq!(clip2.display, DisplayKind::Wayland);

        env::set_var("WSL_DISTRO_NAME", "hanna_montana_linux");
        let clip3 = Clipboard::new("baz").try_into_selected().unwrap();
        assert_eq!(clip3.display, DisplayKind::Wsl);
    }
}