mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
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:
parent
6202c67fe8
commit
8f39f4580a
@ -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;
|
||||
|
61
crates/nu-command/src/commands/platform/paste.rs
Normal file
61
crates/nu-command/src/commands/platform/paste.rs
Normal 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,
|
||||
))
|
||||
}
|
||||
}
|
@ -320,7 +320,10 @@ pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Bo
|
||||
|
||||
#[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),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user