color_config now accepts closures as color values (#7141)
# Description
Closes #6909. You can now add closures to your `color_config` themes.
Whenever a value would be printed with `table`, the closure is run with
the value piped-in. The closure must return either a {fg,bg,attr} record
or a color name (`'light_red'` etc.). This returned style is used to
colour the value.
This is entirely backwards-compatible with existing config.nu files.
Example code excerpt:
```
let my_theme = {
header: green_bold
bool: { if $in { 'light_cyan' } else { 'light_red' } }
int: purple_bold
filesize: { |e| if $e == 0b { 'gray' } else if $e < 1mb { 'purple_bold' } else { 'cyan_bold' } }
duration: purple_bold
date: { (date now) - $in | if $in > 1wk { 'cyan_bold' } else if $in > 1day { 'green_bold' } else { 'yellow_bold' } }
range: yellow_bold
string: { if $in =~ '^#\w{6}$' { $in } else { 'white' } }
nothing: white
```
Example output with this in effect:
![2022-11-16 12 47 23 AM - style_computer
rs_-_nushell_-_VSCodium](https://user-images.githubusercontent.com/83939/201952558-482de05d-69c7-4bf2-91fc-d0964bf71264.png)
![2022-11-16 12 39 41 AM - style_computer
rs_-_nushell_-_VSCodium](https://user-images.githubusercontent.com/83939/201952580-2384bb86-b680-40fe-8192-71bae396c738.png)
![2022-11-15 09 21 54 PM - run_external
rs_-_nushell_-_VSCodium](https://user-images.githubusercontent.com/83939/201952601-343fc15d-e4a8-4a92-ad89-9a7d17d42748.png)
Slightly important notes:
* Some color_config names, namely "separator", "empty" and "hints", pipe
in `null` instead of a value.
* Currently, doing anything non-trivial inside a closure has an
understandably big perf hit. I currently do not actually recommend
something like `string: { if $in =~ '^#\w{6}$' { $in } else { 'white' }
}` for serious work, mainly because of the abundance of string-type data
in the world. Nevertheless, lesser-used types like "date" and "duration"
work well with this.
* I had to do some reorganisation in order to make it possible to call
`eval_block()` that late in table rendering. I invented a new struct
called "StyleComputer" which holds the engine_state and stack of the
initial `table` command (implicit or explicit).
* StyleComputer has a `compute()` method which takes a color_config name
and a nu value, and always returns the correct Style, so you don't have
to worry about A) the color_config value was set at all, B) whether it
was set to a closure or not, or C) which default style to use in those
cases.
* Currently, errors encountered during execution of the closures are
thrown in the garbage. Any other ideas are welcome. (Nonetheless, errors
result in a huge perf hit when they are encountered. I think what should
be done is to assume something terrible happened to the user's config
and invalidate the StyleComputer for that `table` run, thus causing
subsequent output to just be Style::default().)
* More thorough tests are forthcoming - ran into some difficulty using
`nu!` to take an alternative config, and for some reason `let-env config
=` statements don't seem to work inside `nu!` pipelines(???)
* The default config.nu has not been updated to make use of this yet. Do
tell if you think I should incorporate that into this.
# User-Facing Changes
See above.
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace --features=extra -- -D warnings -D
clippy::unwrap_used -A clippy::needless_collect` to check that you're
using the standard code style
- `cargo test --workspace --features=extra` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2022-12-17 14:07:56 +01:00
|
|
|
use nu_color_config::TextStyle;
|
|
|
|
use nu_table::{Table, TableConfig, TableTheme};
|
2022-10-03 18:40:16 +02:00
|
|
|
use tabled::papergrid::records::{cell_info::CellInfo, tcell::TCell};
|
2020-06-20 05:41:53 +02:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args: Vec<_> = std::env::args().collect();
|
2021-02-19 09:40:53 +01:00
|
|
|
let mut width = 0;
|
|
|
|
|
|
|
|
if args.len() > 1 {
|
|
|
|
// Width in terminal characters
|
|
|
|
width = args[1].parse::<usize>().expect("Need a width in columns");
|
|
|
|
}
|
|
|
|
|
|
|
|
if width < 4 {
|
|
|
|
println!("Width must be greater than or equal to 4, setting width to 80");
|
|
|
|
width = 80;
|
|
|
|
}
|
2020-06-20 05:41:53 +02:00
|
|
|
|
2021-02-16 16:47:47 +01:00
|
|
|
// The mocked up table data
|
|
|
|
let (table_headers, row_data) = make_table_data();
|
2022-12-15 15:47:04 +01:00
|
|
|
|
2021-02-16 16:47:47 +01:00
|
|
|
// The table headers
|
|
|
|
let headers = vec_of_str_to_vec_of_styledstr(&table_headers, true);
|
2022-12-15 15:47:04 +01:00
|
|
|
|
2021-02-16 16:47:47 +01:00
|
|
|
// The table rows
|
|
|
|
let rows = vec_of_str_to_vec_of_styledstr(&row_data, false);
|
2022-12-15 15:47:04 +01:00
|
|
|
|
2021-02-16 16:47:47 +01:00
|
|
|
// The table itself
|
2022-10-03 18:40:16 +02:00
|
|
|
let count_cols = std::cmp::max(rows.len(), headers.len());
|
|
|
|
let mut rows = vec![rows; 3];
|
|
|
|
rows.insert(0, headers);
|
2022-12-15 15:47:04 +01:00
|
|
|
|
|
|
|
let theme = TableTheme::rounded();
|
|
|
|
let table_cfg = TableConfig::new(theme, true, false, false);
|
|
|
|
|
|
|
|
let table = Table::new(rows, (3, count_cols));
|
|
|
|
|
2021-09-10 04:27:12 +02:00
|
|
|
// Capture the table as a string
|
2022-07-14 22:24:32 +02:00
|
|
|
let output_table = table
|
2022-12-15 15:47:04 +01:00
|
|
|
.draw(table_cfg, width)
|
2023-01-30 02:37:54 +01:00
|
|
|
.unwrap_or_else(|| format!("Couldn't fit table into {width} columns!"));
|
2022-12-15 15:47:04 +01:00
|
|
|
|
2021-09-10 04:27:12 +02:00
|
|
|
// Draw the table
|
2023-01-30 02:37:54 +01:00
|
|
|
println!("{output_table}")
|
2021-02-16 16:47:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_table_data() -> (Vec<&'static str>, Vec<&'static str>) {
|
2021-03-26 09:26:57 +01:00
|
|
|
let table_headers = vec![
|
|
|
|
"category",
|
|
|
|
"description",
|
|
|
|
"emoji",
|
|
|
|
"ios_version",
|
|
|
|
"unicode_version",
|
|
|
|
"aliases",
|
|
|
|
"tags",
|
|
|
|
"category2",
|
|
|
|
"description2",
|
|
|
|
"emoji2",
|
|
|
|
"ios_version2",
|
|
|
|
"unicode_version2",
|
|
|
|
"aliases2",
|
|
|
|
"tags2",
|
|
|
|
];
|
2021-02-16 16:47:47 +01:00
|
|
|
|
2021-03-26 09:26:57 +01:00
|
|
|
let row_data = vec![
|
|
|
|
"Smileys & Emotion",
|
|
|
|
"grinning face",
|
|
|
|
"😀",
|
|
|
|
"6",
|
|
|
|
"6.1",
|
|
|
|
"grinning",
|
|
|
|
"smile",
|
|
|
|
"Smileys & Emotion",
|
|
|
|
"grinning face",
|
|
|
|
"😀",
|
|
|
|
"6",
|
|
|
|
"6.1",
|
|
|
|
"grinning",
|
|
|
|
"smile",
|
|
|
|
];
|
2021-02-16 16:47:47 +01:00
|
|
|
|
|
|
|
(table_headers, row_data)
|
|
|
|
}
|
|
|
|
|
2022-10-03 18:40:16 +02:00
|
|
|
fn vec_of_str_to_vec_of_styledstr(
|
|
|
|
data: &[&str],
|
|
|
|
is_header: bool,
|
|
|
|
) -> Vec<TCell<CellInfo<'static>, TextStyle>> {
|
2021-02-16 16:47:47 +01:00
|
|
|
let mut v = vec![];
|
|
|
|
|
2021-09-10 00:44:22 +02:00
|
|
|
for x in data {
|
2021-02-16 16:47:47 +01:00
|
|
|
if is_header {
|
2022-10-03 18:40:16 +02:00
|
|
|
v.push(Table::create_cell(
|
2021-02-16 16:47:47 +01:00
|
|
|
String::from(*x),
|
|
|
|
TextStyle::default_header(),
|
|
|
|
))
|
|
|
|
} else {
|
2022-10-03 18:40:16 +02:00
|
|
|
v.push(Table::create_cell(
|
|
|
|
String::from(*x),
|
|
|
|
TextStyle::basic_left(),
|
|
|
|
))
|
2021-02-16 16:47:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
v
|
2020-06-20 05:41:53 +02:00
|
|
|
}
|