forked from dathere/qsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipboard.rs
84 lines (70 loc) · 2.77 KB
/
clipboard.rs
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
static USAGE: &str = r#"
Provide input from the clipboard or save output to the clipboard.
Note when saving to clipboard on Windows, line breaks may be represented as \r\n (CRLF).
Meanwhile on Linux and macOS, they may be represented as \n (LF).
Examples:
Pipe into qsv stats using qsv clipboard and render it as a table:
qsv clipboard | qsv stats | qsv table
If you want to save the output of a command to the clipboard,
pipe into qsv clipboard using the --save or -s flag:
qsv clipboard | qsv stats | qsv clipboard -s
Usage:
qsv clipboard [options]
qsv clipboard --help
clip options:
-s, --save Save output to clipboard.
Common options:
-h, --help Display this message
"#;
use std::io::Read;
use arboard::Clipboard;
use serde::Deserialize;
use crate::{util, CliError, CliResult};
#[allow(dead_code)]
#[derive(Deserialize)]
struct Args {
flag_save: bool,
}
impl From<arboard::Error> for CliError {
fn from(err: arboard::Error) -> Self {
match err {
arboard::Error::ClipboardNotSupported => CliError::Other(
"The clipboard may not be supported for the current environment.".to_string(),
),
arboard::Error::ConversionFailure => CliError::Other(
"The content that was about the be transferred to/from the clipboard could not be \
converted to the appropriate format."
.to_string(),
),
arboard::Error::ClipboardOccupied => CliError::Other(
"The clipboard was unaccessible when attempting to access/use it. It may have \
been held by another process/thread."
.to_string(),
),
arboard::Error::ContentNotAvailable => CliError::Other(
"The clipboard contents were not available in the requested format. This could \
either be due to the clipboard being empty or the clipboard contents having an \
incompatible format to the requested one."
.to_string(),
),
arboard::Error::Unknown { description: _ } => CliError::Other(
"An unknown error occurred while attempting to use the clipboard.".to_string(),
),
_ => CliError::Other(
"An unexpected error occurred while using the clipboard.".to_string(),
),
}
}
}
pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let mut clipboard = Clipboard::new()?;
if args.flag_save {
let mut buffer = String::new();
std::io::stdin().read_to_string(&mut buffer)?;
clipboard.set_text(buffer).unwrap();
} else {
print!("{}", clipboard.get_text().unwrap());
}
Ok(())
}