From 57a26bbd429f4fa085c4fb5a9c12b18f376db531 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Tue, 1 Sep 2020 19:08:41 -0500 Subject: [PATCH] Default alignment (#2481) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * WIP - compiling but not working * semi-working * making progress * working except for table lines * fmt + clippy * cleaned up some comments * working line colors * fmt, clippy, updated sample config.toml * merges * fixed bug where no config.toml or not set settings made weird defaults. * clippy & fmt again * Header default alignment is left. Co-authored-by: Andrés N. Robalino --- .../nu-cli/src/commands/autoview/command.rs | 2 +- crates/nu-cli/src/commands/table/command.rs | 2 +- crates/nu-cli/src/commands/table/options.rs | 35 ++++++----- crates/nu-data/src/primitive.rs | 61 ++++++++++--------- crates/nu-data/src/value.rs | 2 +- crates/nu-table/src/main.rs | 2 +- crates/nu-table/src/table.rs | 10 ++- 7 files changed, 63 insertions(+), 51 deletions(-) diff --git a/crates/nu-cli/src/commands/autoview/command.rs b/crates/nu-cli/src/commands/autoview/command.rs index 8217e07878..fdd0e6814b 100644 --- a/crates/nu-cli/src/commands/autoview/command.rs +++ b/crates/nu-cli/src/commands/autoview/command.rs @@ -266,7 +266,7 @@ pub async fn autoview(context: RunnableContext) -> Result) -> nu_table::Ali } } -pub fn get_color_from_key_and_subkey(config: &NuConfig, key: &str, subkey: &str) -> Value { +pub fn get_color_from_key_and_subkey(config: &NuConfig, key: &str, subkey: &str) -> Option { let vars = config.vars.lock(); - let mut v: Value = - UntaggedValue::Primitive(nu_protocol::Primitive::String("nocolor".to_string())) - .into_value(Tag::unknown()); if let Some(config_vars) = vars.get(key) { for (kee, value) in config_vars.row_entries() { if kee == subkey { - v = value.to_owned(); + return Some(value.clone()); } } } - v + None } pub fn header_bold_from_value(bold_value: Option<&Value>) -> bool { @@ -76,15 +72,22 @@ impl ConfigExtensions for NuConfig { fn header_style(&self) -> TextStyle { // FIXME: I agree, this is the long way around, please suggest and alternative. let head_color = get_color_from_key_and_subkey(self, "color_config", "header_color"); - let head_color_style = lookup_ansi_color_style( - head_color - .as_string() - .unwrap_or_else(|_| "green".to_string()), - ); + let head_color_style = match head_color { + Some(s) => { + lookup_ansi_color_style(s.as_string().unwrap_or_else(|_| "green".to_string())) + } + None => ansi_term::Color::Green.normal(), + }; let head_bold = get_color_from_key_and_subkey(self, "color_config", "header_bold"); - let head_bold_bool = header_bold_from_value(Some(&head_bold)); + let head_bold_bool = match head_bold { + Some(b) => header_bold_from_value(Some(&b)), + None => true, + }; let head_align = get_color_from_key_and_subkey(self, "color_config", "header_align"); - let head_alignment = header_alignment_from_value(Some(&head_align)); + let head_alignment = match head_align { + Some(a) => header_alignment_from_value(Some(&a)), + None => Alignment::Center, + }; TextStyle::new() .alignment(head_alignment) diff --git a/crates/nu-data/src/primitive.rs b/crates/nu-data/src/primitive.rs index 429cbd4335..d84e77a8c9 100644 --- a/crates/nu-data/src/primitive.rs +++ b/crates/nu-data/src/primitive.rs @@ -122,11 +122,11 @@ pub fn get_color_config() -> HashMap { hm.insert("primitive_path".to_string(), Color::White.normal()); hm.insert("primitive_binary".to_string(), Color::White.normal()); hm.insert("separator_color".to_string(), Color::White.normal()); - hm.insert("header_align".to_string(), Color::White.normal()); - hm.insert("header_color".to_string(), Color::White.normal()); - hm.insert("header_bold".to_string(), Color::White.normal()); + hm.insert("header_align".to_string(), Color::Green.bold()); + hm.insert("header_color".to_string(), Color::Green.bold()); + hm.insert("header_bold".to_string(), Color::Green.bold()); hm.insert("header_style".to_string(), Style::default()); - hm.insert("index_color".to_string(), Color::Green.normal()); + hm.insert("index_color".to_string(), Color::Green.bold()); // populate hashmap from config values if let Ok(config) = crate::config::config(Tag::unknown()) { @@ -228,135 +228,138 @@ pub fn style_primitive(primitive: &str, color_hm: &HashMap) -> Te let style = color_hm.get("Primitive::String"); match style { Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + None => TextStyle::basic_left(), } } "Line" => { let style = color_hm.get("Primitive::Line"); match style { Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + None => TextStyle::basic_left(), } } "ColumnPath" => { let style = color_hm.get("Primitive::ColumnPath"); match style { Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + None => TextStyle::basic_left(), } } "Pattern" => { let style = color_hm.get("Primitive::Pattern"); match style { Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + None => TextStyle::basic_left(), } } "Boolean" => { let style = color_hm.get("Primitive::Boolean"); match style { Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + None => TextStyle::basic_left(), } } "Date" => { let style = color_hm.get("Primitive::Date"); match style { Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + None => TextStyle::basic_left(), } } "Duration" => { let style = color_hm.get("Primitive::Duration"); match style { Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + None => TextStyle::basic_left(), } } "Range" => { let style = color_hm.get("Primitive::Range"); match style { Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + None => TextStyle::basic_left(), } } "Path" => { let style = color_hm.get("Primitive::Path"); match style { Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + None => TextStyle::basic_left(), } } "Binary" => { let style = color_hm.get("Primitive::Binary"); match style { Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + None => TextStyle::basic_left(), } } "BeginningOfStream" => { let style = color_hm.get("Primitive::BeginningOfStream"); match style { Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + None => TextStyle::basic_left(), } } "EndOfStream" => { let style = color_hm.get("Primitive::EndOfStream"); match style { Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + None => TextStyle::basic_left(), } } "Nothing" => { let style = color_hm.get("Primitive::Nothing"); match style { Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + None => TextStyle::basic_left(), } } "separator_color" => { let style = color_hm.get("separator"); match style { Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + None => TextStyle::basic_left(), } } "header_align" => { let style = color_hm.get("header_align"); match style { - Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + Some(s) => TextStyle::with_style(Alignment::Center, *s), + None => TextStyle::default_header(), } } "header_color" => { let style = color_hm.get("header_color"); match style { - Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + Some(s) => TextStyle::with_style(Alignment::Center, *s), + None => TextStyle::default_header(), } } "header_bold" => { let style = color_hm.get("header_bold"); match style { - Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + Some(s) => TextStyle::with_style(Alignment::Center, *s), + None => TextStyle::default_header(), } } "header_style" => { let style = color_hm.get("header_style"); match style { - Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + Some(s) => TextStyle::with_style(Alignment::Center, *s), + None => TextStyle::default_header(), } } "index_color" => { let style = color_hm.get("index_color"); match style { - Some(s) => TextStyle::with_style(Alignment::Left, *s), - None => TextStyle::basic_right(), + Some(s) => TextStyle::with_style(Alignment::Right, *s), + None => TextStyle::new() + .alignment(Alignment::Right) + .fg(Color::Green) + .bold(Some(true)), } } - _ => TextStyle::basic(), + _ => TextStyle::basic_center(), } } diff --git a/crates/nu-data/src/value.rs b/crates/nu-data/src/value.rs index 742d2d4a03..292140ecbb 100644 --- a/crates/nu-data/src/value.rs +++ b/crates/nu-data/src/value.rs @@ -260,7 +260,7 @@ pub fn style_leaf<'a>( let prim_type = str[0..paren_index].to_string(); style_primitive(&prim_type, &color_hash_map) } - _ => TextStyle::basic(), + _ => TextStyle::basic_left(), } } diff --git a/crates/nu-table/src/main.rs b/crates/nu-table/src/main.rs index 5e4324e41d..ba1f2ec52f 100644 --- a/crates/nu-table/src/main.rs +++ b/crates/nu-table/src/main.rs @@ -7,7 +7,7 @@ fn main() { let width = args[1].parse::().expect("Need a width in columns"); let msg = args[2..] .iter() - .map(|x| StyledString::new(x.to_owned(), TextStyle::basic())) + .map(|x| StyledString::new(x.to_owned(), TextStyle::basic_left())) .collect(); let t = Table::new( diff --git a/crates/nu-table/src/table.rs b/crates/nu-table/src/table.rs index 5c5822190e..55815f2959 100644 --- a/crates/nu-table/src/table.rs +++ b/crates/nu-table/src/table.rs @@ -217,9 +217,9 @@ impl TextStyle { } } - pub fn basic() -> TextStyle { + pub fn basic_center() -> TextStyle { TextStyle::new() - .alignment(Alignment::Left) + .alignment(Alignment::Center) .style(Style::default()) } @@ -229,6 +229,12 @@ impl TextStyle { .style(Style::default()) } + pub fn basic_left() -> TextStyle { + TextStyle::new() + .alignment(Alignment::Left) + .style(Style::default()) + } + pub fn default_header() -> TextStyle { TextStyle::new() .alignment(Alignment::Center)