From a7274115d00c95793d020ca81247d10d65184079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20N=2E=20Robalino?= Date: Thu, 8 Apr 2021 17:31:19 -0500 Subject: [PATCH] make Table, Autoview read in memory config. (#3287) --- .../src/commands/autoview/command.rs | 6 +- .../nu-command/src/commands/table/command.rs | 21 ++- crates/nu-data/src/primitive.rs | 134 +++++++++--------- crates/nu-engine/src/config_holder.rs | 7 + 4 files changed, 91 insertions(+), 77 deletions(-) diff --git a/crates/nu-command/src/commands/autoview/command.rs b/crates/nu-command/src/commands/autoview/command.rs index 285dce6eaa..4eeb508497 100644 --- a/crates/nu-command/src/commands/autoview/command.rs +++ b/crates/nu-command/src/commands/autoview/command.rs @@ -1,4 +1,4 @@ -use crate::commands::autoview::options::{ConfigExtensions, NuConfig as AutoViewConfiguration}; +use crate::commands::autoview::options::ConfigExtensions; use crate::prelude::*; use crate::primitive::get_color_config; use nu_data::value::format_leaf; @@ -44,7 +44,7 @@ impl WholeStreamCommand for Command { } pub fn autoview(context: CommandArgs) -> Result { - let configuration = AutoViewConfiguration::new(); + let configuration = context.configs.lock().global_config(); let binary = context.scope.get_command("binaryview"); let text = context.scope.get_command("textview"); @@ -213,7 +213,7 @@ pub fn autoview(context: CommandArgs) -> Result { ]); } - let color_hm = get_color_config(); + let color_hm = get_color_config(&configuration); let table = nu_table::Table::new(vec![], entries, nu_table::Theme::compact()); diff --git a/crates/nu-command/src/commands/table/command.rs b/crates/nu-command/src/commands/table/command.rs index 28d43ee489..a7fb1fd288 100644 --- a/crates/nu-command/src/commands/table/command.rs +++ b/crates/nu-command/src/commands/table/command.rs @@ -1,4 +1,4 @@ -use crate::commands::table::options::{ConfigExtensions, NuConfig as TableConfiguration}; +use crate::commands::table::options::{ConfigExtensions, NuConfig}; use crate::prelude::*; use crate::primitive::get_color_config; use nu_data::value::{format_leaf, style_leaf}; @@ -41,13 +41,13 @@ impl WholeStreamCommand for Command { } fn run(&self, args: CommandArgs) -> Result { - table(TableConfiguration::new(), args) + table(args) } } pub fn from_list( values: &[Value], - configuration: &TableConfiguration, + configuration: &NuConfig, starting_idx: usize, color_hm: &HashMap, ) -> nu_table::Table { @@ -56,7 +56,13 @@ pub fn from_list( .into_iter() .map(|x| StyledString::new(x, header_style)) .collect(); - let entries = values_to_entries(values, &mut headers, configuration, starting_idx, &color_hm); + let entries = values_to_entries( + values, + &mut headers, + &configuration, + starting_idx, + &color_hm, + ); nu_table::Table { headers, data: entries, @@ -67,7 +73,7 @@ pub fn from_list( fn values_to_entries( values: &[Value], headers: &mut Vec, - configuration: &TableConfiguration, + configuration: &NuConfig, starting_idx: usize, color_hm: &HashMap, ) -> Vec> { @@ -158,8 +164,9 @@ fn values_to_entries( entries } -fn table(configuration: TableConfiguration, args: CommandArgs) -> Result { +fn table(args: CommandArgs) -> Result { let mut args = args.evaluate_once()?; + let configuration = args.configs.lock().global_config(); // Ideally, get_color_config would get all the colors configured in the config.toml // and create a style based on those settings. However, there are few places where @@ -167,7 +174,7 @@ fn table(configuration: TableConfiguration, args: CommandArgs) -> Result) { } } -pub fn get_color_config() -> HashMap { +pub fn get_color_config(config: &NuConfig) -> HashMap { + let config = &config.vars; + // create the hashmap let mut hm: HashMap = HashMap::new(); // set some defaults @@ -150,72 +152,70 @@ pub fn get_color_config() -> HashMap { ); // populate hashmap from config values - if let Ok(config) = crate::config::config(Tag::unknown()) { - if let Some(primitive_color_vars) = config.get("color_config") { - for (key, value) in primitive_color_vars.row_entries() { - match key.as_ref() { - "primitive_int" => { - update_hashmap(&key, &value, &mut hm); - } - "primitive_decimal" => { - update_hashmap(&key, &value, &mut hm); - } - "primitive_filesize" => { - update_hashmap(&key, &value, &mut hm); - } - "primitive_string" => { - update_hashmap(&key, &value, &mut hm); - } - "primitive_line" => { - update_hashmap(&key, &value, &mut hm); - } - "primitive_columnpath" => { - update_hashmap(&key, &value, &mut hm); - } - "primitive_pattern" => { - update_hashmap(&key, &value, &mut hm); - } - "primitive_boolean" => { - update_hashmap(&key, &value, &mut hm); - } - "primitive_date" => { - update_hashmap(&key, &value, &mut hm); - } - "primitive_duration" => { - update_hashmap(&key, &value, &mut hm); - } - "primitive_range" => { - update_hashmap(&key, &value, &mut hm); - } - "primitive_path" => { - update_hashmap(&key, &value, &mut hm); - } - "primitive_binary" => { - update_hashmap(&key, &value, &mut hm); - } - "separator_color" => { - update_hashmap(&key, &value, &mut hm); - } - "header_align" => { - update_hashmap(&key, &value, &mut hm); - } - "header_color" => { - update_hashmap(&key, &value, &mut hm); - } - "header_bold" => { - update_hashmap(&key, &value, &mut hm); - } - "header_style" => { - update_hashmap(&key, &value, &mut hm); - } - "index_color" => { - update_hashmap(&key, &value, &mut hm); - } - "leading_trailing_space_bg" => { - update_hashmap(&key, &value, &mut hm); - } - _ => (), + if let Some(primitive_color_vars) = config.get("color_config") { + for (key, value) in primitive_color_vars.row_entries() { + match key.as_ref() { + "primitive_int" => { + update_hashmap(&key, &value, &mut hm); } + "primitive_decimal" => { + update_hashmap(&key, &value, &mut hm); + } + "primitive_filesize" => { + update_hashmap(&key, &value, &mut hm); + } + "primitive_string" => { + update_hashmap(&key, &value, &mut hm); + } + "primitive_line" => { + update_hashmap(&key, &value, &mut hm); + } + "primitive_columnpath" => { + update_hashmap(&key, &value, &mut hm); + } + "primitive_pattern" => { + update_hashmap(&key, &value, &mut hm); + } + "primitive_boolean" => { + update_hashmap(&key, &value, &mut hm); + } + "primitive_date" => { + update_hashmap(&key, &value, &mut hm); + } + "primitive_duration" => { + update_hashmap(&key, &value, &mut hm); + } + "primitive_range" => { + update_hashmap(&key, &value, &mut hm); + } + "primitive_path" => { + update_hashmap(&key, &value, &mut hm); + } + "primitive_binary" => { + update_hashmap(&key, &value, &mut hm); + } + "separator_color" => { + update_hashmap(&key, &value, &mut hm); + } + "header_align" => { + update_hashmap(&key, &value, &mut hm); + } + "header_color" => { + update_hashmap(&key, &value, &mut hm); + } + "header_bold" => { + update_hashmap(&key, &value, &mut hm); + } + "header_style" => { + update_hashmap(&key, &value, &mut hm); + } + "index_color" => { + update_hashmap(&key, &value, &mut hm); + } + "leading_trailing_space_bg" => { + update_hashmap(&key, &value, &mut hm); + } + _ => (), } } } diff --git a/crates/nu-engine/src/config_holder.rs b/crates/nu-engine/src/config_holder.rs index ff3d8fee59..d6079a3265 100644 --- a/crates/nu-engine/src/config_holder.rs +++ b/crates/nu-engine/src/config_holder.rs @@ -24,6 +24,13 @@ impl ConfigHolder { } } + pub fn global_config(&self) -> NuConfig { + match &self.global_config { + Some(config) => config.clone(), + None => NuConfig::default(), + } + } + pub fn add_local_cfg(&mut self, cfg: NuConfig) { self.local_configs.push(cfg); }