diff --git a/crates/nu-command/src/commands/platform/mod.rs b/crates/nu-command/src/commands/platform/mod.rs index 2db742f74..9bc013385 100644 --- a/crates/nu-command/src/commands/platform/mod.rs +++ b/crates/nu-command/src/commands/platform/mod.rs @@ -6,6 +6,8 @@ mod clip; mod du; mod exec; mod kill; +#[cfg(feature = "clipboard-cli")] +mod paste; mod pwd; mod run_external; mod sleep; @@ -20,6 +22,8 @@ pub use clip::Clip; pub use du::Du; pub use exec::Exec; pub use kill::Kill; +#[cfg(feature = "clipboard-cli")] +pub use paste::Paste; pub use pwd::Pwd; pub use run_external::RunExternalCommand; pub use sleep::Sleep; diff --git a/crates/nu-command/src/commands/platform/paste.rs b/crates/nu-command/src/commands/platform/paste.rs new file mode 100644 index 000000000..2927a7eb7 --- /dev/null +++ b/crates/nu-command/src/commands/platform/paste.rs @@ -0,0 +1,61 @@ +use crate::prelude::*; + +use nu_engine::WholeStreamCommand; +use nu_errors::ShellError; +use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue}; + +use arboard::Clipboard; + +pub struct Paste; + +impl WholeStreamCommand for Paste { + fn name(&self) -> &str { + "paste" + } + + fn signature(&self) -> Signature { + Signature::build("paste") + } + + fn usage(&self) -> &str { + "Paste contents from the clipboard" + } + + fn run_with_actions(&self, args: CommandArgs) -> Result { + paste(args) + } + + fn examples(&self) -> Vec { + vec![Example { + description: "Paste text from your clipboard", + example: "echo 'secret value' | clip | paste", + result: Some(vec![UntaggedValue::Primitive(Primitive::String( + "secret value".to_owned(), + )) + .into_value(Tag::default())]), + }] + } +} + +pub fn paste(args: CommandArgs) -> Result { + let name = args.call_info.name_tag; + + if let Ok(mut clip_context) = Clipboard::new() { + match clip_context.get_text() { + Ok(out) => Ok(ActionStream::one(ReturnSuccess::value( + UntaggedValue::Primitive(Primitive::String(out)), + ))), + Err(_) => Err(ShellError::labeled_error( + "Could not get contents of clipboard", + "could not get contents of clipboard", + name, + )), + } + } else { + Err(ShellError::labeled_error( + "Could not open clipboard", + "could not open clipboard", + name, + )) + } +} diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 673c25fa2..06142a6c1 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -320,7 +320,10 @@ pub fn create_default_context(interactive: bool) -> Result