Update config directly at assignment (#13332)

# Description

Allows `Stack` to have a modified local `Config`, which is updated
immediately when `$env.config` is assigned to. This means that even
within a script, commands that come after `$env.config` changes will
always see those changes in `Stack::get_config()`.

Also fixed a lot of cases where `engine_state.get_config()` was used
even when `Stack` was available.

Closes #13324.

# User-Facing Changes
- Config changes apply immediately after the assignment is executed,
rather than whenever config is read by a command that needs it.
- Potentially slower performance when executing a lot of lines that
change `$env.config` one after another. Recommended to get `$env.config`
into a `mut` variable first and do modifications, then assign it back.
- Much faster performance when executing a script that made
modifications to `$env.config`, as the changes are only parsed once.

# Tests + Formatting
All passing.

# After Submitting
- [ ] release notes
This commit is contained in:
Devyn Cairns
2024-07-11 06:09:33 -07:00
committed by GitHub
parent deaa711ca6
commit f65bc97a54
46 changed files with 327 additions and 222 deletions

View File

@@ -1,7 +1,7 @@
use itertools::Itertools;
use nu_engine::command_prelude::*;
use nu_protocol::Range;
use std::{io::Cursor, iter::Peekable, str::CharIndices};
use nu_protocol::{Config, Range};
use std::{io::Cursor, iter::Peekable, str::CharIndices, sync::Arc};
type Input<'t> = Peekable<CharIndices<'t>>;
@@ -110,11 +110,13 @@ none 8150224 4 8150220 1% /mnt/c' | detect columns --gue
let num_rows_to_skip: Option<usize> = call.get_flag(engine_state, stack, "skip")?;
let noheader = call.has_flag(engine_state, stack, "no-headers")?;
let range: Option<Range> = call.get_flag(engine_state, stack, "combine-columns")?;
let config = stack.get_config(engine_state);
let args = Arguments {
noheader,
num_rows_to_skip,
range,
config,
};
if call.has_flag(engine_state, stack, "guess")? {
@@ -133,11 +135,13 @@ none 8150224 4 8150220 1% /mnt/c' | detect columns --gue
let num_rows_to_skip: Option<usize> = call.get_flag_const(working_set, "skip")?;
let noheader = call.has_flag_const(working_set, "no-headers")?;
let range: Option<Range> = call.get_flag_const(working_set, "combine-columns")?;
let config = working_set.get_config().clone();
let args = Arguments {
noheader,
num_rows_to_skip,
range,
config,
};
if call.has_flag_const(working_set, "guess")? {
@@ -152,6 +156,7 @@ struct Arguments {
num_rows_to_skip: Option<usize>,
noheader: bool,
range: Option<Range>,
config: Arc<Config>,
}
fn guess_width(
@@ -163,7 +168,7 @@ fn guess_width(
use super::guess_width::GuessWidth;
let input_span = input.span().unwrap_or(call.head);
let mut input = input.collect_string("", engine_state.get_config())?;
let mut input = input.collect_string("", &args.config)?;
if let Some(rows) = args.num_rows_to_skip {
input = input.lines().skip(rows).map(|x| x.to_string()).join("\n");
}
@@ -235,8 +240,7 @@ fn detect_columns(
args: Arguments,
) -> Result<PipelineData, ShellError> {
let name_span = call.head;
let config = engine_state.get_config();
let input = input.collect_string("", config)?;
let input = input.collect_string("", &args.config)?;
let input: Vec<_> = input
.lines()