diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index e4f5191827..fbaaaf537b 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -5,7 +5,7 @@ use nu_protocol::{ Signature, SyntaxShape, }; -use crate::{Alias, Benchmark, BuildString, Def, Each, For, If, Length, Let, LetEnv}; +use crate::{Alias, Benchmark, BuildString, Def, Do, Each, For, If, Length, Let, LetEnv}; pub fn create_default_context() -> Rc> { let engine_state = Rc::new(RefCell::new(EngineState::new())); @@ -33,6 +33,8 @@ pub fn create_default_context() -> Rc> { working_set.add_decl(Box::new(Each)); + working_set.add_decl(Box::new(Do)); + working_set.add_decl(Box::new(Benchmark)); working_set.add_decl(Box::new(Length)); diff --git a/crates/nu-command/src/do_.rs b/crates/nu-command/src/do_.rs new file mode 100644 index 0000000000..dba61b6024 --- /dev/null +++ b/crates/nu-command/src/do_.rs @@ -0,0 +1,40 @@ +use nu_engine::{eval_block, eval_expression}; +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EvaluationContext}; +use nu_protocol::{Signature, SyntaxShape, Value}; + +pub struct Do; + +impl Command for Do { + fn name(&self) -> &str { + "do" + } + + fn usage(&self) -> &str { + "Run a block" + } + + fn signature(&self) -> nu_protocol::Signature { + Signature::build("do").required("block", SyntaxShape::Block, "the block to run") + } + + fn run( + &self, + context: &EvaluationContext, + call: &Call, + input: Value, + ) -> Result { + let block = &call.positional[0]; + + let out = eval_expression(context, &block)?; + + match out { + Value::Block { val: block_id, .. } => { + let engine_state = context.engine_state.borrow(); + let block = engine_state.get_block(block_id); + eval_block(context, block, input) + } + _ => Ok(Value::nothing()), + } + } +} diff --git a/crates/nu-command/src/lib.rs b/crates/nu-command/src/lib.rs index 1df25ef519..3e7d99e7d7 100644 --- a/crates/nu-command/src/lib.rs +++ b/crates/nu-command/src/lib.rs @@ -3,6 +3,7 @@ mod benchmark; mod build_string; mod def; mod default_context; +mod do_; mod each; mod for_; mod if_; @@ -15,6 +16,7 @@ pub use benchmark::Benchmark; pub use build_string::BuildString; pub use def::Def; pub use default_context::create_default_context; +pub use do_::Do; pub use each::Each; pub use for_::For; pub use if_::If;