Add minor theme support (#2449)

* 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

* removed extra comments
This commit is contained in:
Darren Schroeder
2020-09-01 00:09:55 -05:00
committed by GitHub
parent 860c2a606d
commit adbbcafd30
12 changed files with 904 additions and 384 deletions

View File

@ -1,143 +0,0 @@
use crate::config::nuconfig::NuConfig;
use std::fmt::Debug;
#[derive(PartialEq, Debug)]
pub enum AutoPivotMode {
Auto,
Always,
Never,
}
pub trait HasTableProperties: Debug + Send {
fn pivot_mode(&self) -> AutoPivotMode;
fn header_alignment(&self) -> nu_table::Alignment;
fn header_color(&self) -> Option<ansi_term::Color>;
fn header_bold(&self) -> bool;
fn table_mode(&self) -> nu_table::Theme;
fn disabled_indexes(&self) -> bool;
fn text_color(&self) -> Option<ansi_term::Color>;
fn line_color(&self) -> Option<ansi_term::Color>;
}
pub fn pivot_mode(config: &NuConfig) -> AutoPivotMode {
let vars = config.vars.lock();
if let Some(mode) = vars.get("pivot_mode") {
let mode = match mode.as_string() {
Ok(m) if m.to_lowercase() == "auto" => AutoPivotMode::Auto,
Ok(m) if m.to_lowercase() == "always" => AutoPivotMode::Always,
Ok(m) if m.to_lowercase() == "never" => AutoPivotMode::Never,
_ => AutoPivotMode::Always,
};
return mode;
}
AutoPivotMode::Always
}
pub fn header_alignment(config: &NuConfig) -> nu_table::Alignment {
let vars = config.vars.lock();
let alignment = vars.get("header_align");
if alignment.is_none() {
return nu_table::Alignment::Center;
}
alignment.map_or(nu_table::Alignment::Left, |a| {
a.as_string().map_or(nu_table::Alignment::Center, |a| {
match a.to_lowercase().as_str() {
"center" | "c" => nu_table::Alignment::Center,
"right" | "r" => nu_table::Alignment::Right,
_ => nu_table::Alignment::Center,
}
})
})
}
pub fn get_color_for_config_key(config: &NuConfig, key: &str) -> Option<ansi_term::Color> {
let vars = config.vars.lock();
Some(match vars.get(key) {
Some(c) => match c.as_string() {
Ok(color) => match color.to_lowercase().as_str() {
"g" | "green" => ansi_term::Color::Green,
"r" | "red" => ansi_term::Color::Red,
"u" | "blue" => ansi_term::Color::Blue,
"b" | "black" => ansi_term::Color::Black,
"y" | "yellow" => ansi_term::Color::Yellow,
"p" | "purple" => ansi_term::Color::Purple,
"c" | "cyan" => ansi_term::Color::Cyan,
"w" | "white" => ansi_term::Color::White,
_ => ansi_term::Color::Green,
},
_ => ansi_term::Color::Green,
},
_ => ansi_term::Color::Green,
})
}
pub fn header_bold(config: &NuConfig) -> bool {
let vars = config.vars.lock();
vars.get("header_bold")
.map(|x| x.as_bool().unwrap_or(true))
.unwrap_or(true)
}
pub fn table_mode(config: &NuConfig) -> nu_table::Theme {
let vars = config.vars.lock();
vars.get("table_mode")
.map_or(nu_table::Theme::compact(), |mode| match mode.as_string() {
Ok(m) if m == "basic" => nu_table::Theme::basic(),
Ok(m) if m == "compact" => nu_table::Theme::compact(),
Ok(m) if m == "light" => nu_table::Theme::light(),
Ok(m) if m == "thin" => nu_table::Theme::thin(),
Ok(m) if m == "with_love" => nu_table::Theme::with_love(),
Ok(m) if m == "compact_double" => nu_table::Theme::compact_double(),
_ => nu_table::Theme::compact(),
})
}
pub fn disabled_indexes(config: &NuConfig) -> bool {
let vars = config.vars.lock();
vars.get("disable_table_indexes")
.map_or(false, |x| x.as_bool().unwrap_or(false))
}
impl HasTableProperties for NuConfig {
fn pivot_mode(&self) -> AutoPivotMode {
pivot_mode(self)
}
fn header_alignment(&self) -> nu_table::Alignment {
header_alignment(self)
}
fn header_color(&self) -> Option<ansi_term::Color> {
get_color_for_config_key(self, "header_color")
}
fn text_color(&self) -> Option<ansi_term::Color> {
get_color_for_config_key(self, "text_color")
}
fn line_color(&self) -> Option<ansi_term::Color> {
get_color_for_config_key(self, "line_color")
}
fn header_bold(&self) -> bool {
header_bold(self)
}
fn table_mode(&self) -> nu_table::Theme {
table_mode(self)
}
fn disabled_indexes(&self) -> bool {
disabled_indexes(self)
}
}

View File

@ -1,5 +1,8 @@
use nu_protocol::{hir::Number, Primitive};
use nu_table::TextStyle;
use ansi_term::{Color, Style};
use nu_protocol::{hir::Number, Primitive, Value};
use nu_source::Tag;
use nu_table::{Alignment, TextStyle};
use std::collections::HashMap;
pub fn number(number: impl Into<Number>) -> Primitive {
let number = number.into();
@ -10,10 +13,349 @@ pub fn number(number: impl Into<Number>) -> Primitive {
}
}
pub fn style_primitive(primitive: &Primitive) -> TextStyle {
pub fn lookup_ansi_color_style(s: String) -> Style {
match s.as_str() {
"g" | "green" => Color::Green.normal(),
"gb" | "green_bold" => Color::Green.bold(),
"gu" | "green_underline" => Color::Green.underline(),
"gi" | "green_italic" => Color::Green.italic(),
"gd" | "green_dimmed" => Color::Green.dimmed(),
"gr" | "green_reverse" => Color::Green.reverse(),
"r" | "red" => Color::Red.normal(),
"rb" | "red_bold" => Color::Red.bold(),
"ru" | "red_underline" => Color::Red.underline(),
"ri" | "red_italic" => Color::Red.italic(),
"rd" | "red_dimmed" => Color::Red.dimmed(),
"rr" | "red_reverse" => Color::Red.reverse(),
"u" | "blue" => Color::Blue.normal(),
"ub" | "blue_bold" => Color::Blue.bold(),
"uu" | "blue_underline" => Color::Blue.underline(),
"ui" | "blue_italic" => Color::Blue.italic(),
"ud" | "blue_dimmed" => Color::Blue.dimmed(),
"ur" | "blue_reverse" => Color::Blue.reverse(),
"b" | "black" => Color::Black.normal(),
"bb" | "black_bold" => Color::Black.bold(),
"bu" | "black_underline" => Color::Black.underline(),
"bi" | "black_italic" => Color::Black.italic(),
"bd" | "black_dimmed" => Color::Black.dimmed(),
"br" | "black_reverse" => Color::Black.reverse(),
"y" | "yellow" => Color::Yellow.normal(),
"yb" | "yellow_bold" => Color::Yellow.bold(),
"yu" | "yellow_underline" => Color::Yellow.underline(),
"yi" | "yellow_italic" => Color::Yellow.italic(),
"yd" | "yellow_dimmed" => Color::Yellow.dimmed(),
"yr" | "yellow_reverse" => Color::Yellow.reverse(),
"p" | "purple" => Color::Purple.normal(),
"pb" | "purple_bold" => Color::Purple.bold(),
"pu" | "purple_underline" => Color::Purple.underline(),
"pi" | "purple_italic" => Color::Purple.italic(),
"pd" | "purple_dimmed" => Color::Purple.dimmed(),
"pr" | "purple_reverse" => Color::Purple.reverse(),
"c" | "cyan" => Color::Cyan.normal(),
"cb" | "cyan_bold" => Color::Cyan.bold(),
"cu" | "cyan_underline" => Color::Cyan.underline(),
"ci" | "cyan_italic" => Color::Cyan.italic(),
"cd" | "cyan_dimmed" => Color::Cyan.dimmed(),
"cr" | "cyan_reverse" => Color::Cyan.reverse(),
"w" | "white" => Color::White.normal(),
"wb" | "white_bold" => Color::White.bold(),
"wu" | "white_underline" => Color::White.underline(),
"wi" | "white_italic" => Color::White.italic(),
"wd" | "white_dimmed" => Color::White.dimmed(),
"wr" | "white_reverse" => Color::White.reverse(),
_ => Color::White.normal(),
}
}
pub fn string_to_lookup_value(str_prim: &str) -> String {
match str_prim {
"primitive_int" => "Primitive::Int".to_string(),
"primitive_decimal" => "Primitive::Decimal".to_string(),
"primitive_filesize" => "Primitive::Filesize".to_string(),
"primitive_string" => "Primitive::String".to_string(),
"primitive_line" => "Primitive::Line".to_string(),
"primitive_columnpath" => "Primitive::ColumnPath".to_string(),
"primitive_pattern" => "Primitive::Pattern".to_string(),
"primitive_boolean" => "Primitive::Boolean".to_string(),
"primitive_date" => "Primitive::Date".to_string(),
"primitive_duration" => "Primitive::Duration".to_string(),
"primitive_range" => "Primitive::Range".to_string(),
"primitive_path" => "Primitive::Path".to_string(),
"primitive_binary" => "Primitive::Binary".to_string(),
"separator_color" => "separator_color".to_string(),
"header_align" => "header_align".to_string(),
"header_color" => "header_color".to_string(),
"header_bold" => "header_bold".to_string(),
"header_style" => "header_style".to_string(),
"index_color" => "index_color".to_string(),
_ => "Primitive::Nothing".to_string(),
}
}
fn update_hashmap(key: &str, val: &Value, hm: &mut HashMap<String, Style>) {
if let Ok(var) = val.as_string() {
let color = lookup_ansi_color_style(var);
let prim = string_to_lookup_value(&key);
if let Some(v) = hm.get_mut(&prim) {
*v = color;
} else {
hm.insert(prim, color);
}
}
}
pub fn get_color_config() -> HashMap<String, Style> {
// create the hashmap
let mut hm: HashMap<String, Style> = HashMap::new();
// set some defaults
hm.insert("primitive_int".to_string(), Color::White.normal());
hm.insert("primitive_decimal".to_string(), Color::White.normal());
hm.insert("primitive_filesize".to_string(), Color::White.normal());
hm.insert("primitive_string".to_string(), Color::White.normal());
hm.insert("primitive_line".to_string(), Color::White.normal());
hm.insert("primitive_columnpath".to_string(), Color::White.normal());
hm.insert("primitive_pattern".to_string(), Color::White.normal());
hm.insert("primitive_boolean".to_string(), Color::White.normal());
hm.insert("primitive_date".to_string(), Color::White.normal());
hm.insert("primitive_duration".to_string(), Color::White.normal());
hm.insert("primitive_range".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("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_style".to_string(), Style::default());
hm.insert("index_color".to_string(), Color::Green.normal());
// 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);
}
_ => (),
}
}
}
}
hm
}
// This function will assign a text style to a primitive, or really any string that's
// in the hashmap. The hashmap actually contains the style to be applied.
pub fn style_primitive(primitive: &str, color_hm: &HashMap<String, Style>) -> TextStyle {
match primitive {
Primitive::Int(_) | Primitive::Filesize(_) | Primitive::Decimal(_) => {
TextStyle::basic_right()
"Int" => {
let style = color_hm.get("Primitive::Int");
match style {
Some(s) => TextStyle::with_style(Alignment::Right, *s),
None => TextStyle::basic_right(),
}
}
"Decimal" => {
let style = color_hm.get("Primitive::Decimal");
match style {
Some(s) => TextStyle::with_style(Alignment::Right, *s),
None => TextStyle::basic_right(),
}
}
"Filesize" => {
let style = color_hm.get("Primitive::Filesize");
match style {
Some(s) => TextStyle::with_style(Alignment::Right, *s),
None => TextStyle::basic_right(),
}
}
"String" => {
let style = color_hm.get("Primitive::String");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"Line" => {
let style = color_hm.get("Primitive::Line");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"ColumnPath" => {
let style = color_hm.get("Primitive::ColumnPath");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"Pattern" => {
let style = color_hm.get("Primitive::Pattern");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"Boolean" => {
let style = color_hm.get("Primitive::Boolean");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"Date" => {
let style = color_hm.get("Primitive::Date");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"Duration" => {
let style = color_hm.get("Primitive::Duration");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"Range" => {
let style = color_hm.get("Primitive::Range");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"Path" => {
let style = color_hm.get("Primitive::Path");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"Binary" => {
let style = color_hm.get("Primitive::Binary");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"BeginningOfStream" => {
let style = color_hm.get("Primitive::BeginningOfStream");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"EndOfStream" => {
let style = color_hm.get("Primitive::EndOfStream");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"Nothing" => {
let style = color_hm.get("Primitive::Nothing");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"separator_color" => {
let style = color_hm.get("separator");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"header_align" => {
let style = color_hm.get("header_align");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"header_color" => {
let style = color_hm.get("header_color");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"header_bold" => {
let style = color_hm.get("header_bold");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"header_style" => {
let style = color_hm.get("header_style");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
"index_color" => {
let style = color_hm.get("index_color");
match style {
Some(s) => TextStyle::with_style(Alignment::Left, *s),
None => TextStyle::basic_right(),
}
}
_ => TextStyle::basic(),
}

View File

@ -9,6 +9,7 @@ use nu_protocol::{Primitive, Type, UntaggedValue};
use nu_source::{DebugDocBuilder, PrettyDebug, Span, Tagged};
use nu_table::TextStyle;
use num_traits::Zero;
use std::collections::HashMap;
pub struct Date;
@ -245,9 +246,20 @@ pub fn format_leaf<'a>(value: impl Into<&'a UntaggedValue>) -> DebugDocBuilder {
InlineShape::from_value(value.into()).format().pretty()
}
pub fn style_leaf<'a>(value: impl Into<&'a UntaggedValue>) -> TextStyle {
pub fn style_leaf<'a>(
value: impl Into<&'a UntaggedValue>,
color_hash_map: &HashMap<String, ansi_term::Style>,
) -> TextStyle {
match value.into() {
UntaggedValue::Primitive(p) => style_primitive(p),
UntaggedValue::Primitive(p) => {
// This is just to return the name of the type so that style_primitive
// can work on a string versus a type like String("some_text")
let str: &str = &p.to_string();
let str_len = str.len();
let paren_index = str.find('(').unwrap_or(str_len - 1);
let prim_type = str[0..paren_index].to_string();
style_primitive(&prim_type, &color_hash_map)
}
_ => TextStyle::basic(),
}
}