diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index e128ba637d..54c6f20a8d 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -18,8 +18,8 @@ use nu_protocol::{ ByteStream, Config, DataSource, ListStream, PipelineMetadata, Signals, TableMode, ValueIterator, }; use nu_table::{ - common::create_nu_table_config, CollapsedTable, ExpandedTable, JustTable, NuRecordsValue, - NuTable, StringResult, TableOpts, TableOutput, + common::configure_table, CollapsedTable, ExpandedTable, JustTable, NuRecordsValue, NuTable, + StringResult, TableOpts, TableOutput, }; use nu_utils::{get_ls_colors, terminal_size}; @@ -1070,13 +1070,13 @@ fn create_empty_placeholder( let data = vec![vec![cell]]; let mut table = NuTable::from(data); table.set_data_style(TextStyle::default().dimmed()); - let out = TableOutput::from_table(table, false, false); + let mut out = TableOutput::from_table(table, false, false); let style_computer = &StyleComputer::from_config(engine_state, stack); - let config = create_nu_table_config(&config, style_computer, &out, false, TableMode::default()); + configure_table(&mut out, &config, style_computer, TableMode::default()); out.table - .draw(config, termwidth) + .draw(termwidth) .expect("Could not create empty table placeholder") } diff --git a/crates/nu-table/examples/table_demo.rs b/crates/nu-table/examples/table_demo.rs index 402936e8c0..581d8934ea 100644 --- a/crates/nu-table/examples/table_demo.rs +++ b/crates/nu-table/examples/table_demo.rs @@ -1,6 +1,6 @@ use nu_ansi_term::{Color, Style}; use nu_color_config::TextStyle; -use nu_table::{NuTable, NuTableConfig, TableTheme}; +use nu_table::{NuTable, TableTheme}; use tabled::grid::records::vec_records::Text; fn main() { @@ -28,15 +28,11 @@ fn main() { table.set_data_style(TextStyle::basic_left()); table.set_header_style(TextStyle::basic_center().style(Style::new().on(Color::Blue))); - - let table_cfg = NuTableConfig { - theme: TableTheme::rounded(), - with_header: true, - ..Default::default() - }; + table.set_theme(TableTheme::rounded()); + table.set_structure(false, true, false); let output_table = table - .draw(table_cfg, width) + .draw(width) .unwrap_or_else(|| format!("Couldn't fit table into {width} columns!")); println!("{output_table}") diff --git a/crates/nu-table/src/common.rs b/crates/nu-table/src/common.rs index e5733b98ba..8c89fbbfe2 100644 --- a/crates/nu-table/src/common.rs +++ b/crates/nu-table/src/common.rs @@ -3,9 +3,7 @@ use nu_protocol::{Config, FooterMode, ShellError, Span, TableMode, TrimStrategy, use terminal_size::{terminal_size, Height, Width}; -use crate::{ - clean_charset, colorize_space_str, string_wrap, NuTableConfig, TableOutput, TableTheme, -}; +use crate::{clean_charset, colorize_space_str, string_wrap, TableOutput, TableTheme}; pub type NuText = (String, TextStyle); pub type TableResult = Result, ShellError>; @@ -13,30 +11,31 @@ pub type StringResult = Result, 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, comp: &StyleComputer) -> String { diff --git a/crates/nu-table/src/lib.rs b/crates/nu-table/src/lib.rs index f4d7d7c6eb..eb4934d87e 100644 --- a/crates/nu-table/src/lib.rs +++ b/crates/nu-table/src/lib.rs @@ -10,7 +10,7 @@ pub mod common; pub use common::{StringResult, TableResult}; pub use nu_color_config::TextStyle; -pub use table::{NuRecordsValue, NuTable, NuTableConfig}; +pub use table::{NuRecordsValue, NuTable}; pub use table_theme::TableTheme; pub use types::{CollapsedTable, ExpandedTable, JustTable, TableOpts, TableOutput}; pub use unstructured_table::UnstructuredTable; diff --git a/crates/nu-table/src/table.rs b/crates/nu-table/src/table.rs index 32687651aa..a6882e2cba 100644 --- a/crates/nu-table/src/table.rs +++ b/crates/nu-table/src/table.rs @@ -2,7 +2,7 @@ use std::{cmp::min, collections::HashMap}; use nu_ansi_term::Style; use nu_color_config::TextStyle; -use nu_protocol::TrimStrategy; +use nu_protocol::{TableIndent, TrimStrategy}; use nu_utils::strip_ansi_unlikely; use tabled::{ @@ -40,29 +40,15 @@ pub struct NuTable { data: NuRecords, styles: Styles, alignments: Alignments, - indent: (usize, usize), + config: TableConfig, } -#[derive(Debug, Default, Clone)] -struct TableConfig { - data: Value, - index: Value, - header: Value, - columns: HashMap, - cells: HashMap, -} - -type Alignments = TableConfig; - -type Styles = TableConfig; - impl NuTable { /// Creates an empty [`NuTable`] instance. pub fn new(count_rows: usize, count_columns: usize) -> Self { Self { data: VecRecords::new(vec![vec![Text::default(); count_columns]; count_rows]), styles: Styles::default(), - indent: (1, 1), alignments: Alignments { data: AlignmentHorizontal::Left, index: AlignmentHorizontal::Right, @@ -70,6 +56,15 @@ impl NuTable { columns: HashMap::default(), cells: HashMap::default(), }, + config: TableConfig { + theme: TableTheme::basic(), + trim: TrimStrategy::truncate(None), + structure: TableStructure::new(false, false, false), + indent: TableIndent::new(1, 1), + header_on_border: false, + expand: false, + border_color: None, + }, } } @@ -151,8 +146,32 @@ impl NuTable { self.alignments.data = convert_alignment(style.alignment); } - pub fn set_indent(&mut self, left: usize, right: usize) { - self.indent = (left, right); + pub fn set_indent(&mut self, indent: TableIndent) { + self.config.indent = indent; + } + + pub fn set_theme(&mut self, theme: TableTheme) { + self.config.theme = theme; + } + + pub fn set_structure(&mut self, index: bool, header: bool, footer: bool) { + self.config.structure = TableStructure::new(index, header, footer); + } + + pub fn set_border_header(&mut self, on: bool) { + self.config.header_on_border = on; + } + + pub fn set_trim(&mut self, strategy: TrimStrategy) { + self.config.trim = strategy; + } + + pub fn set_strategy(&mut self, expand: bool) { + self.config.expand = expand; + } + + pub fn set_border_color(&mut self, color: Style) { + self.config.border_color = (!color.is_plain()).then_some(color); } pub fn get_records_mut(&mut self) -> &mut NuRecords { @@ -162,21 +181,15 @@ impl NuTable { /// Converts a table to a String. /// /// It returns None in case where table cannot be fit to a terminal width. - pub fn draw(self, config: NuTableConfig, termwidth: usize) -> Option { - build_table( - self.data, - config, - self.alignments, - self.styles, - termwidth, - self.indent, - ) + pub fn draw(self, termwidth: usize) -> Option { + build_table(self, termwidth) } /// Return a total table width. - pub fn total_width(&self, config: &NuTableConfig) -> usize { - let config = get_config(&config.theme, false, None); - let widths = build_width(&self.data, self.indent.0 + self.indent.1); + pub fn total_width(&self) -> usize { + let config = create_config(&self.config.theme, false, None); + let pad = indent_sum(self.config.indent); + let widths = build_width(&self.data, pad); get_total_width2(&widths, &config) } } @@ -190,33 +203,31 @@ impl From>>> for NuTable { } } +type Alignments = CellConfiguration; + +type Styles = CellConfiguration; + +#[derive(Debug, Default, Clone)] +struct CellConfiguration { + data: Value, + index: Value, + header: Value, + columns: HashMap, + cells: HashMap, +} + #[derive(Debug, Clone)] -pub struct NuTableConfig { - pub theme: TableTheme, - pub trim: TrimStrategy, - pub split_color: Option