Bump tabled to 0.17 (#14415)

With this comes a new `unicode-width` as I remember there was some issue
with `ratatui`.
 
And a bit of refactorings which are ment to reduce code lines while not
breaking anything.
Not yet complete, I think I'll try to improve some more places,
just wanted to trigger CI 😄 

And yessssssssss we have a new `unicode-width` but I sort of doubtful,
I mean the original issue with emojie.
I think it may require an additional "clean" call.
I am just saying I was not testing it with that case of complex emojies.

---------

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
This commit is contained in:
Maxim Zhiburt
2024-12-28 17:19:48 +03:00
committed by GitHub
parent 5314b31b12
commit 4401924128
24 changed files with 1419 additions and 1382 deletions

View File

@ -1,51 +1,57 @@
use crate::{
clean_charset, colorize_space_str, string_wrap, NuTableConfig, TableOutput, TableTheme,
};
use nu_color_config::{Alignment, StyleComputer, TextStyle};
use nu_protocol::{Config, FooterMode, ShellError, Span, TableMode, TrimStrategy, Value};
use terminal_size::{terminal_size, Height, Width};
use crate::{clean_charset, colorize_space_str, string_wrap, TableOutput, TableTheme};
pub type NuText = (String, TextStyle);
pub type TableResult = Result<Option<TableOutput>, ShellError>;
pub type StringResult = Result<Option<String>, ShellError>;
pub const INDEX_COLUMN_NAME: &str = "index";
pub fn create_nu_table_config(
pub fn configure_table(
out: &mut TableOutput,
config: &Config,
comp: &StyleComputer,
out: &TableOutput,
expand: bool,
mode: TableMode,
) -> NuTableConfig {
) {
let with_footer = is_footer_needed(config, out);
let theme = load_theme(mode);
out.table.set_theme(theme);
out.table
.set_structure(out.with_index, out.with_header, with_footer);
out.table.set_trim(config.table.trim.clone());
out.table
.set_border_header(config.table.header_on_separator);
out.table.set_border_color(lookup_separator_color(comp));
}
fn is_footer_needed(config: &Config, out: &TableOutput) -> bool {
let mut count_rows = out.table.count_rows();
if config.table.footer_inheritance {
count_rows = out.count_rows;
}
let with_footer = with_footer(config, out.with_header, count_rows);
NuTableConfig {
theme: load_theme(mode),
with_footer,
with_index: out.with_index,
with_header: out.with_header,
split_color: Some(lookup_separator_color(comp)),
trim: config.table.trim.clone(),
header_on_border: config.table.header_on_separator,
expand,
}
with_footer(config, out.with_header, count_rows)
}
pub fn nu_value_to_string_colored(val: &Value, cfg: &Config, style: &StyleComputer) -> String {
let (mut text, value_style) = nu_value_to_string(val, cfg, style);
if let Some(color) = value_style.color_style {
pub fn nu_value_to_string_colored(val: &Value, cfg: &Config, comp: &StyleComputer) -> String {
let (mut text, style) = nu_value_to_string(val, cfg, comp);
let is_string = matches!(val, Value::String { .. });
if is_string {
text = clean_charset(&text);
}
if let Some(color) = style.color_style {
text = color.paint(text).to_string();
}
if matches!(val, Value::String { .. }) {
text = clean_charset(&text);
colorize_space_str(&mut text, style);
if is_string {
colorize_space_str(&mut text, comp);
}
text
@ -54,9 +60,11 @@ pub fn nu_value_to_string_colored(val: &Value, cfg: &Config, style: &StyleComput
pub fn nu_value_to_string(val: &Value, cfg: &Config, style: &StyleComputer) -> NuText {
let float_precision = cfg.float_precision as usize;
let text = val.to_abbreviated_string(cfg);
make_styled_string(style, text, Some(val), float_precision)
make_styled_value(text, val, float_precision, style)
}
// todo: Expose a method which returns just style
pub fn nu_value_to_string_clean(val: &Value, cfg: &Config, style_comp: &StyleComputer) -> NuText {
let (text, style) = nu_value_to_string(val, cfg, style_comp);
let mut text = clean_charset(&text);
@ -66,7 +74,11 @@ pub fn nu_value_to_string_clean(val: &Value, cfg: &Config, style_comp: &StyleCom
}
pub fn error_sign(style_computer: &StyleComputer) -> (String, TextStyle) {
make_styled_string(style_computer, String::from(""), None, 0)
// Though holes are not the same as null, the closure for "empty" is passed a null anyway.
let text = String::from("");
let style = style_computer.compute("empty", &Value::nothing(Span::unknown()));
(text, TextStyle::with_style(Alignment::Center, style))
}
pub fn wrap_text(text: &str, width: usize, config: &Config) -> String {
@ -122,36 +134,23 @@ pub fn get_empty_style(style_computer: &StyleComputer) -> NuText {
)
}
fn make_styled_string(
style_computer: &StyleComputer,
fn make_styled_value(
text: String,
value: Option<&Value>, // None represents table holes.
value: &Value,
float_precision: usize,
style_computer: &StyleComputer,
) -> NuText {
match value {
Some(value) => {
match value {
Value::Float { .. } => {
// set dynamic precision from config
let precise_number = match convert_with_precision(&text, float_precision) {
Ok(num) => num,
Err(e) => e.to_string(),
};
(precise_number, style_computer.style_primitive(value))
}
_ => (text, style_computer.style_primitive(value)),
}
}
None => {
// Though holes are not the same as null, the closure for "empty" is passed a null anyway.
(
text,
TextStyle::with_style(
Alignment::Center,
style_computer.compute("empty", &Value::nothing(Span::unknown())),
),
)
Value::Float { .. } => {
// set dynamic precision from config
let precise_number = match convert_with_precision(&text, float_precision) {
Ok(num) => num,
Err(e) => e.to_string(),
};
(precise_number, style_computer.style_primitive(value))
}
_ => (text, style_computer.style_primitive(value)),
}
}
@ -220,3 +219,10 @@ fn need_footer(config: &Config, count_records: u64) -> bool {
}
}
}
pub fn check_value(value: &Value) -> Result<(), ShellError> {
match value {
Value::Error { error, .. } => Err(*error.clone()),
_ => Ok(()),
}
}