mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 08:45:45 +02:00
Refactor using ClosureEval
types (#12541)
# Description Adds two new types in `nu-engine` for evaluating closures: `ClosureEval` and `ClosureEvalOnce`. This removed some duplicate code and centralizes our logic for setting up, running, and cleaning up closures. For example, in the future if we are able to reduce the cloning necessary to run a closure, then we only have to change the code related to these types. `ClosureEval` and `ClosureEvalOnce` are designed with a builder API. `ClosureEval` is used to run a closure multiple times whereas `ClosureEvalOnce` is used for a one-shot closure. # User-Facing Changes Should be none, unless I messed up one of the command migrations. Actually, this will fix any unreported environment bugs for commands that didn't reset the env after running a closure.
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
use crate::NushellPrompt;
|
||||
use log::trace;
|
||||
use nu_engine::get_eval_subexpression;
|
||||
use nu_engine::ClosureEvalOnce;
|
||||
use nu_protocol::{
|
||||
engine::{EngineState, Stack, StateWorkingSet},
|
||||
report_error, Config, PipelineData, Value,
|
||||
@ -34,17 +34,13 @@ fn get_prompt_string(
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
) -> Option<String> {
|
||||
let eval_subexpression = get_eval_subexpression(engine_state);
|
||||
|
||||
stack
|
||||
.get_env_var(engine_state, prompt)
|
||||
.and_then(|v| match v {
|
||||
Value::Closure { val, .. } => {
|
||||
let block = engine_state.get_block(val.block_id);
|
||||
let mut stack = stack.captures_to_stack(val.captures);
|
||||
// Use eval_subexpression to force a redirection of output, so we can use everything in prompt
|
||||
let ret_val =
|
||||
eval_subexpression(engine_state, &mut stack, block, PipelineData::empty());
|
||||
let result = ClosureEvalOnce::new(engine_state, stack, val)
|
||||
.run_with_input(PipelineData::Empty);
|
||||
|
||||
trace!(
|
||||
"get_prompt_string (block) {}:{}:{}",
|
||||
file!(),
|
||||
@ -52,7 +48,7 @@ fn get_prompt_string(
|
||||
column!()
|
||||
);
|
||||
|
||||
ret_val
|
||||
result
|
||||
.map_err(|err| {
|
||||
let working_set = StateWorkingSet::new(engine_state);
|
||||
report_error(&working_set, &err);
|
||||
|
Reference in New Issue
Block a user