nushell/src/commands/clip.rs

32 lines
1018 B
Rust
Raw Normal View History

2019-06-07 18:30:50 +02:00
use crate::commands::command::SinkCommandArgs;
use crate::errors::ShellError;
use clipboard::{ClipboardContext, ClipboardProvider};
2019-06-07 18:46:47 +02:00
pub fn clip(args: SinkCommandArgs) -> Result<(), ShellError> {
2019-06-07 18:30:50 +02:00
let mut clip_context: ClipboardContext = ClipboardProvider::new().unwrap();
let mut new_copy_data = String::new();
if args.input.len() > 0 {
let mut first = true;
for i in args.input.iter() {
if !first {
new_copy_data.push_str("\n");
} else {
first = false;
}
2019-06-16 01:03:49 +02:00
match i.as_string() {
Ok(s) => new_copy_data.push_str(&s),
Err(_) => {
return Err(ShellError::maybe_labeled_error(
"Given non-string data",
"expected strings from pipeline",
args.name_span,
))
}
}
2019-06-07 18:30:50 +02:00
}
}
clip_context.set_contents(new_copy_data).unwrap();
Ok(())
}