2020-05-18 14:56:01 +02:00
|
|
|
use crate::prelude::*;
|
2021-04-06 18:19:43 +02:00
|
|
|
|
2021-01-10 03:50:49 +01:00
|
|
|
use nu_engine::WholeStreamCommand;
|
2020-05-18 14:56:01 +02:00
|
|
|
use nu_errors::ShellError;
|
2020-05-30 01:36:04 +02:00
|
|
|
use nu_protocol::{Signature, Value};
|
2019-08-30 00:52:32 +02:00
|
|
|
|
2020-12-30 18:16:02 +01:00
|
|
|
use arboard::Clipboard;
|
2019-08-30 00:52:32 +02:00
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
pub struct Clip;
|
2019-08-30 00:52:32 +02:00
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
impl WholeStreamCommand for Clip {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"clip"
|
|
|
|
}
|
2020-05-12 03:00:55 +02:00
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("clip")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2021-03-08 00:57:58 +01:00
|
|
|
"Copy the contents of the pipeline to the copy/paste buffer."
|
2019-08-23 05:29:08 +02:00
|
|
|
}
|
2019-08-02 21:15:07 +02:00
|
|
|
|
2021-04-06 18:19:43 +02:00
|
|
|
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
clip(args)
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|
2019-08-02 21:15:07 +02:00
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
fn examples(&self) -> Vec<Example> {
|
2020-10-01 07:22:19 +02:00
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Save text to the clipboard",
|
|
|
|
example: "echo 'secret value' | clip",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Save numbers to the clipboard",
|
|
|
|
example: "random integer 10000000..99999999 | clip",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
]
|
2019-08-23 05:29:08 +02:00
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|
2019-07-03 22:31:15 +02:00
|
|
|
|
2021-04-06 18:19:43 +02:00
|
|
|
pub fn clip(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2020-05-30 01:36:04 +02:00
|
|
|
let input = args.input;
|
|
|
|
let name = args.call_info.name_tag.clone();
|
2021-04-06 18:19:43 +02:00
|
|
|
let values: Vec<Value> = input.collect();
|
2019-07-03 22:31:15 +02:00
|
|
|
|
2020-12-30 18:16:02 +01:00
|
|
|
if let Ok(mut clip_context) = Clipboard::new() {
|
2020-05-18 14:56:01 +02:00
|
|
|
let mut new_copy_data = String::new();
|
|
|
|
|
2020-05-30 01:36:04 +02:00
|
|
|
if !values.is_empty() {
|
2020-05-18 14:56:01 +02:00
|
|
|
let mut first = true;
|
2020-05-30 01:36:04 +02:00
|
|
|
for i in values.iter() {
|
2020-05-18 14:56:01 +02:00
|
|
|
if !first {
|
2020-11-22 01:37:16 +01:00
|
|
|
new_copy_data.push('\n');
|
2020-05-18 14:56:01 +02:00
|
|
|
} else {
|
|
|
|
first = false;
|
2020-01-03 22:11:21 +01:00
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
2020-10-01 07:22:19 +02:00
|
|
|
let string: String = i.convert_to_string();
|
|
|
|
if string.is_empty() {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Unable to convert to string",
|
|
|
|
"Unable to convert to string",
|
|
|
|
name,
|
|
|
|
));
|
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
new_copy_data.push_str(&string);
|
2020-01-03 22:11:21 +01:00
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|
2019-06-07 18:30:50 +02:00
|
|
|
|
2020-12-30 18:16:02 +01:00
|
|
|
match clip_context.set_text(new_copy_data) {
|
2020-05-18 14:56:01 +02:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(_) => {
|
2020-05-30 01:36:04 +02:00
|
|
|
return Err(ShellError::labeled_error(
|
2020-05-18 14:56:01 +02:00
|
|
|
"Could not set contents of clipboard",
|
|
|
|
"could not set contents of clipboard",
|
|
|
|
name,
|
2020-05-30 01:36:04 +02:00
|
|
|
));
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|
2020-01-03 22:11:21 +01:00
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
} else {
|
2020-05-30 01:36:04 +02:00
|
|
|
return Err(ShellError::labeled_error(
|
2020-05-18 14:56:01 +02:00
|
|
|
"Could not open clipboard",
|
|
|
|
"could not open clipboard",
|
|
|
|
name,
|
2020-05-30 01:36:04 +02:00
|
|
|
));
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|
2020-05-30 01:36:04 +02:00
|
|
|
Ok(OutputStream::empty())
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Clip;
|
2020-10-03 16:06:02 +02:00
|
|
|
use super::ShellError;
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-10-03 16:06:02 +02:00
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2020-05-18 14:56:01 +02:00
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2021-02-12 11:13:14 +01:00
|
|
|
test_examples(Clip {})
|
2019-08-23 05:29:08 +02:00
|
|
|
}
|
2019-06-07 18:30:50 +02:00
|
|
|
}
|