move lang command to $nu (#3720)

This commit is contained in:
Darren Schroeder 2021-07-01 13:09:50 -05:00 committed by GitHub
parent 17008bb648
commit 9a1e1d5b1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 33 deletions

View File

@ -6,7 +6,6 @@ mod clip;
mod du;
mod exec;
mod kill;
mod lang;
#[cfg(feature = "clipboard-cli")]
mod paste;
mod pwd;
@ -23,7 +22,6 @@ 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

@ -69,7 +69,6 @@ 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),

View File

@ -1,28 +1,16 @@
use crate::prelude::*;
use crate::Scope;
use indexmap::IndexMap;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Dictionary, Signature, UntaggedValue, Value};
use nu_source::Tag;
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();
impl Lang {
pub fn query_commands(scope: &Scope) -> Result<Vec<Value>, ShellError> {
let tag = Tag::unknown();
let full_commands = scope.get_commands_info();
let mut cmd_vec = Vec::new();
for (key, cmd) in full_commands {
let mut indexmap = IndexMap::new();
let mut sig = cmd.signature();
@ -78,18 +66,10 @@ impl WholeStreamCommand for Lang {
UntaggedValue::string(cmd.extra_usage().to_string()).into_value(&tag),
);
cmd_vec_deque
.push_back(UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag));
cmd_vec.push(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,
}]
Ok(cmd_vec)
}
}

View File

@ -3,6 +3,7 @@ pub(crate) mod evaluate_args;
pub mod evaluator;
pub(crate) mod expr;
pub mod internal;
pub(crate) mod lang;
pub(crate) mod operator;
pub(crate) mod scope;
pub(crate) mod variables;

View File

@ -1,4 +1,7 @@
use crate::{evaluate::scope::Scope, EvaluationContext};
use crate::{
evaluate::{lang, scope::Scope},
EvaluationContext,
};
use indexmap::IndexMap;
use nu_data::config::path::{default_history_path, history_path};
use nu_errors::ShellError;
@ -88,6 +91,12 @@ pub fn nu(scope: &Scope, ctx: &EvaluationContext) -> Result<Value, ShellError> {
);
}
let cmd_info = lang::Lang::query_commands(scope);
match cmd_info {
Ok(cmds) => nu_dict.insert_value("lang", UntaggedValue::table(&cmds).into_value(&tag)),
Err(_) => nu_dict.insert_value("lang", UntaggedValue::string("no commands found")),
}
Ok(nu_dict.into_value())
}