From b01f50bd7000389aa22ba3623e1f48ef2fcd4813 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Mon, 19 Dec 2022 20:34:21 +0300 Subject: [PATCH] nu-explore/ Fix configuration issues (#7520) close #7518 Signed-off-by: Maxim Zhiburt --- crates/nu-color-config/src/color_config.rs | 60 ++++++++++++++----- crates/nu-command/src/viewers/explore.rs | 52 ++++------------ crates/nu-explore/src/commands/config_show.rs | 53 +++++++++++++++- 3 files changed, 107 insertions(+), 58 deletions(-) diff --git a/crates/nu-color-config/src/color_config.rs b/crates/nu-color-config/src/color_config.rs index 6d605e5e66..e8045386c1 100644 --- a/crates/nu-color-config/src/color_config.rs +++ b/crates/nu-color-config/src/color_config.rs @@ -19,29 +19,61 @@ pub fn lookup_ansi_color_style(s: &str) -> Style { } } -// These two are used only for Explore's very limited color config -fn update_hashmap(key: &str, val: &str, hm: &mut HashMap) { - // eprintln!("key: {}, val: {}", &key, &val); - let color = lookup_ansi_color_style(val); - if let Some(v) = hm.get_mut(key) { - *v = color; - } else { - hm.insert(key.to_string(), color); - } -} - pub fn get_color_map(colors: &HashMap) -> HashMap { let mut hm: HashMap = HashMap::new(); for (key, value) in colors { - if let Value::String { val, .. } = value { - update_hashmap(key, val, &mut hm); - } + parse_map_entry(&mut hm, key, value); } hm } +fn parse_map_entry(hm: &mut HashMap, key: &str, value: &Value) { + let value = match value { + Value::String { val, .. } => Some(lookup_ansi_color_style(val)), + Value::Record { cols, vals, .. } => get_style_from_value(cols, vals).map(parse_nustyle), + _ => None, + }; + if let Some(value) = value { + hm.entry(key.to_owned()).or_insert(value); + } +} + +fn get_style_from_value(cols: &[String], vals: &[Value]) -> Option { + let mut was_set = false; + let mut style = NuStyle::from(Style::default()); + for (col, val) in cols.iter().zip(vals) { + match col.as_str() { + "bg" => { + if let Value::String { val, .. } = val { + style.bg = Some(val.clone()); + was_set = true; + } + } + "fg" => { + if let Value::String { val, .. } = val { + style.fg = Some(val.clone()); + was_set = true; + } + } + "attr" => { + if let Value::String { val, .. } = val { + style.attr = Some(val.clone()); + was_set = true; + } + } + _ => (), + } + } + + if was_set { + Some(style) + } else { + None + } +} + fn color_string_to_nustyle(color_string: String) -> Style { // eprintln!("color_string: {}", &color_string); if color_string.is_empty() { diff --git a/crates/nu-command/src/viewers/explore.rs b/crates/nu-command/src/viewers/explore.rs index 5fe14948ba..6e84948494 100644 --- a/crates/nu-command/src/viewers/explore.rs +++ b/crates/nu-command/src/viewers/explore.rs @@ -72,9 +72,9 @@ impl Command for Explore { let style_computer = StyleComputer::from_config(engine_state, stack); let mut config = nu_config.explore.clone(); - prepare_default_config(&mut config); - update_config(&mut config, show_index, show_head); include_nu_config(&mut config, &style_computer); + update_config(&mut config, show_index, show_head); + prepare_default_config(&mut config); let show_banner = is_need_banner(&config).unwrap_or(true); let exit_esc = is_need_esc_exit(&config).unwrap_or(true); @@ -293,12 +293,13 @@ fn prepare_default_config(config: &mut HashMap) { } fn parse_hash_map(value: &Value) -> Option> { - value - .as_string() - .ok() - .and_then(|s| nu_json::from_str::(&s).ok()) - .map(convert_json_value_into_value) - .and_then(|v| create_map(&v)) + value.as_record().ok().map(|(cols, vals)| { + cols.iter() + .take(vals.len()) + .zip(vals) + .map(|(col, val)| (col.clone(), val.clone())) + .collect::>() + }) } const fn color(foreground: Option, background: Option) -> Style { @@ -339,39 +340,6 @@ fn insert_bool(map: &mut HashMap, key: &str, value: bool) { map.insert(String::from(key), Value::boolean(value, Span::unknown())); } -fn convert_json_value_into_value(value: nu_json::Value) -> Value { - match value { - nu_json::Value::Null => Value::nothing(Span::unknown()), - nu_json::Value::Bool(val) => Value::boolean(val, Span::unknown()), - nu_json::Value::I64(val) => Value::int(val, Span::unknown()), - nu_json::Value::U64(val) => Value::int(val as i64, Span::unknown()), - nu_json::Value::F64(val) => Value::float(val, Span::unknown()), - nu_json::Value::String(val) => Value::string(val, Span::unknown()), - nu_json::Value::Array(val) => { - let vals = val - .into_iter() - .map(convert_json_value_into_value) - .collect::>(); - - Value::List { - vals, - span: Span::unknown(), - } - } - nu_json::Value::Object(val) => { - let hm = val - .into_iter() - .map(|(key, value)| { - let val = convert_json_value_into_value(value); - (key, val) - }) - .collect(); - - map_into_value(hm) - } - } -} - fn include_nu_config(config: &mut HashMap, style_computer: &StyleComputer) { let line_color = lookup_color(style_computer, "separator"); if line_color != nu_ansi_term::Style::default() { @@ -398,7 +366,9 @@ fn include_nu_config(config: &mut HashMap, style_computer: &Style .get("config") .and_then(parse_hash_map) .unwrap_or_default(); + insert_style(&mut map, "border_color", line_color); + config.insert(String::from("config"), map_into_value(map)); } } diff --git a/crates/nu-explore/src/commands/config_show.rs b/crates/nu-explore/src/commands/config_show.rs index c4547b2b56..1d43bee24b 100644 --- a/crates/nu-explore/src/commands/config_show.rs +++ b/crates/nu-explore/src/commands/config_show.rs @@ -1,4 +1,4 @@ -use std::io::Result; +use std::{collections::HashMap, io::Result}; use nu_protocol::{ engine::{EngineState, Stack}, @@ -7,7 +7,7 @@ use nu_protocol::{ use tui::layout::Rect; use crate::{ - nu_common::try_build_table, + nu_common::{try_build_table, NuSpan}, pager::Frame, util::map_into_value, views::{Layout, Preview, View, ViewConfig}, @@ -128,10 +128,57 @@ impl ConfigView { fn create_output_string(&mut self, config: ViewConfig) -> String { match self.format { ConfigFormat::Table => { - let value = map_into_value(config.config.clone()); + let mut m = config.config.clone(); + convert_styles(&mut m); + + let value = map_into_value(m); try_build_table(None, config.nu_config, config.style_computer, value) } ConfigFormat::Nu => nu_json::to_string(&config.config).unwrap_or_default(), } } } + +fn convert_styles(m: &mut HashMap) { + for value in m.values_mut() { + convert_styles_value(value); + } +} + +fn convert_styles_value(value: &mut Value) { + match value { + Value::String { val, .. } => { + if let Some(v) = convert_style_from_string(val) { + *value = v; + } + } + Value::List { vals, .. } => { + for value in vals { + convert_styles_value(value); + } + } + Value::Record { vals, .. } => { + for value in vals { + convert_styles_value(value); + } + } + _ => (), + } +} + +fn convert_style_from_string(s: &str) -> Option { + let style = nu_json::from_str::(s).ok()?; + let cols = vec![String::from("bg"), String::from("fg"), String::from("attr")]; + + let vals = vec![ + Value::string(style.bg.unwrap_or_default(), NuSpan::unknown()), + Value::string(style.fg.unwrap_or_default(), NuSpan::unknown()), + Value::string(style.attr.unwrap_or_default(), NuSpan::unknown()), + ]; + + Some(Value::Record { + cols, + vals, + span: NuSpan::unknown(), + }) +}