Default alignment (#2481)

* 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 <andres@androbtech.com>
This commit is contained in:
Darren Schroeder 2020-09-01 19:08:41 -05:00 committed by GitHub
parent f9acb7a7a5
commit 57a26bbd42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 63 additions and 51 deletions

View file

@ -266,7 +266,7 @@ pub async fn autoview(context: RunnableContext) -> Result<OutputStream, ShellErr
), ),
nu_table::StyledString::new( nu_table::StyledString::new(
format_leaf(value).plain_string(100_000), format_leaf(value).plain_string(100_000),
nu_table::TextStyle::basic(), nu_table::TextStyle::basic_left(),
), ),
]); ]);
} }

View file

@ -72,7 +72,7 @@ fn values_to_entries(
let mut entries = vec![]; let mut entries = vec![];
if headers.is_empty() { if headers.is_empty() {
headers.push(StyledString::new("".to_string(), TextStyle::basic())); headers.push(StyledString::new("".to_string(), TextStyle::basic_left()));
} }
for (idx, value) in values.iter().enumerate() { for (idx, value) in values.iter().enumerate() {

View file

@ -1,8 +1,7 @@
pub use nu_data::config::NuConfig; pub use nu_data::config::NuConfig;
use nu_data::primitive::lookup_ansi_color_style; use nu_data::primitive::lookup_ansi_color_style;
use nu_protocol::{UntaggedValue, Value}; use nu_protocol::Value;
use nu_source::Tag; use nu_table::{Alignment, TextStyle};
use nu_table::TextStyle;
use std::fmt::Debug; use std::fmt::Debug;
pub trait ConfigExtensions: Debug + Send { pub trait ConfigExtensions: Debug + Send {
@ -27,21 +26,18 @@ pub fn header_alignment_from_value(align_value: Option<&Value>) -> 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<Value> {
let vars = config.vars.lock(); 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) { if let Some(config_vars) = vars.get(key) {
for (kee, value) in config_vars.row_entries() { for (kee, value) in config_vars.row_entries() {
if kee == subkey { if kee == subkey {
v = value.to_owned(); return Some(value.clone());
} }
} }
} }
v None
} }
pub fn header_bold_from_value(bold_value: Option<&Value>) -> bool { pub fn header_bold_from_value(bold_value: Option<&Value>) -> bool {
@ -76,15 +72,22 @@ impl ConfigExtensions for NuConfig {
fn header_style(&self) -> TextStyle { fn header_style(&self) -> TextStyle {
// FIXME: I agree, this is the long way around, please suggest and alternative. // 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 = get_color_from_key_and_subkey(self, "color_config", "header_color");
let head_color_style = lookup_ansi_color_style( let head_color_style = match head_color {
head_color Some(s) => {
.as_string() lookup_ansi_color_style(s.as_string().unwrap_or_else(|_| "green".to_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 = 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_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() TextStyle::new()
.alignment(head_alignment) .alignment(head_alignment)

View file

@ -122,11 +122,11 @@ pub fn get_color_config() -> HashMap<String, Style> {
hm.insert("primitive_path".to_string(), Color::White.normal()); hm.insert("primitive_path".to_string(), Color::White.normal());
hm.insert("primitive_binary".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("separator_color".to_string(), Color::White.normal());
hm.insert("header_align".to_string(), Color::White.normal()); hm.insert("header_align".to_string(), Color::Green.bold());
hm.insert("header_color".to_string(), Color::White.normal()); hm.insert("header_color".to_string(), Color::Green.bold());
hm.insert("header_bold".to_string(), Color::White.normal()); hm.insert("header_bold".to_string(), Color::Green.bold());
hm.insert("header_style".to_string(), Style::default()); 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 // populate hashmap from config values
if let Ok(config) = crate::config::config(Tag::unknown()) { if let Ok(config) = crate::config::config(Tag::unknown()) {
@ -228,135 +228,138 @@ pub fn style_primitive(primitive: &str, color_hm: &HashMap<String, Style>) -> Te
let style = color_hm.get("Primitive::String"); let style = color_hm.get("Primitive::String");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(), None => TextStyle::basic_left(),
} }
} }
"Line" => { "Line" => {
let style = color_hm.get("Primitive::Line"); let style = color_hm.get("Primitive::Line");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(), None => TextStyle::basic_left(),
} }
} }
"ColumnPath" => { "ColumnPath" => {
let style = color_hm.get("Primitive::ColumnPath"); let style = color_hm.get("Primitive::ColumnPath");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(), None => TextStyle::basic_left(),
} }
} }
"Pattern" => { "Pattern" => {
let style = color_hm.get("Primitive::Pattern"); let style = color_hm.get("Primitive::Pattern");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(), None => TextStyle::basic_left(),
} }
} }
"Boolean" => { "Boolean" => {
let style = color_hm.get("Primitive::Boolean"); let style = color_hm.get("Primitive::Boolean");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(), None => TextStyle::basic_left(),
} }
} }
"Date" => { "Date" => {
let style = color_hm.get("Primitive::Date"); let style = color_hm.get("Primitive::Date");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(), None => TextStyle::basic_left(),
} }
} }
"Duration" => { "Duration" => {
let style = color_hm.get("Primitive::Duration"); let style = color_hm.get("Primitive::Duration");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(), None => TextStyle::basic_left(),
} }
} }
"Range" => { "Range" => {
let style = color_hm.get("Primitive::Range"); let style = color_hm.get("Primitive::Range");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(), None => TextStyle::basic_left(),
} }
} }
"Path" => { "Path" => {
let style = color_hm.get("Primitive::Path"); let style = color_hm.get("Primitive::Path");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(), None => TextStyle::basic_left(),
} }
} }
"Binary" => { "Binary" => {
let style = color_hm.get("Primitive::Binary"); let style = color_hm.get("Primitive::Binary");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(), None => TextStyle::basic_left(),
} }
} }
"BeginningOfStream" => { "BeginningOfStream" => {
let style = color_hm.get("Primitive::BeginningOfStream"); let style = color_hm.get("Primitive::BeginningOfStream");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(), None => TextStyle::basic_left(),
} }
} }
"EndOfStream" => { "EndOfStream" => {
let style = color_hm.get("Primitive::EndOfStream"); let style = color_hm.get("Primitive::EndOfStream");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(), None => TextStyle::basic_left(),
} }
} }
"Nothing" => { "Nothing" => {
let style = color_hm.get("Primitive::Nothing"); let style = color_hm.get("Primitive::Nothing");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(), None => TextStyle::basic_left(),
} }
} }
"separator_color" => { "separator_color" => {
let style = color_hm.get("separator"); let style = color_hm.get("separator");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(), None => TextStyle::basic_left(),
} }
} }
"header_align" => { "header_align" => {
let style = color_hm.get("header_align"); let style = color_hm.get("header_align");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Center, *s),
None => TextStyle::basic_right(), None => TextStyle::default_header(),
} }
} }
"header_color" => { "header_color" => {
let style = color_hm.get("header_color"); let style = color_hm.get("header_color");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Center, *s),
None => TextStyle::basic_right(), None => TextStyle::default_header(),
} }
} }
"header_bold" => { "header_bold" => {
let style = color_hm.get("header_bold"); let style = color_hm.get("header_bold");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Center, *s),
None => TextStyle::basic_right(), None => TextStyle::default_header(),
} }
} }
"header_style" => { "header_style" => {
let style = color_hm.get("header_style"); let style = color_hm.get("header_style");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Center, *s),
None => TextStyle::basic_right(), None => TextStyle::default_header(),
} }
} }
"index_color" => { "index_color" => {
let style = color_hm.get("index_color"); let style = color_hm.get("index_color");
match style { match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s), Some(s) => TextStyle::with_style(Alignment::Right, *s),
None => TextStyle::basic_right(), None => TextStyle::new()
.alignment(Alignment::Right)
.fg(Color::Green)
.bold(Some(true)),
} }
} }
_ => TextStyle::basic(), _ => TextStyle::basic_center(),
} }
} }

View file

@ -260,7 +260,7 @@ pub fn style_leaf<'a>(
let prim_type = str[0..paren_index].to_string(); let prim_type = str[0..paren_index].to_string();
style_primitive(&prim_type, &color_hash_map) style_primitive(&prim_type, &color_hash_map)
} }
_ => TextStyle::basic(), _ => TextStyle::basic_left(),
} }
} }

View file

@ -7,7 +7,7 @@ fn main() {
let width = args[1].parse::<usize>().expect("Need a width in columns"); let width = args[1].parse::<usize>().expect("Need a width in columns");
let msg = args[2..] let msg = args[2..]
.iter() .iter()
.map(|x| StyledString::new(x.to_owned(), TextStyle::basic())) .map(|x| StyledString::new(x.to_owned(), TextStyle::basic_left()))
.collect(); .collect();
let t = Table::new( let t = Table::new(

View file

@ -217,9 +217,9 @@ impl TextStyle {
} }
} }
pub fn basic() -> TextStyle { pub fn basic_center() -> TextStyle {
TextStyle::new() TextStyle::new()
.alignment(Alignment::Left) .alignment(Alignment::Center)
.style(Style::default()) .style(Style::default())
} }
@ -229,6 +229,12 @@ impl TextStyle {
.style(Style::default()) .style(Style::default())
} }
pub fn basic_left() -> TextStyle {
TextStyle::new()
.alignment(Alignment::Left)
.style(Style::default())
}
pub fn default_header() -> TextStyle { pub fn default_header() -> TextStyle {
TextStyle::new() TextStyle::new()
.alignment(Alignment::Center) .alignment(Alignment::Center)