forked from extern/nushell
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:
parent
f9acb7a7a5
commit
57a26bbd42
@ -266,7 +266,7 @@ pub async fn autoview(context: RunnableContext) -> Result<OutputStream, ShellErr
|
||||
),
|
||||
nu_table::StyledString::new(
|
||||
format_leaf(value).plain_string(100_000),
|
||||
nu_table::TextStyle::basic(),
|
||||
nu_table::TextStyle::basic_left(),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ fn values_to_entries(
|
||||
let mut entries = vec![];
|
||||
|
||||
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() {
|
||||
|
@ -1,8 +1,7 @@
|
||||
pub use nu_data::config::NuConfig;
|
||||
use nu_data::primitive::lookup_ansi_color_style;
|
||||
use nu_protocol::{UntaggedValue, Value};
|
||||
use nu_source::Tag;
|
||||
use nu_table::TextStyle;
|
||||
use nu_protocol::Value;
|
||||
use nu_table::{Alignment, TextStyle};
|
||||
use std::fmt::Debug;
|
||||
|
||||
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 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)
|
||||
|
@ -122,11 +122,11 @@ pub fn get_color_config() -> HashMap<String, Style> {
|
||||
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<String, Style>) -> 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(),
|
||||
}
|
||||
}
|
||||
|
@ -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(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,7 @@ fn main() {
|
||||
let width = args[1].parse::<usize>().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(
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user