Use IntoValue in config code (#13751)

# Description

Cleans up and refactors the config code using the `IntoValue` macro.
Shoutout to @cptpiepmatz for making the macro!

# User-Facing Changes

Should be none.

# After Submitting

Somehow refactor the reverse transformation.
This commit is contained in:
Ian Manske
2024-09-05 00:44:23 -07:00
committed by GitHub
parent 4792328d0e
commit abd230e12e
37 changed files with 992 additions and 1206 deletions

View File

@ -23,8 +23,8 @@ pub fn create_nu_table_config(
with_index: out.with_index,
with_header: out.with_header,
split_color: Some(lookup_separator_color(comp)),
trim: config.trim_strategy.clone(),
header_on_border: config.table_move_header,
trim: config.table.trim.clone(),
header_on_border: config.table.header_on_separator,
expand,
}
}
@ -62,7 +62,8 @@ pub fn error_sign(style_computer: &StyleComputer) -> (String, TextStyle) {
}
pub fn wrap_text(text: &str, width: usize, config: &Config) -> String {
string_wrap(text, width, is_cfg_trim_keep_words(config))
let keep_words = config.table.trim == TrimStrategy::wrap(true);
string_wrap(text, width, keep_words)
}
pub fn get_header_style(style_computer: &StyleComputer) -> TextStyle {
@ -163,15 +164,6 @@ fn convert_with_precision(val: &str, precision: usize) -> Result<String, ShellEr
Ok(format!("{val_float:.precision$}"))
}
fn is_cfg_trim_keep_words(config: &Config) -> bool {
matches!(
config.trim_strategy,
TrimStrategy::Wrap {
try_to_keep_words: true
}
)
}
pub fn load_theme(mode: TableMode) -> TableTheme {
match mode {
TableMode::Basic => TableTheme::basic(),

View File

@ -36,7 +36,7 @@ fn collapsed_table(
return Ok(None);
}
let indent = (config.table_indent.left, config.table_indent.right);
let indent = (config.table.padding.left, config.table.padding.right);
let table = table.draw(style_computer, &theme, indent);
Ok(Some(table))

View File

@ -26,8 +26,8 @@ impl JustTable {
fn create_table(input: &[Value], opts: TableOpts<'_>) -> Result<Option<String>, ShellError> {
match table(input, &opts)? {
Some(mut out) => {
let left = opts.config.table_indent.left;
let right = opts.config.table_indent.right;
let left = opts.config.table.padding.left;
let right = opts.config.table.padding.right;
out.table.set_indent(left, right);
colorize_space(out.table.get_records_mut(), opts.style_computer);
@ -59,8 +59,8 @@ fn kv_table(record: &Record, opts: TableOpts<'_>) -> StringResult {
let mut out = TableOutput::new(table, false, true);
let left = opts.config.table_indent.left;
let right = opts.config.table_indent.right;
let left = opts.config.table.padding.left;
let right = opts.config.table.padding.right;
out.table.set_indent(left, right);
let table_config =

View File

@ -67,7 +67,7 @@ impl<'a> TableOpts<'a> {
}
fn has_index(opts: &TableOpts<'_>, headers: &[String]) -> bool {
let with_index = match opts.config.table_index_mode {
let with_index = match opts.config.table.index_mode {
TableIndexMode::Always => true,
TableIndexMode::Never => false,
TableIndexMode::Auto => headers.iter().any(|header| header == INDEX_COLUMN_NAME),