Allow hooks to be lists of blocks (#5480)

This commit is contained in:
JT 2022-05-09 13:56:48 +12:00 committed by GitHub
parent 3a35bf7d4e
commit 54fc164e1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 26 deletions

View file

@ -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),
}
}

View file

@ -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