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

@ -2,7 +2,7 @@ pub(crate) mod block;
pub(crate) mod evaluate_args;
pub mod evaluator;
pub(crate) mod expr;
pub(crate) mod internal;
pub mod internal;
pub(crate) mod operator;
pub(crate) mod scope;
pub(crate) mod variables;

View File

@ -65,6 +65,20 @@ impl Scope {
output.sorted_by(|k1, _v1, k2, _v2| k1.cmp(k2)).collect()
}
pub fn get_commands_info(&self) -> IndexMap<String, Command> {
let mut output: IndexMap<String, Command> = IndexMap::new();
for frame in self.frames.lock().iter().rev() {
for (name, command) in frame.commands.iter() {
if !output.contains_key(name) {
output.insert(name.clone(), command.clone());
}
}
}
output.sorted_by(|k1, _v1, k2, _v2| k1.cmp(k2)).collect()
}
pub fn get_variable_names(&self) -> Vec<String> {
self.get_vars().iter().map(|(k, _)| k.to_string()).collect()
}

View File

@ -3,7 +3,7 @@ mod command_args;
mod config_holder;
pub mod documentation;
mod env;
mod evaluate;
pub mod evaluate;
pub mod evaluation_context;
mod example;
pub mod filesystem;

View File

@ -111,9 +111,21 @@ impl WholeStreamCommand for PluginFilter {
&self.config.usage
}
fn extra_usage(&self) -> &str {
&self.config.extra_usage
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
run_filter(self.path.clone(), args)
}
fn is_plugin(&self) -> bool {
true
}
fn is_builtin(&self) -> bool {
false
}
}
fn run_filter(path: String, args: CommandArgs) -> Result<ActionStream, ShellError> {
@ -383,9 +395,21 @@ impl WholeStreamCommand for PluginSink {
&self.config.usage
}
fn extra_usage(&self) -> &str {
&self.config.extra_usage
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
run_sink(self.path.clone(), args)
}
fn is_plugin(&self) -> bool {
true
}
fn is_builtin(&self) -> bool {
false
}
}
fn run_sink(path: String, args: CommandArgs) -> Result<ActionStream, ShellError> {

View File

@ -47,19 +47,38 @@ pub trait WholeStreamCommand: Send + Sync {
}
// Commands that are not meant to be run by users
fn is_internal(&self) -> bool {
fn is_private(&self) -> bool {
false
}
fn examples(&self) -> Vec<Example> {
Vec::new()
}
// This is a built-in command
fn is_builtin(&self) -> bool {
true
}
// Is a sub command
fn is_sub(&self) -> bool {
self.name().contains(' ')
}
// Is a plugin command
fn is_plugin(&self) -> bool {
false
}
// Is a custom command i.e. def blah [] { }
fn is_custom(&self) -> bool {
false
}
}
// Custom commands are blocks, so we can use the information in the block to also
// implement a WholeStreamCommand
#[allow(clippy::suspicious_else_formatting)]
impl WholeStreamCommand for Arc<Block> {
fn name(&self) -> &str {
&self.params.name
@ -73,6 +92,10 @@ impl WholeStreamCommand for Arc<Block> {
&self.params.usage
}
fn extra_usage(&self) -> &str {
&self.params.extra_usage
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let call_info = args.call_info.clone();
@ -184,13 +207,21 @@ impl WholeStreamCommand for Arc<Block> {
false
}
fn is_internal(&self) -> bool {
fn is_private(&self) -> bool {
false
}
fn examples(&self) -> Vec<Example> {
vec![]
}
fn is_custom(&self) -> bool {
true
}
fn is_builtin(&self) -> bool {
false
}
}
#[derive(Clone)]
@ -228,6 +259,10 @@ impl Command {
self.0.usage()
}
pub fn extra_usage(&self) -> &str {
self.0.extra_usage()
}
pub fn examples(&self) -> Vec<Example> {
self.0.examples()
}
@ -260,13 +295,29 @@ impl Command {
self.0.is_binary()
}
pub fn is_internal(&self) -> bool {
self.0.is_internal()
pub fn is_private(&self) -> bool {
self.0.is_private()
}
pub fn stream_command(&self) -> &dyn WholeStreamCommand {
&*self.0
}
pub fn is_builtin(&self) -> bool {
self.0.is_builtin()
}
pub fn is_sub(&self) -> bool {
self.0.is_sub()
}
pub fn is_plugin(&self) -> bool {
self.0.is_plugin()
}
pub fn is_custom(&self) -> bool {
self.0.is_custom()
}
}
pub fn whole_stream_command(command: impl WholeStreamCommand + 'static) -> Command {