Add the ability to remove and list aliases (#3879)

* Add the ability to remove and list aliases

* Fix failing unit tests

* Add a test to check unalias shadowing blocks
This commit is contained in:
soumil-07
2021-08-17 19:26:35 +05:30
committed by GitHub
parent 2b7390c2a1
commit 9bd408449e
9 changed files with 110 additions and 16 deletions

View File

@ -2,7 +2,7 @@ use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape};
use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
pub struct Alias;
@ -35,6 +35,18 @@ impl WholeStreamCommand for Alias {
}
}
pub fn alias(_: CommandArgs) -> Result<OutputStream, ShellError> {
pub fn alias(args: CommandArgs) -> Result<OutputStream, ShellError> {
// TODO: is there a better way of checking whether no arguments were passed?
if args.nth(0).is_none() {
let aliases = UntaggedValue::string(
&args
.scope()
.get_aliases()
.iter()
.map(|val| format!("{} = '{}'", val.0, val.1.iter().map(|x| &x.item).join(" ")))
.join("\n"),
);
return Ok(OutputStream::one(aliases));
}
Ok(OutputStream::empty())
}

View File

@ -13,6 +13,7 @@ mod nu_plugin;
mod nu_signature;
mod source;
mod tags;
mod unalias;
mod version;
pub use self::nu_plugin::SubCommand as NuPlugin;
@ -32,4 +33,5 @@ pub use ignore::Ignore;
pub use let_::Let;
pub use source::Source;
pub use tags::Tags;
pub use unalias::Unalias;
pub use version::{version, Version};

View File

@ -0,0 +1,37 @@
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape};
pub struct Unalias;
impl WholeStreamCommand for Unalias {
fn name(&self) -> &str {
"unalias"
}
fn signature(&self) -> Signature {
Signature::build("unalias").required("name", SyntaxShape::String, "the name of the alias")
}
fn usage(&self) -> &str {
"Removes an alias"
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
unalias(args)
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Remove the 'v' alias",
example: "unalias v",
result: None,
}]
}
}
pub fn unalias(_: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(OutputStream::empty())
}