2020-05-11 20:05:44 +00:00
|
|
|
use crate::commands::{Example, WholeStreamCommand};
|
2020-04-15 05:43:23 +00:00
|
|
|
use crate::context::CommandRegistry;
|
|
|
|
use crate::prelude::*;
|
|
|
|
use nu_errors::ShellError;
|
2020-04-27 02:04:54 +00:00
|
|
|
use nu_protocol::{hir::Block, CommandAction, ReturnSuccess, Signature, SyntaxShape, Value};
|
|
|
|
use nu_source::Tagged;
|
2020-04-15 05:43:23 +00:00
|
|
|
|
|
|
|
pub struct Alias;
|
|
|
|
|
2020-04-27 02:04:54 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct AliasArgs {
|
|
|
|
pub name: Tagged<String>,
|
|
|
|
pub args: Vec<Value>,
|
|
|
|
pub block: Block,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WholeStreamCommand for Alias {
|
2020-04-15 05:43:23 +00:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"alias"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("alias")
|
|
|
|
.required("name", SyntaxShape::String, "the name of the alias")
|
|
|
|
.required("args", SyntaxShape::Table, "the arguments to the alias")
|
2020-05-09 17:16:14 +00:00
|
|
|
.required(
|
|
|
|
"block",
|
|
|
|
SyntaxShape::Block,
|
|
|
|
"the block to run as the body of the alias",
|
|
|
|
)
|
2020-04-15 05:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2020-05-09 17:16:14 +00:00
|
|
|
"Define a shortcut for another command."
|
2020-04-15 05:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
2020-04-27 02:04:54 +00:00
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
2020-04-15 05:43:23 +00:00
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-04-27 02:04:54 +00:00
|
|
|
args.process(registry, alias)?.run()
|
2020-04-15 05:43:23 +00:00
|
|
|
}
|
2020-05-11 20:05:44 +00:00
|
|
|
|
|
|
|
fn examples(&self) -> &[Example] {
|
|
|
|
&[Example {
|
|
|
|
description: "Some people prefer to write one letter instead of two",
|
|
|
|
example: "alias l [x] { ls $x }",
|
|
|
|
}]
|
|
|
|
}
|
2020-04-15 05:43:23 +00:00
|
|
|
}
|
2020-04-27 02:04:54 +00:00
|
|
|
|
|
|
|
pub fn alias(
|
|
|
|
AliasArgs {
|
|
|
|
name,
|
|
|
|
args: list,
|
|
|
|
block,
|
|
|
|
}: AliasArgs,
|
|
|
|
_: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let stream = async_stream! {
|
|
|
|
let mut args: Vec<String> = vec![];
|
|
|
|
for item in list.iter() {
|
|
|
|
if let Ok(string) = item.as_string() {
|
|
|
|
args.push(format!("${}", string));
|
|
|
|
} else {
|
|
|
|
yield Err(ShellError::labeled_error("Expected a string", "expected a string", item.tag()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
yield ReturnSuccess::action(CommandAction::AddAlias(name.to_string(), args, block.clone()))
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|