From 76079d518354922724ebb775e886d879fb4f7fee Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Tue, 19 Apr 2022 10:28:01 +1200 Subject: [PATCH] Move config to be an env var (#5230) * Move config to be an env var * fix fmt and tests --- crates/nu-cli/src/commands.rs | 12 +- crates/nu-cli/src/completions/completer.rs | 7 +- .../src/completions/custom_completions.rs | 26 +- crates/nu-cli/src/nu_highlight.rs | 4 +- crates/nu-cli/src/repl.rs | 46 +--- crates/nu-cli/src/util.rs | 6 +- .../nu-command/src/conversions/into/string.rs | 2 +- crates/nu-command/src/core_commands/debug.rs | 4 +- .../src/dataframe/test_dataframe.rs | 12 +- crates/nu-command/src/example_test.rs | 12 +- crates/nu-command/src/filesystem/open.rs | 21 +- crates/nu-command/src/filesystem/rm.rs | 2 +- crates/nu-command/src/filters/find.rs | 6 +- crates/nu-command/src/filters/headers.rs | 8 +- crates/nu-command/src/formats/from/csv.rs | 4 +- crates/nu-command/src/formats/from/eml.rs | 4 +- crates/nu-command/src/formats/from/ics.rs | 8 +- crates/nu-command/src/formats/from/ini.rs | 8 +- crates/nu-command/src/formats/from/json.rs | 6 +- crates/nu-command/src/formats/from/nuon.rs | 8 +- crates/nu-command/src/formats/from/ssv.rs | 4 +- crates/nu-command/src/formats/from/toml.rs | 8 +- crates/nu-command/src/formats/from/tsv.rs | 8 +- crates/nu-command/src/formats/from/url.rs | 8 +- crates/nu-command/src/formats/from/vcf.rs | 8 +- crates/nu-command/src/formats/from/xml.rs | 8 +- crates/nu-command/src/formats/from/yaml.rs | 16 +- crates/nu-command/src/formats/to/csv.rs | 4 +- crates/nu-command/src/formats/to/delimited.rs | 4 +- crates/nu-command/src/formats/to/html.rs | 10 +- crates/nu-command/src/formats/to/md.rs | 16 +- crates/nu-command/src/formats/to/tsv.rs | 8 +- crates/nu-command/src/formats/to/xml.rs | 4 +- crates/nu-command/src/platform/ansi/ansi_.rs | 2 +- .../reedline_commands/keybindings_listen.rs | 12 +- crates/nu-command/src/strings/build_string.rs | 4 +- .../nu-command/src/strings/detect_columns.rs | 4 +- .../nu-command/src/strings/format/command.rs | 4 +- crates/nu-command/src/strings/str_/collect.rs | 4 +- crates/nu-command/src/system/run_external.rs | 13 +- crates/nu-command/src/viewers/griddle.rs | 8 +- crates/nu-command/src/viewers/table.rs | 19 +- crates/nu-engine/src/eval.rs | 16 +- crates/nu-parser/src/parse_keywords.rs | 6 +- crates/nu-parser/src/parser.rs | 22 +- crates/nu-protocol/src/engine/engine_state.rs | 21 +- crates/nu-protocol/src/engine/stack.rs | 50 ++-- crates/nu-protocol/src/lib.rs | 2 +- docs/sample_config/default_config.nu | 259 ------------------ docs/sample_config/default_env.nu | 259 ++++++++++++++++++ src/main.rs | 19 +- src/tests/test_engine.rs | 27 -- 52 files changed, 455 insertions(+), 608 deletions(-) diff --git a/crates/nu-cli/src/commands.rs b/crates/nu-cli/src/commands.rs index a83a16dae7..313202e6a0 100644 --- a/crates/nu-cli/src/commands.rs +++ b/crates/nu-cli/src/commands.rs @@ -6,7 +6,7 @@ use nu_parser::{parse, trim_quotes}; use nu_protocol::engine::Stack; use nu_protocol::{ engine::{EngineState, StateDelta, StateWorkingSet}, - Config, PipelineData, Spanned, + PipelineData, Spanned, }; use std::path::Path; @@ -46,15 +46,7 @@ pub fn evaluate_commands( report_error(&working_set, &err); } - let config = match stack.get_config() { - Ok(config) => config, - Err(e) => { - let working_set = StateWorkingSet::new(engine_state); - - report_error(&working_set, &e); - Config::default() - } - }; + let config = engine_state.get_config().clone(); // Merge the delta in case env vars changed in the config match nu_engine::env::current_dir(engine_state, stack) { diff --git a/crates/nu-cli/src/completions/completer.rs b/crates/nu-cli/src/completions/completer.rs index b31801390d..61af34baed 100644 --- a/crates/nu-cli/src/completions/completer.rs +++ b/crates/nu-cli/src/completions/completer.rs @@ -5,7 +5,7 @@ use crate::completions::{ use nu_parser::{flatten_expression, parse, FlatShape}; use nu_protocol::{ engine::{EngineState, Stack, StateWorkingSet}, - Span, Value, + Span, }; use reedline::{Completer as ReedlineCompleter, Suggestion}; use std::str; @@ -15,15 +15,13 @@ use std::sync::Arc; pub struct NuCompleter { engine_state: Arc, stack: Stack, - config: Option, } impl NuCompleter { - pub fn new(engine_state: Arc, stack: Stack, config: Option) -> Self { + pub fn new(engine_state: Arc, stack: Stack) -> Self { Self { engine_state, stack, - config, } } @@ -146,7 +144,6 @@ impl NuCompleter { let mut completer = CustomCompletion::new( self.engine_state.clone(), self.stack.clone(), - self.config.clone(), *decl_id, line, ); diff --git a/crates/nu-cli/src/completions/custom_completions.rs b/crates/nu-cli/src/completions/custom_completions.rs index 1f5334b03e..49df473023 100644 --- a/crates/nu-cli/src/completions/custom_completions.rs +++ b/crates/nu-cli/src/completions/custom_completions.rs @@ -3,7 +3,7 @@ use nu_engine::eval_call; use nu_protocol::{ ast::{Argument, Call, Expr, Expression}, engine::{EngineState, Stack, StateWorkingSet}, - PipelineData, Span, Type, Value, CONFIG_VARIABLE_ID, + PipelineData, Span, Type, Value, }; use reedline::Suggestion; use std::sync::Arc; @@ -11,23 +11,15 @@ use std::sync::Arc; pub struct CustomCompletion { engine_state: Arc, stack: Stack, - config: Option, decl_id: usize, line: String, } impl CustomCompletion { - pub fn new( - engine_state: Arc, - stack: Stack, - config: Option, - decl_id: usize, - line: String, - ) -> Self { + pub fn new(engine_state: Arc, stack: Stack, decl_id: usize, line: String) -> Self { Self { engine_state, stack, - config, decl_id, line, } @@ -71,20 +63,6 @@ impl Completer for CustomCompletion { // Line position let line_pos = pos - offset; - // Set up our initial config to start from - if let Some(conf) = &self.config { - self.stack.vars.insert(CONFIG_VARIABLE_ID, conf.clone()); - } else { - self.stack.vars.insert( - CONFIG_VARIABLE_ID, - Value::Record { - cols: vec![], - vals: vec![], - span: Span { start: 0, end: 0 }, - }, - ); - } - // Call custom declaration let result = eval_call( &self.engine_state, diff --git a/crates/nu-cli/src/nu_highlight.rs b/crates/nu-cli/src/nu_highlight.rs index 66cee8785b..f3c7d66a40 100644 --- a/crates/nu-cli/src/nu_highlight.rs +++ b/crates/nu-cli/src/nu_highlight.rs @@ -22,7 +22,7 @@ impl Command for NuHighlight { fn run( &self, engine_state: &EngineState, - stack: &mut Stack, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { @@ -30,7 +30,7 @@ impl Command for NuHighlight { let ctrlc = engine_state.ctrlc.clone(); let engine_state = engine_state.clone(); - let config = stack.get_config()?; + let config = engine_state.get_config().clone(); let highlighter = crate::NuHighlighter { engine_state, diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index 74112f4730..2b771bdbba 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -15,7 +15,7 @@ use nu_protocol::engine::Stack; use nu_protocol::PipelineData; use nu_protocol::{ engine::{EngineState, StateWorkingSet}, - Config, ShellError, Span, Value, CONFIG_VARIABLE_ID, + ShellError, Span, Value, }; use reedline::{DefaultHinter, Emacs, Vi}; use std::path::PathBuf; @@ -76,15 +76,7 @@ pub fn evaluate_repl( // Get the config once for the history `max_history_size` // Updating that will not be possible in one session - let mut config = match stack.get_config() { - Ok(config) => config, - Err(e) => { - let working_set = StateWorkingSet::new(engine_state); - - report_error(&working_set, &e); - Config::default() - } - }; + let mut config = engine_state.get_config(); if is_perf_true { info!("setup reedline {}:{}:{}", file!(), line!(), column!()); @@ -114,26 +106,18 @@ pub fn evaluate_repl( ); } - config = match stack.get_config() { - Ok(config) => config, - Err(e) => { - let working_set = StateWorkingSet::new(engine_state); + //Reset the ctrl-c handler + if let Some(ctrlc) = &mut engine_state.ctrlc { + ctrlc.store(false, Ordering::SeqCst); + } - report_error(&working_set, &e); - Config::default() - } - }; + config = engine_state.get_config(); if is_perf_true { info!("setup colors {}:{}:{}", file!(), line!(), column!()); } - let color_hm = get_color_config(&config); - - //Reset the ctrl-c handler - if let Some(ctrlc) = &mut engine_state.ctrlc { - ctrlc.store(false, Ordering::SeqCst); - } + let color_hm = get_color_config(config); if is_perf_true { info!("update reedline {}:{}:{}", file!(), line!(), column!()); @@ -151,7 +135,6 @@ pub fn evaluate_repl( .with_completer(Box::new(NuCompleter::new( engine_reference.clone(), stack.clone(), - stack.vars.get(&CONFIG_VARIABLE_ID).cloned(), ))) .with_quick_completions(config.quick_completions) .with_partial_completions(config.partial_completions) @@ -165,7 +148,7 @@ pub fn evaluate_repl( line_editor.disable_hints() }; - line_editor = match add_menus(line_editor, engine_reference, stack, &config) { + line_editor = match add_menus(line_editor, engine_reference, stack, config) { Ok(line_editor) => line_editor, Err(e) => { let working_set = StateWorkingSet::new(engine_state); @@ -186,7 +169,7 @@ pub fn evaluate_repl( } // Changing the line editor based on the found keybindings - line_editor = match reedline_config::create_keybindings(&config) { + line_editor = match reedline_config::create_keybindings(config) { Ok(keybindings) => match keybindings { KeybindingsMode::Emacs(keybindings) => { let edit_mode = Box::new(Emacs::new(keybindings)); @@ -211,13 +194,8 @@ pub fn evaluate_repl( info!("prompt_update {}:{}:{}", file!(), line!(), column!()); } - let prompt = prompt_update::update_prompt( - &config, - engine_state, - stack, - &mut nu_prompt, - is_perf_true, - ); + let prompt = + prompt_update::update_prompt(config, engine_state, stack, &mut nu_prompt, is_perf_true); entry_num += 1; diff --git a/crates/nu-cli/src/util.rs b/crates/nu-cli/src/util.rs index db5f64ed22..cbabe6c667 100644 --- a/crates/nu-cli/src/util.rs +++ b/crates/nu-cli/src/util.rs @@ -21,7 +21,7 @@ pub fn print_pipeline_data( // If the table function is in the declarations, then we can use it // to create the table value that will be printed in the terminal - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config(); let stdout = std::io::stdout(); @@ -61,7 +61,7 @@ pub fn print_pipeline_data( return Err(error); } - let mut out = item.into_string("\n", &config); + let mut out = item.into_string("\n", config); out.push('\n'); match stdout.lock().write_all(out.as_bytes()) { @@ -78,7 +78,7 @@ pub fn print_pipeline_data( return Err(error); } - let mut out = item.into_string("\n", &config); + let mut out = item.into_string("\n", config); out.push('\n'); match stdout.lock().write_all(out.as_bytes()) { diff --git a/crates/nu-command/src/conversions/into/string.rs b/crates/nu-command/src/conversions/into/string.rs index b35cf30d2b..e1ac748158 100644 --- a/crates/nu-command/src/conversions/into/string.rs +++ b/crates/nu-command/src/conversions/into/string.rs @@ -138,7 +138,7 @@ fn string_helper( let head = call.head; let decimals_value: Option = call.get_flag(engine_state, stack, "decimals")?; let column_paths: Vec = call.rest(engine_state, stack, 0)?; - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config().clone(); if let Some(decimal_val) = decimals_value { if decimals && decimal_val.is_negative() { diff --git a/crates/nu-command/src/core_commands/debug.rs b/crates/nu-command/src/core_commands/debug.rs index 67062924c8..fd2e674746 100644 --- a/crates/nu-command/src/core_commands/debug.rs +++ b/crates/nu-command/src/core_commands/debug.rs @@ -25,12 +25,12 @@ impl Command for Debug { fn run( &self, engine_state: &EngineState, - stack: &mut Stack, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { let head = call.head; - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config().clone(); let raw = call.has_flag("raw"); input.map( diff --git a/crates/nu-command/src/dataframe/test_dataframe.rs b/crates/nu-command/src/dataframe/test_dataframe.rs index 8c33690761..d855367bb3 100644 --- a/crates/nu-command/src/dataframe/test_dataframe.rs +++ b/crates/nu-command/src/dataframe/test_dataframe.rs @@ -2,7 +2,7 @@ use nu_engine::eval_block; use nu_parser::parse; use nu_protocol::{ engine::{Command, EngineState, Stack, StateWorkingSet}, - PipelineData, Span, Value, CONFIG_VARIABLE_ID, + PipelineData, Span, }; use super::eager::ToDataFrame; @@ -63,16 +63,6 @@ pub fn test_dataframe(cmds: Vec>) { let mut stack = Stack::new(); - // Set up our initial config to start from - stack.vars.insert( - CONFIG_VARIABLE_ID, - Value::Record { - cols: vec![], - vals: vec![], - span: Span::test_data(), - }, - ); - match eval_block( &engine_state, &mut stack, diff --git a/crates/nu-command/src/example_test.rs b/crates/nu-command/src/example_test.rs index 0d65910b11..26126991d2 100644 --- a/crates/nu-command/src/example_test.rs +++ b/crates/nu-command/src/example_test.rs @@ -5,7 +5,7 @@ use nu_parser::parse; #[cfg(test)] use nu_protocol::{ engine::{Command, EngineState, Stack, StateWorkingSet}, - PipelineData, Span, Value, CONFIG_VARIABLE_ID, + PipelineData, Span, Value, }; #[cfg(test)] @@ -112,16 +112,6 @@ pub fn test_examples(cmd: impl Command + 'static) { }, ); - // Set up our initial config to start from - stack.vars.insert( - CONFIG_VARIABLE_ID, - Value::Record { - cols: vec![], - vals: vec![], - span: Span::test_data(), - }, - ); - match eval_block( &engine_state, &mut stack, diff --git a/crates/nu-command/src/filesystem/open.rs b/crates/nu-command/src/filesystem/open.rs index 78c30f1715..e35ea5d367 100644 --- a/crates/nu-command/src/filesystem/open.rs +++ b/crates/nu-command/src/filesystem/open.rs @@ -378,31 +378,22 @@ mod test { vals: vec![ Value::Record { cols: vec!["id".to_string(), "name".to_string()], - vals: vec![ - Value::Int { - val: 123, - span: span, - }, - Value::Nothing { span: span }, - ], - span: span, + vals: vec![Value::Int { val: 123, span }, Value::Nothing { span }], + span, }, Value::Record { cols: vec!["id".to_string(), "name".to_string()], vals: vec![ - Value::Int { - val: 456, - span: span, - }, + Value::Int { val: 456, span }, Value::String { val: "foo bar".to_string(), - span: span, + span, }, ], - span: span, + span, }, ], - span: span, + span, }], span, }; diff --git a/crates/nu-command/src/filesystem/rm.rs b/crates/nu-command/src/filesystem/rm.rs index 2c79ed2899..edebc6f25d 100644 --- a/crates/nu-command/src/filesystem/rm.rs +++ b/crates/nu-command/src/filesystem/rm.rs @@ -133,7 +133,7 @@ fn rm( let targets: Vec> = call.rest(engine_state, stack, 0)?; let span = call.head; - let config = stack.get_config()?; + let config = engine_state.get_config(); let rm_always_trash = config.rm_always_trash; diff --git a/crates/nu-command/src/filters/find.rs b/crates/nu-command/src/filters/find.rs index 3fa4fcf3e5..80040ea57b 100644 --- a/crates/nu-command/src/filters/find.rs +++ b/crates/nu-command/src/filters/find.rs @@ -157,13 +157,13 @@ impl Command for Find { fn find_with_regex( regex: String, engine_state: &EngineState, - stack: &mut Stack, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { let span = call.head; let ctrlc = engine_state.ctrlc.clone(); - let config = stack.get_config()?; + let config = engine_state.get_config().clone(); let insensitive = call.has_flag("insensitive"); let multiline = call.has_flag("multiline"); @@ -271,7 +271,7 @@ fn find_with_rest( let ctrlc = engine_state.ctrlc.clone(); let metadata = input.metadata(); let engine_state = engine_state.clone(); - let config = stack.get_config()?; + let config = engine_state.get_config().clone(); let invert = call.has_flag("invert"); let terms = call.rest::(&engine_state, stack, 0)?; diff --git a/crates/nu-command/src/filters/headers.rs b/crates/nu-command/src/filters/headers.rs index c5546c7953..aeb9f61d99 100644 --- a/crates/nu-command/src/filters/headers.rs +++ b/crates/nu-command/src/filters/headers.rs @@ -71,15 +71,15 @@ impl Command for Headers { fn run( &self, - _engine_state: &EngineState, - stack: &mut Stack, + engine_state: &EngineState, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { - let config = stack.get_config()?; + let config = engine_state.get_config(); let metadata = input.metadata(); let value = input.into_value(call.head); - let headers = extract_headers(&value, &config)?; + let headers = extract_headers(&value, config)?; let new_headers = replace_headers(value, &headers)?; Ok(new_headers.into_pipeline_data().set_metadata(metadata)) diff --git a/crates/nu-command/src/formats/from/csv.rs b/crates/nu-command/src/formats/from/csv.rs index cf60873b1b..77d3a5e5ab 100644 --- a/crates/nu-command/src/formats/from/csv.rs +++ b/crates/nu-command/src/formats/from/csv.rs @@ -79,7 +79,7 @@ fn from_csv( let noheaders = call.has_flag("noheaders"); let separator: Option = call.get_flag(engine_state, stack, "separator")?; - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config(); let sep = match separator { Some(Value::String { val: s, span }) => { @@ -99,7 +99,7 @@ fn from_csv( _ => ',', }; - from_delimited_data(noheaders, sep, input, name, &config) + from_delimited_data(noheaders, sep, input, name, config) } #[cfg(test)] diff --git a/crates/nu-command/src/formats/from/eml.rs b/crates/nu-command/src/formats/from/eml.rs index 853f575bbb..1880afc3b2 100644 --- a/crates/nu-command/src/formats/from/eml.rs +++ b/crates/nu-command/src/formats/from/eml.rs @@ -45,8 +45,8 @@ impl Command for FromEml { let head = call.head; let preview_body: Option> = call.get_flag(engine_state, stack, "preview-body")?; - let config = stack.get_config().unwrap_or_default(); - from_eml(input, preview_body, head, &config) + let config = engine_state.get_config(); + from_eml(input, preview_body, head, config) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/formats/from/ics.rs b/crates/nu-command/src/formats/from/ics.rs index c320de7bd5..382d54935a 100644 --- a/crates/nu-command/src/formats/from/ics.rs +++ b/crates/nu-command/src/formats/from/ics.rs @@ -28,14 +28,14 @@ impl Command for FromIcs { fn run( &self, - _engine_state: &EngineState, - stack: &mut Stack, + engine_state: &EngineState, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { let head = call.head; - let config = stack.get_config().unwrap_or_default(); - from_ics(input, head, &config) + let config = engine_state.get_config(); + from_ics(input, head, config) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/formats/from/ini.rs b/crates/nu-command/src/formats/from/ini.rs index f7b83ec5cd..4a40e1c369 100644 --- a/crates/nu-command/src/formats/from/ini.rs +++ b/crates/nu-command/src/formats/from/ini.rs @@ -50,14 +50,14 @@ b=2' | from ini", fn run( &self, - _engine_state: &EngineState, - stack: &mut Stack, + engine_state: &EngineState, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { let head = call.head; - let config = stack.get_config().unwrap_or_default(); - from_ini(input, head, &config) + let config = engine_state.get_config(); + from_ini(input, head, config) } } diff --git a/crates/nu-command/src/formats/from/json.rs b/crates/nu-command/src/formats/from/json.rs index c3d5e32ced..5cf7143f5a 100644 --- a/crates/nu-command/src/formats/from/json.rs +++ b/crates/nu-command/src/formats/from/json.rs @@ -70,13 +70,13 @@ impl Command for FromJson { fn run( &self, engine_state: &EngineState, - stack: &mut Stack, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { let span = call.head; - let config = stack.get_config().unwrap_or_default(); - let string_input = input.collect_string("", &config)?; + let config = engine_state.get_config(); + let string_input = input.collect_string("", config)?; if string_input.is_empty() { return Ok(PipelineData::new(span)); diff --git a/crates/nu-command/src/formats/from/nuon.rs b/crates/nu-command/src/formats/from/nuon.rs index 925d4f764a..2cdffe3208 100644 --- a/crates/nu-command/src/formats/from/nuon.rs +++ b/crates/nu-command/src/formats/from/nuon.rs @@ -66,14 +66,14 @@ impl Command for FromNuon { fn run( &self, - _engine_state: &EngineState, - stack: &mut Stack, + engine_state: &EngineState, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { let head = call.head; - let config = stack.get_config().unwrap_or_default(); - let string_input = input.collect_string("", &config)?; + let config = engine_state.get_config(); + let string_input = input.collect_string("", config)?; let engine_state = EngineState::new(); diff --git a/crates/nu-command/src/formats/from/ssv.rs b/crates/nu-command/src/formats/from/ssv.rs index 2be0bd52ba..e6a917371a 100644 --- a/crates/nu-command/src/formats/from/ssv.rs +++ b/crates/nu-command/src/formats/from/ssv.rs @@ -267,7 +267,7 @@ fn from_ssv( call: &Call, input: PipelineData, ) -> Result { - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config(); let name = call.head; let noheaders = call.has_flag("noheaders"); @@ -275,7 +275,7 @@ fn from_ssv( let minimum_spaces: Option> = call.get_flag(engine_state, stack, "minimum-spaces")?; - let concat_string = input.collect_string("", &config)?; + let concat_string = input.collect_string("", config)?; let split_at = match minimum_spaces { Some(number) => number.item, None => DEFAULT_MINIMUM_SPACES, diff --git a/crates/nu-command/src/formats/from/toml.rs b/crates/nu-command/src/formats/from/toml.rs index 258dae3f85..6c3d5a6f93 100644 --- a/crates/nu-command/src/formats/from/toml.rs +++ b/crates/nu-command/src/formats/from/toml.rs @@ -67,14 +67,14 @@ b = [1, 2]' | from toml", fn run( &self, - _engine_state: &EngineState, - stack: &mut Stack, + engine_state: &EngineState, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { let span = call.head; - let config = stack.get_config().unwrap_or_default(); - let mut string_input = input.collect_string("", &config)?; + let config = engine_state.get_config(); + let mut string_input = input.collect_string("", config)?; string_input.push('\n'); Ok(convert_string_to_value(string_input, span)?.into_pipeline_data()) } diff --git a/crates/nu-command/src/formats/from/tsv.rs b/crates/nu-command/src/formats/from/tsv.rs index a38346dd04..a569806f5c 100644 --- a/crates/nu-command/src/formats/from/tsv.rs +++ b/crates/nu-command/src/formats/from/tsv.rs @@ -28,13 +28,13 @@ impl Command for FromTsv { fn run( &self, - _engine_state: &EngineState, - stack: &mut Stack, + engine_state: &EngineState, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { - let config = stack.get_config().unwrap_or_default(); - from_tsv(call, input, &config) + let config = engine_state.get_config(); + from_tsv(call, input, config) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/formats/from/url.rs b/crates/nu-command/src/formats/from/url.rs index 1ee91d4296..630f57f226 100644 --- a/crates/nu-command/src/formats/from/url.rs +++ b/crates/nu-command/src/formats/from/url.rs @@ -20,14 +20,14 @@ impl Command for FromUrl { fn run( &self, - _engine_state: &EngineState, - stack: &mut Stack, + engine_state: &EngineState, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { let head = call.head; - let config = stack.get_config().unwrap_or_default(); - from_url(input, head, &config) + let config = engine_state.get_config(); + from_url(input, head, config) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/formats/from/vcf.rs b/crates/nu-command/src/formats/from/vcf.rs index 4b98535474..d6fd84f191 100644 --- a/crates/nu-command/src/formats/from/vcf.rs +++ b/crates/nu-command/src/formats/from/vcf.rs @@ -26,14 +26,14 @@ impl Command for FromVcf { fn run( &self, - _engine_state: &EngineState, - stack: &mut Stack, + engine_state: &EngineState, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { let head = call.head; - let config = stack.get_config().unwrap_or_default(); - from_vcf(input, head, &config) + let config = engine_state.get_config(); + from_vcf(input, head, config) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/formats/from/xml.rs b/crates/nu-command/src/formats/from/xml.rs index bf6756cc82..481f734b51 100644 --- a/crates/nu-command/src/formats/from/xml.rs +++ b/crates/nu-command/src/formats/from/xml.rs @@ -24,14 +24,14 @@ impl Command for FromXml { fn run( &self, - _engine_state: &EngineState, - stack: &mut Stack, + engine_state: &EngineState, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { let head = call.head; - let config = stack.get_config().unwrap_or_default(); - from_xml(input, head, &config) + let config = engine_state.get_config(); + from_xml(input, head, config) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/formats/from/yaml.rs b/crates/nu-command/src/formats/from/yaml.rs index 0fe0e50f80..abab7cdaa2 100644 --- a/crates/nu-command/src/formats/from/yaml.rs +++ b/crates/nu-command/src/formats/from/yaml.rs @@ -30,14 +30,14 @@ impl Command for FromYaml { fn run( &self, - _engine_state: &EngineState, - stack: &mut Stack, + engine_state: &EngineState, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { let head = call.head; - let config = stack.get_config().unwrap_or_default(); - from_yaml(input, head, &config) + let config = engine_state.get_config(); + from_yaml(input, head, config) } } @@ -59,14 +59,14 @@ impl Command for FromYml { fn run( &self, - _engine_state: &EngineState, - stack: &mut Stack, + engine_state: &EngineState, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { let head = call.head; - let config = stack.get_config().unwrap_or_default(); - from_yaml(input, head, &config) + let config = engine_state.get_config(); + from_yaml(input, head, config) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/formats/to/csv.rs b/crates/nu-command/src/formats/to/csv.rs index 5a33ba1b74..2ad25294e1 100644 --- a/crates/nu-command/src/formats/to/csv.rs +++ b/crates/nu-command/src/formats/to/csv.rs @@ -60,7 +60,7 @@ impl Command for ToCsv { let head = call.head; let noheaders = call.has_flag("noheaders"); let separator: Option> = call.get_flag(engine_state, stack, "separator")?; - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config(); to_csv(input, noheaders, separator, head, config) } } @@ -70,7 +70,7 @@ fn to_csv( noheaders: bool, separator: Option>, head: Span, - config: Config, + config: &Config, ) -> Result { let sep = match separator { Some(Spanned { item: s, span, .. }) => { diff --git a/crates/nu-command/src/formats/to/delimited.rs b/crates/nu-command/src/formats/to/delimited.rs index 255a5c3997..7ad136c1da 100644 --- a/crates/nu-command/src/formats/to/delimited.rs +++ b/crates/nu-command/src/formats/to/delimited.rs @@ -125,10 +125,10 @@ pub fn to_delimited_data( format_name: &'static str, input: PipelineData, span: Span, - config: Config, + config: &Config, ) -> Result { let value = input.into_value(span); - let output = match from_value_to_delimited_string(&value, sep, &config, span) { + let output = match from_value_to_delimited_string(&value, sep, config, span) { Ok(mut x) => { if noheaders { if let Some(second_line) = x.find('\n') { diff --git a/crates/nu-command/src/formats/to/html.rs b/crates/nu-command/src/formats/to/html.rs index 564630f344..d2c8976fa2 100644 --- a/crates/nu-command/src/formats/to/html.rs +++ b/crates/nu-command/src/formats/to/html.rs @@ -295,7 +295,7 @@ fn to_html( let partial = call.has_flag("partial"); let list = call.has_flag("list"); let theme: Option> = call.get_flag(engine_state, stack, "theme")?; - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config(); let vec_of_values = input.into_iter().collect::>(); let headers = merge_descriptors(&vec_of_values); @@ -365,15 +365,15 @@ fn to_html( let inner_value = match vec_of_values.len() { 0 => String::default(), 1 => match headers { - Some(headers) => html_table(vec_of_values, headers, &config), + Some(headers) => html_table(vec_of_values, headers, config), None => { let value = &vec_of_values[0]; - html_value(value.clone(), &config) + html_value(value.clone(), config) } }, _ => match headers { - Some(headers) => html_table(vec_of_values, headers, &config), - None => html_list(vec_of_values, &config), + Some(headers) => html_table(vec_of_values, headers, config), + None => html_list(vec_of_values, config), }, }; diff --git a/crates/nu-command/src/formats/to/md.rs b/crates/nu-command/src/formats/to/md.rs index c12ae90563..8ffc6af58c 100644 --- a/crates/nu-command/src/formats/to/md.rs +++ b/crates/nu-command/src/formats/to/md.rs @@ -59,15 +59,15 @@ impl Command for ToMd { fn run( &self, - _engine_state: &EngineState, - stack: &mut Stack, + engine_state: &EngineState, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { let head = call.head; let pretty = call.has_flag("pretty"); let per_element = call.has_flag("per-element"); - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config(); to_md(input, pretty, per_element, config, head) } } @@ -76,17 +76,17 @@ fn to_md( input: PipelineData, pretty: bool, per_element: bool, - config: Config, + config: &Config, head: Span, ) -> Result { - let (grouped_input, single_list) = group_by(input, head, &config); + let (grouped_input, single_list) = group_by(input, head, config); if per_element || single_list { return Ok(Value::string( grouped_input .into_iter() .map(move |val| match val { - Value::List { .. } => table(val.into_pipeline_data(), pretty, &config), - other => fragment(other, pretty, &config), + Value::List { .. } => table(val.into_pipeline_data(), pretty, config), + other => fragment(other, pretty, config), }) .collect::>() .join(""), @@ -94,7 +94,7 @@ fn to_md( ) .into_pipeline_data()); } - Ok(Value::string(table(grouped_input, pretty, &config), head).into_pipeline_data()) + Ok(Value::string(table(grouped_input, pretty, config), head).into_pipeline_data()) } fn fragment(input: Value, pretty: bool, config: &Config) -> String { diff --git a/crates/nu-command/src/formats/to/tsv.rs b/crates/nu-command/src/formats/to/tsv.rs index 957fa15383..8efca319e3 100644 --- a/crates/nu-command/src/formats/to/tsv.rs +++ b/crates/nu-command/src/formats/to/tsv.rs @@ -35,14 +35,14 @@ impl Command for ToTsv { fn run( &self, - _engine_state: &EngineState, - stack: &mut Stack, + engine_state: &EngineState, + _stack: &mut Stack, call: &Call, input: PipelineData, ) -> Result { let head = call.head; let noheaders = call.has_flag("noheaders"); - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config(); to_tsv(input, noheaders, head, config) } } @@ -51,7 +51,7 @@ fn to_tsv( input: PipelineData, noheaders: bool, head: Span, - config: Config, + config: &Config, ) -> Result { to_delimited_data(noheaders, '\t', "TSV", input, head, config) } diff --git a/crates/nu-command/src/formats/to/xml.rs b/crates/nu-command/src/formats/to/xml.rs index b07798d732..c6fb7f8eb0 100644 --- a/crates/nu-command/src/formats/to/xml.rs +++ b/crates/nu-command/src/formats/to/xml.rs @@ -61,9 +61,9 @@ impl Command for ToXml { input: PipelineData, ) -> Result { let head = call.head; - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config(); let pretty: Option> = call.get_flag(engine_state, stack, "pretty")?; - to_xml(input, head, pretty, &config) + to_xml(input, head, pretty, config) } } diff --git a/crates/nu-command/src/platform/ansi/ansi_.rs b/crates/nu-command/src/platform/ansi/ansi_.rs index c86436148d..6d818a45fb 100644 --- a/crates/nu-command/src/platform/ansi/ansi_.rs +++ b/crates/nu-command/src/platform/ansi/ansi_.rs @@ -317,7 +317,7 @@ Format: # let list: bool = call.has_flag("list"); let escape: bool = call.has_flag("escape"); let osc: bool = call.has_flag("osc"); - let use_ansi_coloring = stack.get_config()?.use_ansi_coloring; + let use_ansi_coloring = engine_state.get_config().use_ansi_coloring; if list { return generate_ansi_code_list(engine_state, call.head, use_ansi_coloring); diff --git a/crates/nu-command/src/platform/reedline_commands/keybindings_listen.rs b/crates/nu-command/src/platform/reedline_commands/keybindings_listen.rs index 11f32e6284..b33b074997 100644 --- a/crates/nu-command/src/platform/reedline_commands/keybindings_listen.rs +++ b/crates/nu-command/src/platform/reedline_commands/keybindings_listen.rs @@ -25,14 +25,14 @@ impl Command for KeybindingsListen { fn run( &self, - _engine_state: &EngineState, - stack: &mut Stack, + engine_state: &EngineState, + _stack: &mut Stack, _call: &Call, _input: PipelineData, ) -> Result { println!("Type any key combination to see key details. Press ESC to abort."); - match print_events(stack) { + match print_events(engine_state) { Ok(v) => Ok(v.into_pipeline_data()), Err(e) => { terminal::disable_raw_mode()?; @@ -56,8 +56,8 @@ impl Command for KeybindingsListen { } } -pub fn print_events(stack: &mut Stack) -> Result { - let config = stack.get_config()?; +pub fn print_events(engine_state: &EngineState) -> Result { + let config = engine_state.get_config(); stdout().flush()?; terminal::enable_raw_mode()?; @@ -78,7 +78,7 @@ pub fn print_events(stack: &mut Stack) -> Result { Value::Record { cols, vals, .. } => cols .iter() .zip(vals.iter()) - .map(|(x, y)| format!("{}: {}", x, y.into_string("", &config))) + .map(|(x, y)| format!("{}: {}", x, y.into_string("", config))) .collect::>() .join(", "), diff --git a/crates/nu-command/src/strings/build_string.rs b/crates/nu-command/src/strings/build_string.rs index beda86be71..543cc0379d 100644 --- a/crates/nu-command/src/strings/build_string.rs +++ b/crates/nu-command/src/strings/build_string.rs @@ -52,11 +52,11 @@ impl Command for BuildString { call: &Call, _input: PipelineData, ) -> Result { - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config(); let output = call .positional_iter() .map(|expr| { - eval_expression(engine_state, stack, expr).map(|val| val.into_string(", ", &config)) + eval_expression(engine_state, stack, expr).map(|val| val.into_string(", ", config)) }) .collect::, ShellError>>()?; diff --git a/crates/nu-command/src/strings/detect_columns.rs b/crates/nu-command/src/strings/detect_columns.rs index 082e28cf47..d455f73bce 100644 --- a/crates/nu-command/src/strings/detect_columns.rs +++ b/crates/nu-command/src/strings/detect_columns.rs @@ -87,8 +87,8 @@ fn detect_columns( let num_rows_to_skip: Option = call.get_flag(engine_state, stack, "skip")?; let noheader = call.has_flag("no-headers"); let ctrlc = engine_state.ctrlc.clone(); - let config = stack.get_config()?; - let input = input.collect_string("", &config)?; + let config = engine_state.get_config(); + let input = input.collect_string("", config)?; #[allow(clippy::needless_collect)] let input: Vec<_> = input diff --git a/crates/nu-command/src/strings/format/command.rs b/crates/nu-command/src/strings/format/command.rs index fe5fab3256..b0ce93f2d0 100644 --- a/crates/nu-command/src/strings/format/command.rs +++ b/crates/nu-command/src/strings/format/command.rs @@ -35,14 +35,14 @@ impl Command for Format { call: &Call, input: PipelineData, ) -> Result { - let config = stack.get_config()?; + let config = engine_state.get_config(); let specified_pattern: Result = call.req(engine_state, stack, 0); match specified_pattern { Err(e) => Err(e), Ok(pattern) => { let string_pattern = pattern.as_string()?; let ops = extract_formatting_operations(string_pattern); - format(input, &ops, call.head, &config) + format(input, &ops, call.head, config) } } } diff --git a/crates/nu-command/src/strings/str_/collect.rs b/crates/nu-command/src/strings/str_/collect.rs index 08af5f9539..551594ab91 100644 --- a/crates/nu-command/src/strings/str_/collect.rs +++ b/crates/nu-command/src/strings/str_/collect.rs @@ -41,7 +41,7 @@ impl Command for StrCollect { ) -> Result { let separator: Option = call.opt(engine_state, stack, 0)?; - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config(); // let output = input.collect_string(&separator.unwrap_or_default(), &config)?; // Hmm, not sure what we actually want. If you don't use debug_string, Date comes out as human readable @@ -54,7 +54,7 @@ impl Command for StrCollect { return Err(error); } value => { - strings.push(value.debug_string("\n", &config)); + strings.push(value.debug_string("\n", config)); } } } diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index c06768afec..f34e53892f 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -147,15 +147,12 @@ impl ExternalCommand { )), Ok(mut child) => { if !input.is_nothing() { - let engine_state = engine_state.clone(); + let mut engine_state = engine_state.clone(); let mut stack = stack.clone(); - stack.update_config( - "use_ansi_coloring", - Value::Bool { - val: false, - span: Span::new(0, 0), - }, - ); + + // Turn off color as we pass data through + engine_state.config.use_ansi_coloring = false; + // if there is a string or a stream, that is sent to the pipe std if let Some(mut stdin_write) = child.stdin.take() { std::thread::spawn(move || { diff --git a/crates/nu-command/src/viewers/griddle.rs b/crates/nu-command/src/viewers/griddle.rs index c48a951da6..18d6755ea9 100644 --- a/crates/nu-command/src/viewers/griddle.rs +++ b/crates/nu-command/src/viewers/griddle.rs @@ -64,7 +64,7 @@ prints out the list properly."# let width_param: Option = call.get_flag(engine_state, stack, "width")?; let color_param: bool = call.has_flag("color"); let separator_param: Option = call.get_flag(engine_state, stack, "separator")?; - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config(); let env_str = match stack.get_env_var(engine_state, "LS_COLORS") { Some(v) => Some(env_to_string("LS_COLORS", &v, engine_state, stack)?), None => None, @@ -74,7 +74,7 @@ prints out the list properly."# match input { PipelineData::Value(Value::List { vals, .. }, ..) => { // dbg!("value::list"); - let data = convert_to_list(vals, &config, call.head); + let data = convert_to_list(vals, config, call.head); if let Some(items) = data { Ok(create_grid_output( items, @@ -91,7 +91,7 @@ prints out the list properly."# } PipelineData::ListStream(stream, ..) => { // dbg!("value::stream"); - let data = convert_to_list(stream, &config, call.head); + let data = convert_to_list(stream, config, call.head); if let Some(items) = data { Ok(create_grid_output( items, @@ -112,7 +112,7 @@ prints out the list properly."# let mut items = vec![]; for (i, (c, v)) in cols.into_iter().zip(vals.into_iter()).enumerate() { - items.push((i, c, v.into_string(", ", &config))) + items.push((i, c, v.into_string(", ", config))) } Ok(create_grid_output( diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index 4c5ce298cf..9a848f08c7 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -56,8 +56,8 @@ impl Command for Table { ) -> Result { let head = call.head; let ctrlc = engine_state.ctrlc.clone(); - let config = stack.get_config().unwrap_or_default(); - let color_hm = get_color_config(&config); + let config = engine_state.get_config(); + let color_hm = get_color_config(config); let start_num: Option = call.get_flag(engine_state, stack, "start-number")?; let row_offset = start_num.unwrap_or_default() as usize; @@ -99,7 +99,6 @@ impl Command for Table { ListStream::from_stream(vals.into_iter(), ctrlc.clone()), call, row_offset, - config, ctrlc, metadata, ), @@ -109,7 +108,6 @@ impl Command for Table { stream, call, row_offset, - config, ctrlc, metadata, ), @@ -123,7 +121,7 @@ impl Command for Table { style: TextStyle::default_field(), }, StyledString { - contents: v.into_abbreviated_string(&config), + contents: v.into_abbreviated_string(config), style: TextStyle::default(), }, ]) @@ -132,10 +130,10 @@ impl Command for Table { let table = nu_table::Table { headers: vec![], data: output, - theme: load_theme_from_config(&config), + theme: load_theme_from_config(config), }; - let result = nu_table::draw_table(&table, term_width, &color_hm, &config); + let result = nu_table::draw_table(&table, term_width, &color_hm, config); Ok(Value::String { val: result, @@ -149,7 +147,7 @@ impl Command for Table { self.run(engine_state, stack, call, base_pipeline) } PipelineData::Value(x @ Value::Range { .. }, ..) => Ok(Value::String { - val: x.into_string("", &config), + val: x.into_string("", config), span: call.head, } .into_pipeline_data()), @@ -195,7 +193,6 @@ fn handle_row_stream( stream: ListStream, call: &Call, row_offset: usize, - config: Config, ctrlc: Option>, metadata: Option, ) -> Result { @@ -203,7 +200,7 @@ fn handle_row_stream( Some(PipelineMetadata { data_source: DataSource::Ls, }) => { - let config = config.clone(); + let config = engine_state.config.clone(); let ctrlc = ctrlc.clone(); let ls_colors = match stack.get_env_var(engine_state, "LS_COLORS") { @@ -281,7 +278,7 @@ fn handle_row_stream( stdout: Some(RawStream::new( Box::new(PagingTableCreator { row_offset, - config, + config: engine_state.get_config().clone(), ctrlc: ctrlc.clone(), head, stream, diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index aef6aa4ce5..dfdb9691c0 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -506,12 +506,12 @@ pub fn eval_expression( parts.push(eval_expression(engine_state, stack, expr)?); } - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config(); parts .into_iter() .into_pipeline_data(None) - .collect_string("", &config) + .collect_string("", config) .map(|x| Value::String { val: x, span: expr.span, @@ -634,7 +634,7 @@ pub fn eval_block( let exit_code = exit_code.take(); // Drain the input to the screen via tabular output - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config(); match engine_state.find_decl("table".as_bytes()) { Some(decl_id) => { @@ -652,7 +652,7 @@ pub fn eval_block( return Err(error); } - let mut out = item.into_string("\n", &config); + let mut out = item.into_string("\n", config); out.push('\n'); match stdout.lock().write_all(out.as_bytes()) { @@ -669,7 +669,7 @@ pub fn eval_block( return Err(error); } - let mut out = item.into_string("\n", &config); + let mut out = item.into_string("\n", config); out.push('\n'); match stdout.lock().write_all(out.as_bytes()) { @@ -690,7 +690,7 @@ pub fn eval_block( } _ => { // Drain the input to the screen via tabular output - let config = stack.get_config().unwrap_or_default(); + let config = engine_state.get_config(); match engine_state.find_decl("table".as_bytes()) { Some(decl_id) => { @@ -708,7 +708,7 @@ pub fn eval_block( return Err(error); } - let mut out = item.into_string("\n", &config); + let mut out = item.into_string("\n", config); out.push('\n'); match stdout.lock().write_all(out.as_bytes()) { @@ -725,7 +725,7 @@ pub fn eval_block( return Err(error); } - let mut out = item.into_string("\n", &config); + let mut out = item.into_string("\n", config); out.push('\n'); match stdout.lock().write_all(out.as_bytes()) { diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 4dc9317be9..35dc0087d6 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -5,7 +5,7 @@ use nu_protocol::{ ImportPatternMember, Pipeline, }, engine::StateWorkingSet, - span, Exportable, Overlay, PositionalArg, Span, SyntaxShape, Type, CONFIG_VARIABLE_ID, + span, Exportable, Overlay, PositionalArg, Span, SyntaxShape, Type, }; use std::collections::HashSet; use std::path::{Path, PathBuf}; @@ -1710,9 +1710,7 @@ pub fn parse_let( let rhs_type = rvalue.ty.clone(); if let Some(var_id) = var_id { - if var_id != CONFIG_VARIABLE_ID { - working_set.set_variable_type(var_id, rhs_type); - } + working_set.set_variable_type(var_id, rhs_type); } let call = Box::new(Call { diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index a839a9778d..8621021006 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -14,7 +14,7 @@ use nu_protocol::{ }, engine::StateWorkingSet, span, BlockId, Flag, PositionalArg, Signature, Span, Spanned, SyntaxShape, Type, Unit, VarId, - CONFIG_VARIABLE_ID, ENV_VARIABLE_ID, IN_VARIABLE_ID, + ENV_VARIABLE_ID, IN_VARIABLE_ID, }; use crate::parse_keywords::{ @@ -1689,16 +1689,6 @@ pub fn parse_variable_expr( }, None, ); - } else if contents == b"$config" { - return ( - Expression { - expr: Expr::Var(nu_protocol::CONFIG_VARIABLE_ID), - span, - ty: Type::Any, - custom_completion: None, - }, - None, - ); } else if contents == b"$env" { return ( Expression { @@ -2818,16 +2808,6 @@ pub fn parse_var_with_opt_type( Some(ParseError::MissingType(spans[*spans_idx])), ) } - } else if bytes == b"$config" || bytes == b"config" { - ( - Expression { - expr: Expr::Var(CONFIG_VARIABLE_ID), - span: spans[*spans_idx], - ty: Type::Any, - custom_completion: None, - }, - None, - ) } else { let id = working_set.add_variable(bytes, span(&spans[*spans_idx..*spans_idx + 1]), Type::Any); diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index 1512687e65..577b57cace 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -1,7 +1,7 @@ use super::{Command, Stack}; use crate::{ - ast::Block, AliasId, BlockId, DeclId, Example, Overlay, OverlayId, ShellError, Signature, Span, - Type, VarId, Variable, + ast::Block, AliasId, BlockId, Config, DeclId, Example, Overlay, OverlayId, ShellError, + Signature, Span, Type, VarId, Variable, }; use core::panic; use std::{ @@ -176,14 +176,14 @@ pub struct EngineState { pub scope: Vec, pub ctrlc: Option>, pub env_vars: HashMap, + pub config: Config, #[cfg(feature = "plugin")] pub plugin_signatures: Option, } pub const NU_VARIABLE_ID: usize = 0; pub const IN_VARIABLE_ID: usize = 1; -pub const CONFIG_VARIABLE_ID: usize = 2; -pub const ENV_VARIABLE_ID: usize = 3; +pub const ENV_VARIABLE_ID: usize = 2; // NOTE: If you add more to this list, make sure to update the > checks based on the last in the list impl EngineState { @@ -205,6 +205,7 @@ impl EngineState { scope: vec![ScopeFrame::new()], ctrlc: None, env_vars: HashMap::new(), + config: Config::default(), #[cfg(feature = "plugin")] plugin_signatures: None, } @@ -263,6 +264,10 @@ impl EngineState { if let Some(stack) = stack { for mut env_scope in stack.env_vars.drain(..) { for (k, v) in env_scope.drain() { + if k == "config" { + self.config = v.clone().into_config().unwrap_or_default(); + } + self.env_vars.insert(k, v); } } @@ -474,6 +479,10 @@ impl EngineState { panic!("internal error: span missing in file contents cache") } + pub fn get_config(&self) -> &Config { + &self.config + } + pub fn get_var(&self, var_id: VarId) -> &Variable { self.vars .get(var_id) @@ -1233,6 +1242,10 @@ impl<'a> StateWorkingSet<'a> { self.permanent_state.env_vars.get(name) } + pub fn get_config(&self) -> &Config { + &self.permanent_state.config + } + pub fn list_env(&self) -> Vec { let mut env_vars = vec![]; diff --git a/crates/nu-protocol/src/engine/stack.rs b/crates/nu-protocol/src/engine/stack.rs index f898abffa4..d6d630bb11 100644 --- a/crates/nu-protocol/src/engine/stack.rs +++ b/crates/nu-protocol/src/engine/stack.rs @@ -1,7 +1,7 @@ use std::collections::{HashMap, HashSet}; use crate::engine::EngineState; -use crate::{Config, ShellError, Span, Value, VarId, CONFIG_VARIABLE_ID}; +use crate::{ShellError, Span, Value, VarId}; /// A runtime value stack used during evaluation /// @@ -97,11 +97,6 @@ impl Stack { output.env_vars = self.env_vars.clone(); output.env_vars.push(HashMap::new()); - let config = self - .get_var(CONFIG_VARIABLE_ID, Span::new(0, 0)) - .expect("internal error: config is missing"); - output.vars.insert(CONFIG_VARIABLE_ID, config); - output } @@ -122,11 +117,6 @@ impl Stack { output.env_vars = self.env_vars.clone(); output.env_vars.push(HashMap::new()); - let config = self - .get_var(CONFIG_VARIABLE_ID, fake_span) - .expect("internal error: config is missing"); - output.vars.insert(CONFIG_VARIABLE_ID, config); - output } @@ -212,27 +202,27 @@ impl Stack { } } - pub fn get_config(&self) -> Result { - let config = self.get_var(CONFIG_VARIABLE_ID, Span::new(0, 0)); + // pub fn get_config(&self) -> Result { + // let config = self.get_var(CONFIG_VARIABLE_ID, Span::new(0, 0)); - match config { - Ok(config) => config.into_config(), - Err(e) => Err(e), - } - } + // match config { + // Ok(config) => config.into_config(), + // Err(e) => Err(e), + // } + // } - pub fn update_config(&mut self, name: &str, value: Value) { - if let Some(Value::Record { cols, vals, .. }) = self.vars.get_mut(&CONFIG_VARIABLE_ID) { - for col_val in cols.iter().zip(vals.iter_mut()) { - if col_val.0 == name { - *col_val.1 = value; - return; - } - } - cols.push(name.to_string()); - vals.push(value); - } - } + // pub fn update_config(&mut self, name: &str, value: Value) { + // if let Some(Value::Record { cols, vals, .. }) = self.vars.get_mut(&CONFIG_VARIABLE_ID) { + // for col_val in cols.iter().zip(vals.iter_mut()) { + // if col_val.0 == name { + // *col_val.1 = value; + // return; + // } + // } + // cols.push(name.to_string()); + // vals.push(value); + // } + // } pub fn print_stack(&self) { println!("vars:"); diff --git a/crates/nu-protocol/src/lib.rs b/crates/nu-protocol/src/lib.rs index 2b20c5e035..9d8ab12668 100644 --- a/crates/nu-protocol/src/lib.rs +++ b/crates/nu-protocol/src/lib.rs @@ -15,7 +15,7 @@ mod value; mod variable; pub use config::*; -pub use engine::{CONFIG_VARIABLE_ID, ENV_VARIABLE_ID, IN_VARIABLE_ID, NU_VARIABLE_ID}; +pub use engine::{ENV_VARIABLE_ID, IN_VARIABLE_ID, NU_VARIABLE_ID}; pub use example::*; pub use exportable::*; pub use id::*; diff --git a/docs/sample_config/default_config.nu b/docs/sample_config/default_config.nu index 30d0f2ea28..39c30f866e 100644 --- a/docs/sample_config/default_config.nu +++ b/docs/sample_config/default_config.nu @@ -74,262 +74,3 @@ module completions { # Get just the extern definitions without the custom completion commands use completions * - -# for more information on themes see -# https://www.nushell.sh/book/coloring_and_theming.html -let default_theme = { - # color for nushell primitives - separator: white - leading_trailing_space_bg: { attr: n } # no fg, no bg, attr none effectively turns this off - header: green_bold - empty: blue - bool: white - int: white - filesize: white - duration: white - date: white - range: white - float: white - string: white - nothing: white - binary: white - cellpath: white - row_index: green_bold - record: white - list: white - block: white - hints: dark_gray - - # shapes are used to change the cli syntax highlighting - shape_garbage: { fg: "#FFFFFF" bg: "#FF0000" attr: b} - shape_binary: purple_bold - shape_bool: light_cyan - shape_int: purple_bold - shape_float: purple_bold - shape_range: yellow_bold - shape_internalcall: cyan_bold - shape_external: cyan - shape_externalarg: green_bold - shape_literal: blue - shape_operator: yellow - shape_signature: green_bold - shape_string: green - shape_string_interpolation: cyan_bold - shape_datetime: cyan_bold - shape_list: cyan_bold - shape_table: blue_bold - shape_record: cyan_bold - shape_block: blue_bold - shape_filepath: cyan - shape_globpattern: cyan_bold - shape_variable: purple - shape_flag: blue_bold - shape_custom: green - shape_nothing: light_cyan -} - -# The default config record. This is where much of your global configuration is setup. -let $config = { - filesize_metric: false - table_mode: rounded # basic, compact, compact_double, light, thin, with_love, rounded, reinforced, heavy, none, other - use_ls_colors: true - rm_always_trash: false - color_config: $default_theme - use_grid_icons: true - footer_mode: "25" # always, never, number_of_rows, auto - quick_completions: true # set this to false to prevent auto-selecting completions when only one remains - partial_completions: true # set this to false to prevent partial filling of the prompt - animate_prompt: false # redraw the prompt every second - float_precision: 2 - use_ansi_coloring: true - filesize_format: "auto" # b, kb, kib, mb, mib, gb, gib, tb, tib, pb, pib, eb, eib, zb, zib, auto - edit_mode: emacs # emacs, vi - max_history_size: 10000 # Session has to be reloaded for this to take effect - sync_history_on_enter: true # Enable to share the history between multiple sessions, else you have to close the session to persist history to file - menus: [ - # Configuration for default nushell menus - # Note the lack of souce parameter - { - name: completion_menu - only_buffer_difference: false - marker: "| " - type: { - layout: columnar - columns: 4 - col_width: 20 # Optional value. If missing all the screen width is used to calculate column width - col_padding: 2 - } - style: { - text: green - selected_text: green_reverse - description_text: yellow - } - } - { - name: history_menu - only_buffer_difference: true - marker: "? " - type: { - layout: list - page_size: 10 - } - style: { - text: green - selected_text: green_reverse - description_text: yellow - } - } - { - name: help_menu - only_buffer_difference: true - marker: "? " - type: { - layout: description - columns: 4 - col_width: 20 # Optional value. If missing all the screen width is used to calculate column width - col_padding: 2 - selection_rows: 4 - description_rows: 10 - } - style: { - text: green - selected_text: green_reverse - description_text: yellow - } - } - # Example of extra menus created using a nushell source - # Use the source field to create a list of records that populates - # the menu - { - name: commands_menu - only_buffer_difference: false - marker: "# " - type: { - layout: columnar - columns: 4 - col_width: 20 - col_padding: 2 - } - style: { - text: green - selected_text: green_reverse - description_text: yellow - } - source: { |buffer, position| - $nu.scope.commands - | where command =~ $buffer - | each { |it| {value: $it.command description: $it.usage} } - } - } - { - name: vars_menu - only_buffer_difference: true - marker: "# " - type: { - layout: list - page_size: 10 - } - style: { - text: green - selected_text: green_reverse - description_text: yellow - } - source: { |buffer, position| - $nu.scope.vars - | where name =~ $buffer - | sort-by name - | each { |it| {value: $it.name description: $it.type} } - } - } - { - name: commands_with_description - only_buffer_difference: true - marker: "# " - type: { - layout: description - columns: 4 - col_width: 20 - col_padding: 2 - selection_rows: 4 - description_rows: 10 - } - style: { - text: green - selected_text: green_reverse - description_text: yellow - } - source: { |buffer, position| - $nu.scope.commands - | where command =~ $buffer - | each { |it| {value: $it.command description: $it.usage} } - } - } - ] - keybindings: [ - { - name: completion_menu - modifier: none - keycode: tab - mode: emacs # Options: emacs vi_normal vi_insert - event: { - until: [ - { send: menu name: completion_menu } - { send: menunext } - ] - } - } - { - name: completion_previous - modifier: shift - keycode: backtab - mode: [emacs, vi_normal, vi_insert] # Note: You can add the same keybinding to all modes by using a list - event: { send: menuprevious } - } - { - name: history_menu - modifier: control - keycode: char_x - mode: emacs - event: { - until: [ - { send: menu name: history_menu } - { send: menupagenext } - ] - } - } - { - name: history_previous - modifier: control - keycode: char_z - mode: emacs - event: { - until: [ - { send: menupageprevious } - { edit: undo } - ] - } - } - # Keybindings used to trigger the user defined menus - { - name: commands_menu - modifier: control - keycode: char_t - mode: [emacs, vi_normal, vi_insert] - event: { send: menu name: commands_menu } - } - { - name: vars_menu - modifier: control - keycode: char_y - mode: [emacs, vi_normal, vi_insert] - event: { send: menu name: vars_menu } - } - { - name: commands_with_description - modifier: control - keycode: char_u - mode: [emacs, vi_normal, vi_insert] - event: { send: menu name: commands_with_description } - } - ] -} diff --git a/docs/sample_config/default_env.nu b/docs/sample_config/default_env.nu index 7a3946b000..3ff7eca893 100644 --- a/docs/sample_config/default_env.nu +++ b/docs/sample_config/default_env.nu @@ -56,3 +56,262 @@ let-env NU_PLUGIN_DIRS = [ # To add entries to PATH (on Windows you might use Path), you can use the following pattern: # let-env PATH = ($env.PATH | prepend '/some/path') + +# for more information on themes see +# https://www.nushell.sh/book/coloring_and_theming.html +let default_theme = { + # color for nushell primitives + separator: white + leading_trailing_space_bg: { attr: n } # no fg, no bg, attr none effectively turns this off + header: green_bold + empty: blue + bool: white + int: white + filesize: white + duration: white + date: white + range: white + float: white + string: white + nothing: white + binary: white + cellpath: white + row_index: green_bold + record: white + list: white + block: white + hints: dark_gray + + # shapes are used to change the cli syntax highlighting + shape_garbage: { fg: "#FFFFFF" bg: "#FF0000" attr: b} + shape_binary: purple_bold + shape_bool: light_cyan + shape_int: purple_bold + shape_float: purple_bold + shape_range: yellow_bold + shape_internalcall: cyan_bold + shape_external: cyan + shape_externalarg: green_bold + shape_literal: blue + shape_operator: yellow + shape_signature: green_bold + shape_string: green + shape_string_interpolation: cyan_bold + shape_datetime: cyan_bold + shape_list: cyan_bold + shape_table: blue_bold + shape_record: cyan_bold + shape_block: blue_bold + shape_filepath: cyan + shape_globpattern: cyan_bold + shape_variable: purple + shape_flag: blue_bold + shape_custom: green + shape_nothing: light_cyan +} + +# The default config record. This is where much of your global configuration is setup. +let-env config = { + filesize_metric: false + table_mode: rounded # basic, compact, compact_double, light, thin, with_love, rounded, reinforced, heavy, none, other + use_ls_colors: true + rm_always_trash: false + color_config: $default_theme + use_grid_icons: true + footer_mode: "25" # always, never, number_of_rows, auto + quick_completions: true # set this to false to prevent auto-selecting completions when only one remains + partial_completions: true # set this to false to prevent partial filling of the prompt + animate_prompt: false # redraw the prompt every second + float_precision: 2 + use_ansi_coloring: true + filesize_format: "auto" # b, kb, kib, mb, mib, gb, gib, tb, tib, pb, pib, eb, eib, zb, zib, auto + edit_mode: emacs # emacs, vi + max_history_size: 10000 # Session has to be reloaded for this to take effect + sync_history_on_enter: true # Enable to share the history between multiple sessions, else you have to close the session to persist history to file + menus: [ + # Configuration for default nushell menus + # Note the lack of souce parameter + { + name: completion_menu + only_buffer_difference: false + marker: "| " + type: { + layout: columnar + columns: 4 + col_width: 20 # Optional value. If missing all the screen width is used to calculate column width + col_padding: 2 + } + style: { + text: green + selected_text: green_reverse + description_text: yellow + } + } + { + name: history_menu + only_buffer_difference: true + marker: "? " + type: { + layout: list + page_size: 10 + } + style: { + text: green + selected_text: green_reverse + description_text: yellow + } + } + { + name: help_menu + only_buffer_difference: true + marker: "? " + type: { + layout: description + columns: 4 + col_width: 20 # Optional value. If missing all the screen width is used to calculate column width + col_padding: 2 + selection_rows: 4 + description_rows: 10 + } + style: { + text: green + selected_text: green_reverse + description_text: yellow + } + } + # Example of extra menus created using a nushell source + # Use the source field to create a list of records that populates + # the menu + { + name: commands_menu + only_buffer_difference: false + marker: "# " + type: { + layout: columnar + columns: 4 + col_width: 20 + col_padding: 2 + } + style: { + text: green + selected_text: green_reverse + description_text: yellow + } + source: { |buffer, position| + $nu.scope.commands + | where command =~ $buffer + | each { |it| {value: $it.command description: $it.usage} } + } + } + { + name: vars_menu + only_buffer_difference: true + marker: "# " + type: { + layout: list + page_size: 10 + } + style: { + text: green + selected_text: green_reverse + description_text: yellow + } + source: { |buffer, position| + $nu.scope.vars + | where name =~ $buffer + | sort-by name + | each { |it| {value: $it.name description: $it.type} } + } + } + { + name: commands_with_description + only_buffer_difference: true + marker: "# " + type: { + layout: description + columns: 4 + col_width: 20 + col_padding: 2 + selection_rows: 4 + description_rows: 10 + } + style: { + text: green + selected_text: green_reverse + description_text: yellow + } + source: { |buffer, position| + $nu.scope.commands + | where command =~ $buffer + | each { |it| {value: $it.command description: $it.usage} } + } + } + ] + keybindings: [ + { + name: completion_menu + modifier: none + keycode: tab + mode: emacs # Options: emacs vi_normal vi_insert + event: { + until: [ + { send: menu name: completion_menu } + { send: menunext } + ] + } + } + { + name: completion_previous + modifier: shift + keycode: backtab + mode: [emacs, vi_normal, vi_insert] # Note: You can add the same keybinding to all modes by using a list + event: { send: menuprevious } + } + { + name: history_menu + modifier: control + keycode: char_x + mode: emacs + event: { + until: [ + { send: menu name: history_menu } + { send: menupagenext } + ] + } + } + { + name: history_previous + modifier: control + keycode: char_z + mode: emacs + event: { + until: [ + { send: menupageprevious } + { edit: undo } + ] + } + } + # Keybindings used to trigger the user defined menus + { + name: commands_menu + modifier: control + keycode: char_t + mode: [emacs, vi_normal, vi_insert] + event: { send: menu name: commands_menu } + } + { + name: vars_menu + modifier: control + keycode: char_y + mode: [emacs, vi_normal, vi_insert] + event: { send: menu name: vars_menu } + } + { + name: commands_with_description + modifier: control + keycode: char_u + mode: [emacs, vi_normal, vi_insert] + event: { send: menu name: commands_with_description } + } + ] +} diff --git a/src/main.rs b/src/main.rs index d372eca0ce..e028a5d372 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ use nu_protocol::{ ast::{Call, Expr, Expression}, engine::{Command, EngineState, Stack, StateWorkingSet}, Category, Example, IntoPipelineData, PipelineData, RawStream, ShellError, Signature, Span, - Spanned, SyntaxShape, Value, CONFIG_VARIABLE_ID, + Spanned, SyntaxShape, Value, }; use std::cell::RefCell; use std::{ @@ -196,15 +196,6 @@ fn main() -> Result<()> { gather_parent_env_vars(&mut engine_state); let mut stack = nu_protocol::engine::Stack::new(); - stack.vars.insert( - CONFIG_VARIABLE_ID, - Value::Record { - cols: vec![], - vals: vec![], - span: Span::new(0, 0), - }, - ); - if let Some(commands) = &binary_args.commands { #[cfg(feature = "plugin")] read_plugin_file( @@ -317,14 +308,6 @@ fn parse_commandline_args( let _ = engine_state.merge_delta(delta, None, init_cwd); let mut stack = Stack::new(); - stack.add_var( - CONFIG_VARIABLE_ID, - Value::Record { - cols: vec![], - vals: vec![], - span: Span::new(0, 0), - }, - ); // We should have a successful parse now if let Some(pipeline) = block.pipelines.get(0) { diff --git a/src/tests/test_engine.rs b/src/tests/test_engine.rs index db313f9a44..9bacb6ae53 100644 --- a/src/tests/test_engine.rs +++ b/src/tests/test_engine.rs @@ -13,33 +13,6 @@ fn proper_shadow() -> TestResult { run_test("let x = 10; let x = $x + 9; $x", "19") } -#[test] -fn config_filesize_format_with_metric_true() -> TestResult { - // Note: this tests both the config variable and that it is properly captured into a block - run_test( - r#"let config = {"filesize_metric": true "filesize_format": "kib" }; do { 40kb | into string } "#, - "39.1 KiB", - ) -} - -#[test] -fn config_filesize_format_with_metric_false_kib() -> TestResult { - // Note: this tests both the config variable and that it is properly captured into a block - run_test( - r#"let config = {"filesize_metric": false "filesize_format": "kib" }; do { 40kb | into string } "#, - "39.1 KiB", - ) -} - -#[test] -fn config_filesize_format_with_metric_false_kb() -> TestResult { - // Note: this tests both the config variable and that it is properly captured into a block - run_test( - r#"let config = {"filesize_metric": false "filesize_format": "kb" }; do { 40kb | into string } "#, - "40.0 KB", - ) -} - #[test] fn in_variable_1() -> TestResult { run_test(r#"[3] | if $in.0 > 4 { "yay!" } else { "boo" }"#, "boo")