From 54fc164e1cf4c4b55470ea5a652546a180c23267 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Mon, 9 May 2022 13:56:48 +1200 Subject: [PATCH] Allow hooks to be lists of blocks (#5480) --- crates/nu-cli/src/repl.rs | 57 +++++++++++++++++----------- docs/sample_config/default_config.nu | 8 ++-- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index f8f2ae8798..b5f6689f26 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -12,7 +12,7 @@ use nu_engine::{convert_env_values, eval_block}; use nu_parser::lex; use nu_protocol::{ engine::{EngineState, Stack, StateWorkingSet}, - PipelineData, ShellError, Span, Value, + BlockId, PipelineData, ShellError, Span, Value, }; use reedline::{DefaultHinter, Emacs, Vi}; use std::io::{self, Write}; @@ -197,6 +197,15 @@ pub fn evaluate_repl( info!("prompt_update {}:{}:{}", file!(), line!(), column!()); } + // Right before we start our prompt and take input from the user, + // fire the "pre_prompt" hook + if let Some(hook) = &config.hooks.pre_prompt { + if let Err(err) = run_hook(engine_state, stack, hook) { + let working_set = StateWorkingSet::new(engine_state); + report_error(&working_set, &err); + } + } + let prompt = prompt_update::update_prompt(config, engine_state, stack, &mut nu_prompt, is_perf_true); @@ -211,15 +220,6 @@ pub fn evaluate_repl( ); } - // Right before we start our prompt and take input from the user, - // fire the "pre_prompt" hook - if let Some(hook) = &config.hooks.pre_prompt { - if let Err(err) = run_hook(engine_state, stack, hook) { - let working_set = StateWorkingSet::new(engine_state); - report_error(&working_set, &err); - } - } - let input = line_editor.read_line(prompt); let use_shell_integration = config.shell_integration; @@ -384,22 +384,17 @@ pub fn run_hook( value: &Value, ) -> Result<(), ShellError> { match value { + Value::List { vals, .. } => { + for val in vals { + run_hook(engine_state, stack, val)? + } + Ok(()) + } Value::Block { val: block_id, span, .. - } => { - let block = engine_state.get_block(*block_id); - let input = PipelineData::new(*span); - - match eval_block(engine_state, stack, block, input, false, false) { - Ok(pipeline_data) => match pipeline_data.into_value(*span) { - Value::Error { error } => Err(error), - _ => Ok(()), - }, - Err(err) => Err(err), - } - } + } => run_hook_block(engine_state, stack, *block_id, *span), x => match x.span() { Ok(span) => Err(ShellError::MissingConfigValue( "block for hook in config".into(), @@ -412,3 +407,21 @@ pub fn run_hook( }, } } + +pub fn run_hook_block( + engine_state: &EngineState, + stack: &mut Stack, + block_id: BlockId, + span: Span, +) -> Result<(), ShellError> { + let block = engine_state.get_block(block_id); + let input = PipelineData::new(span); + + match eval_block(engine_state, stack, block, input, false, false) { + Ok(pipeline_data) => match pipeline_data.into_value(span) { + Value::Error { error } => Err(error), + _ => Ok(()), + }, + Err(err) => Err(err), + } +} diff --git a/docs/sample_config/default_config.nu b/docs/sample_config/default_config.nu index cd6945bb59..0f69735d38 100644 --- a/docs/sample_config/default_config.nu +++ b/docs/sample_config/default_config.nu @@ -200,12 +200,12 @@ let-env config = { disable_table_indexes: false # set to true to remove the index column from tables cd_with_abbreviations: false # set to true to allow you to do things like cd s/o/f and nushell expand it to cd some/other/folder hooks: { - pre_prompt: { + pre_prompt: [{ $nothing # replace with source code to run before the prompt is shown - } - pre_execution: { + }] + pre_execution: [{ $nothing # replace with source code to run before the repl input is run - } + }] } menus: [ # Configuration for default nushell menus