nushell/src/commands/clip.rs

32 lines
905 B
Rust
Raw Normal View History

2019-06-07 18:30:50 +02:00
use crate::commands::command::SinkCommandArgs;
use crate::errors::{labelled, ShellError};
2019-06-07 18:30:50 +02:00
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();
2019-06-07 18:30:50 +02:00
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;
}
let string = i.as_string().map_err(labelled(
args.call_info.name_span,
"Given non-string data",
"expected strings from pipeline",
))?;
new_copy_data.push_str(&string);
2019-06-07 18:30:50 +02:00
}
}
2019-06-07 18:30:50 +02:00
clip_context.set_contents(new_copy_data).unwrap();
Ok(())
}