Add paste command (#3694)

* Add paste command

* fix build and format failures

* Add examples

* Make tests pass

* Format

* add cfg annotation for Clip

* format code

* remove additional import for clip

* Remove test
This commit is contained in:
Elton Leander Pinto 2021-06-26 16:42:17 -04:00 committed by GitHub
parent 6202c67fe8
commit 8f39f4580a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 1 deletions

View File

@ -6,6 +6,8 @@ mod clip;
mod du; mod du;
mod exec; mod exec;
mod kill; mod kill;
#[cfg(feature = "clipboard-cli")]
mod paste;
mod pwd; mod pwd;
mod run_external; mod run_external;
mod sleep; mod sleep;
@ -20,6 +22,8 @@ pub use clip::Clip;
pub use du::Du; pub use du::Du;
pub use exec::Exec; pub use exec::Exec;
pub use kill::Kill; pub use kill::Kill;
#[cfg(feature = "clipboard-cli")]
pub use paste::Paste;
pub use pwd::Pwd; pub use pwd::Pwd;
pub use run_external::RunExternalCommand; pub use run_external::RunExternalCommand;
pub use sleep::Sleep; pub use sleep::Sleep;

View File

@ -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<ActionStream, ShellError> {
paste(args)
}
fn examples(&self) -> Vec<Example> {
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<ActionStream, ShellError> {
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,
))
}
}

View File

@ -320,7 +320,10 @@ pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Bo
#[cfg(feature = "clipboard-cli")] #[cfg(feature = "clipboard-cli")]
{ {
context.add_commands(vec![whole_stream_command(crate::commands::Clip)]); context.add_commands(vec![
whole_stream_command(crate::commands::Clip),
whole_stream_command(crate::commands::Paste),
]);
} }
} }