a new command to query the nushell internals (#3704)

* a new command to query the nushell internals

* added signature

* a little cleanup
This commit is contained in:
Darren Schroeder
2021-06-29 09:27:16 -05:00
committed by GitHub
parent 1d0483c946
commit 008bdfa43f
13 changed files with 379 additions and 16 deletions

View File

@ -44,11 +44,11 @@ fn help(args: CommandArgs) -> Result<ActionStream, ShellError> {
let (mut subcommand_names, command_names) = sorted_names
.into_iter()
// Internal only commands shouldn't be displayed
// private only commands shouldn't be displayed
.filter(|cmd_name| {
scope
.get_command(cmd_name)
.filter(|command| !command.is_internal())
.filter(|command| !command.is_private())
.is_some()
})
.partition::<Vec<_>, _>(|cmd_name| cmd_name.contains(' '));

View File

@ -0,0 +1,229 @@
use crate::prelude::*;
use indexmap::IndexMap;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Dictionary, Signature, UntaggedValue, Value};
pub struct Lang;
impl WholeStreamCommand for Lang {
fn name(&self) -> &str {
"lang"
}
fn signature(&self) -> Signature {
Signature::build("lang")
}
fn usage(&self) -> &str {
"Returns the nushell-lang information"
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let full_commands = args.context.scope.get_commands_info();
let mut cmd_vec_deque = VecDeque::new();
for (key, cmd) in full_commands {
let mut indexmap = IndexMap::new();
let mut sig = cmd.signature();
// eprintln!("{}", get_signature(&sig));
indexmap.insert(
"name".to_string(),
UntaggedValue::string(key).into_value(&tag),
);
indexmap.insert(
"usage".to_string(),
UntaggedValue::string(cmd.usage().to_string()).into_value(&tag),
);
// let sig_deser = serde_json::to_string(&sig).unwrap();
// indexmap.insert(
// "signature".to_string(),
// UntaggedValue::string(sig_deser).into_value(&tag),
// );
let signature_table = get_signature(&mut sig, tag.clone());
indexmap.insert(
"signature".to_string(),
UntaggedValue::Table(signature_table).into_value(&tag),
);
indexmap.insert(
"is_filter".to_string(),
UntaggedValue::boolean(sig.is_filter).into_value(&tag),
);
indexmap.insert(
"is_builtin".to_string(),
UntaggedValue::boolean(cmd.is_builtin()).into_value(&tag),
);
indexmap.insert(
"is_sub".to_string(),
UntaggedValue::boolean(cmd.is_sub()).into_value(&tag),
);
indexmap.insert(
"is_plugin".to_string(),
UntaggedValue::boolean(cmd.is_plugin()).into_value(&tag),
);
indexmap.insert(
"is_custom".to_string(),
UntaggedValue::boolean(cmd.is_custom()).into_value(&tag),
);
indexmap.insert(
"is_private".to_string(),
UntaggedValue::boolean(cmd.is_private()).into_value(&tag),
);
indexmap.insert(
"is_binary".to_string(),
UntaggedValue::boolean(cmd.is_binary()).into_value(&tag),
);
indexmap.insert(
"extra_usage".to_string(),
UntaggedValue::string(cmd.extra_usage().to_string()).into_value(&tag),
);
cmd_vec_deque
.push_back(UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag));
}
Ok(cmd_vec_deque.into_iter().into_output_stream())
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Query command information from Nushell",
example: "lang",
result: None,
}]
}
}
fn get_signature(sig: &mut Signature, tag: Tag) -> Vec<Value> {
sig.remove_named("help");
let p = &sig.positional;
let r = &sig.rest_positional;
let n = &sig.named;
let name = &sig.name;
let mut sig_vec: Vec<Value> = Vec::new();
for item in p {
let mut indexmap = IndexMap::new();
let (parameter, syntax_shape) = item.0.get_type_description();
let description = &item.1;
// let output = format!(
// "Positional|{}|{}|{}|{}\n",
// name, parameter, syntax_shape, description
// );
// eprintln!("{}", output);
indexmap.insert(
"cmd_name".to_string(),
UntaggedValue::string(name).into_value(&tag),
);
indexmap.insert(
"parameter_name".to_string(),
UntaggedValue::string(parameter).into_value(&tag),
);
indexmap.insert(
"parameter_type".to_string(),
UntaggedValue::string("positional".to_string()).into_value(&tag),
);
indexmap.insert(
"syntax_shape".to_string(),
UntaggedValue::string(syntax_shape).into_value(&tag),
);
indexmap.insert(
"description".to_string(),
UntaggedValue::string(description).into_value(&tag),
);
indexmap.insert(
"flag_name".to_string(),
UntaggedValue::string("".to_string()).into_value(&tag),
);
indexmap.insert(
"flag_type".to_string(),
UntaggedValue::string("".to_string()).into_value(&tag),
);
sig_vec.push(UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag));
}
match r {
Some((shape, desc)) => {
let mut indexmap = IndexMap::new();
// let output = format!("Rest|{}|{}|{}\n", name, shape.syntax_shape_name(), desc);
// eprintln!("{}", output);
indexmap.insert(
"cmd_name".to_string(),
UntaggedValue::string(name).into_value(&tag),
);
indexmap.insert(
"parameter_name".to_string(),
UntaggedValue::string("".to_string()).into_value(&tag),
);
indexmap.insert(
"parameter_type".to_string(),
UntaggedValue::string("rest".to_string()).into_value(&tag),
);
indexmap.insert(
"syntax_shape".to_string(),
UntaggedValue::string(shape.syntax_shape_name()).into_value(&tag),
);
indexmap.insert(
"description".to_string(),
UntaggedValue::string(desc).into_value(&tag),
);
indexmap.insert(
"flag_name".to_string(),
UntaggedValue::string("".to_string()).into_value(&tag),
);
indexmap.insert(
"flag_type".to_string(),
UntaggedValue::string("".to_string()).into_value(&tag),
);
sig_vec.push(UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag));
}
None => {}
}
for (parameter, (b, description)) in n {
let mut indexmap = IndexMap::new();
let (named_type, flag_name, shape) = b.get_type_description();
// let output = format!(
// "Named|{}|{}|{}|{}|{}|{}\n",
// name, parameter, named_type, flag_name, shape, description
// );
// eprint!("{}", output);
indexmap.insert(
"cmd_name".to_string(),
UntaggedValue::string(name).into_value(&tag),
);
indexmap.insert(
"parameter_name".to_string(),
UntaggedValue::string(parameter).into_value(&tag),
);
indexmap.insert(
"parameter_type".to_string(),
UntaggedValue::string("named".to_string()).into_value(&tag),
);
indexmap.insert(
"syntax_shape".to_string(),
UntaggedValue::string(shape).into_value(&tag),
);
indexmap.insert(
"description".to_string(),
UntaggedValue::string(description).into_value(&tag),
);
indexmap.insert(
"flag_name".to_string(),
UntaggedValue::string(flag_name).into_value(&tag),
);
indexmap.insert(
"flag_type".to_string(),
UntaggedValue::string(named_type).into_value(&tag),
);
sig_vec.push(UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag));
}
sig_vec
}

View File

@ -6,6 +6,7 @@ mod clip;
mod du;
mod exec;
mod kill;
mod lang;
#[cfg(feature = "clipboard-cli")]
mod paste;
mod pwd;
@ -22,6 +23,7 @@ pub use clip::Clip;
pub use du::Du;
pub use exec::Exec;
pub use kill::Kill;
pub use lang::Lang;
#[cfg(feature = "clipboard-cli")]
pub use paste::Paste;
pub use pwd::Pwd;

View File

@ -58,7 +58,7 @@ impl WholeStreamCommand for RunExternalCommand {
}]
}
fn is_internal(&self) -> bool {
fn is_private(&self) -> bool {
true
}

View File

@ -69,6 +69,7 @@ pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Bo
whole_stream_command(Benchmark),
// Metadata
whole_stream_command(Tags),
whole_stream_command(Lang),
// Shells
whole_stream_command(Next),
whole_stream_command(Previous),