From d1e7884d1915772a5c0280bcb77f2beaf5f17e36 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Mon, 16 May 2022 10:35:57 -0500 Subject: [PATCH] table refactor for readability (#5555) --- crates/nu-command/src/viewers/table.rs | 31 +- crates/nu-table/src/lib.rs | 6 +- crates/nu-table/src/main.rs | 4 +- crates/nu-table/src/table.rs | 600 +------------------------ crates/nu-table/src/table_theme.rs | 329 ++++++++++++++ crates/nu-table/src/textstyle.rs | 256 +++++++++++ crates/nu-table/src/wrap.rs | 2 +- 7 files changed, 619 insertions(+), 609 deletions(-) create mode 100644 crates/nu-table/src/table_theme.rs create mode 100644 crates/nu-table/src/textstyle.rs diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index 96a7da128..f146d0e31 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -1,14 +1,13 @@ use lscolors::{LsColors, Style}; use nu_color_config::{get_color_config, style_primitive}; -use nu_engine::column::get_columns; -use nu_engine::{env_to_string, CallExt}; -use nu_protocol::ast::{Call, PathMember}; -use nu_protocol::engine::{Command, EngineState, Stack, StateWorkingSet}; +use nu_engine::{column::get_columns, env_to_string, CallExt}; use nu_protocol::{ + ast::{Call, PathMember}, + engine::{Command, EngineState, Stack, StateWorkingSet}, format_error, Category, Config, DataSource, Example, IntoPipelineData, ListStream, PipelineData, PipelineMetadata, RawStream, ShellError, Signature, Span, SyntaxShape, Value, }; -use nu_table::{StyledString, TextStyle, Theme}; +use nu_table::{StyledString, TableTheme, TextStyle}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::time::Instant; @@ -536,17 +535,17 @@ impl Iterator for PagingTableCreator { } } -fn load_theme_from_config(config: &Config) -> Theme { +fn load_theme_from_config(config: &Config) -> TableTheme { match config.table_mode.as_str() { - "basic" => nu_table::Theme::basic(), - "compact" => nu_table::Theme::compact(), - "compact_double" => nu_table::Theme::compact_double(), - "light" => nu_table::Theme::light(), - "with_love" => nu_table::Theme::with_love(), - "rounded" => nu_table::Theme::rounded(), - "reinforced" => nu_table::Theme::reinforced(), - "heavy" => nu_table::Theme::heavy(), - "none" => nu_table::Theme::none(), - _ => nu_table::Theme::rounded(), + "basic" => nu_table::TableTheme::basic(), + "compact" => nu_table::TableTheme::compact(), + "compact_double" => nu_table::TableTheme::compact_double(), + "light" => nu_table::TableTheme::light(), + "with_love" => nu_table::TableTheme::with_love(), + "rounded" => nu_table::TableTheme::rounded(), + "reinforced" => nu_table::TableTheme::reinforced(), + "heavy" => nu_table::TableTheme::heavy(), + "none" => nu_table::TableTheme::none(), + _ => nu_table::TableTheme::rounded(), } } diff --git a/crates/nu-table/src/lib.rs b/crates/nu-table/src/lib.rs index 661d7ddde..4a98a1c3f 100644 --- a/crates/nu-table/src/lib.rs +++ b/crates/nu-table/src/lib.rs @@ -1,5 +1,9 @@ mod table; +mod table_theme; +mod textstyle; mod wrap; -pub use table::{draw_table, StyledString, Table, TextStyle, Theme}; +pub use table::{draw_table, Table}; +pub use table_theme::TableTheme; +pub use textstyle::{StyledString, TextStyle}; pub use wrap::Alignment; diff --git a/crates/nu-table/src/main.rs b/crates/nu-table/src/main.rs index 75401b888..3fe9bdb3b 100644 --- a/crates/nu-table/src/main.rs +++ b/crates/nu-table/src/main.rs @@ -1,5 +1,5 @@ use nu_protocol::Config; -use nu_table::{draw_table, StyledString, Table, TextStyle, Theme}; +use nu_table::{draw_table, StyledString, Table, TableTheme, TextStyle}; use std::collections::HashMap; fn main() { @@ -23,7 +23,7 @@ fn main() { // The table rows let rows = vec_of_str_to_vec_of_styledstr(&row_data, false); // The table itself - let table = Table::new(headers, vec![rows; 3], Theme::rounded()); + let table = Table::new(headers, vec![rows; 3], TableTheme::rounded()); // FIXME: Config isn't available from here so just put these here to compile let color_hm: HashMap = HashMap::new(); // get the default config diff --git a/crates/nu-table/src/table.rs b/crates/nu-table/src/table.rs index 2c61cf899..cd5f3fb5e 100644 --- a/crates/nu-table/src/table.rs +++ b/crates/nu-table/src/table.rs @@ -1,5 +1,7 @@ +use crate::table_theme::TableTheme; use crate::wrap::{column_width, split_sublines, wrap, Alignment, Subline, WrappedCell}; -use nu_ansi_term::{Color, Style}; +use crate::{StyledString, TextStyle}; +use nu_ansi_term::Style; use nu_protocol::{Config, FooterMode}; use std::collections::HashMap; use std::fmt::Write; @@ -14,595 +16,15 @@ enum SeparatorPosition { pub struct Table { pub headers: Vec, pub data: Vec>, - pub theme: Theme, -} - -#[derive(Debug, Clone)] -pub struct StyledString { - pub contents: String, - pub style: TextStyle, -} - -impl StyledString { - pub fn new(contents: String, style: TextStyle) -> StyledString { - StyledString { contents, style } - } - - pub fn set_style(&mut self, style: TextStyle) { - self.style = style; - } -} - -#[derive(Debug, Clone, Copy)] -pub struct TextStyle { - pub alignment: Alignment, - pub color_style: Option