From 41669e60c807b0fe65146fed97f4c3c3db609f6a Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Mon, 18 Jul 2022 19:45:21 +0300 Subject: [PATCH] nu-table: Fix header style (again 2x) (#6073) * nu-table: Fix header style It did appeared again after my small change... Signed-off-by: Maxim Zhiburt * nu-table: Add a empty header style test Signed-off-by: Maxim Zhiburt --- Cargo.lock | 6 +++--- crates/nu-table/Cargo.toml | 2 +- crates/nu-table/src/table.rs | 11 +++++----- crates/nu-table/src/table_theme.rs | 8 ++++--- crates/nu-table/tests/style.rs | 34 ++++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 crates/nu-table/tests/style.rs diff --git a/Cargo.lock b/Cargo.lock index 3d868edf54..6b621b8147 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3171,7 +3171,7 @@ checksum = "decf7381921fea4dcb2549c5667eda59b3ec297ab7e2b5fc33eac69d2e7da87b" [[package]] name = "papergrid" version = "0.4.0" -source = "git+https://github.com/zhiburt/tabled?rev=0ac6b7b7b89b56b23ba479d1b1cfe1976b71fd62#0ac6b7b7b89b56b23ba479d1b1cfe1976b71fd62" +source = "git+https://github.com/zhiburt/tabled?rev=5ac9aa53196cd5bbf570daa5357a76aa238722c1#5ac9aa53196cd5bbf570daa5357a76aa238722c1" dependencies = [ "ansi-str 0.1.1", "bytecount", @@ -4789,7 +4789,7 @@ dependencies = [ [[package]] name = "tabled" version = "0.7.0" -source = "git+https://github.com/zhiburt/tabled?rev=0ac6b7b7b89b56b23ba479d1b1cfe1976b71fd62#0ac6b7b7b89b56b23ba479d1b1cfe1976b71fd62" +source = "git+https://github.com/zhiburt/tabled?rev=5ac9aa53196cd5bbf570daa5357a76aa238722c1#5ac9aa53196cd5bbf570daa5357a76aa238722c1" dependencies = [ "ansi-str 0.2.0", "papergrid", @@ -4800,7 +4800,7 @@ dependencies = [ [[package]] name = "tabled_derive" version = "0.3.0" -source = "git+https://github.com/zhiburt/tabled?rev=0ac6b7b7b89b56b23ba479d1b1cfe1976b71fd62#0ac6b7b7b89b56b23ba479d1b1cfe1976b71fd62" +source = "git+https://github.com/zhiburt/tabled?rev=5ac9aa53196cd5bbf570daa5357a76aa238722c1#5ac9aa53196cd5bbf570daa5357a76aa238722c1" dependencies = [ "heck 0.4.0", "proc-macro-error", diff --git a/crates/nu-table/Cargo.toml b/crates/nu-table/Cargo.toml index 90b0744f8e..6b3bbae117 100644 --- a/crates/nu-table/Cargo.toml +++ b/crates/nu-table/Cargo.toml @@ -16,4 +16,4 @@ nu-ansi-term = "0.46.0" nu-protocol = { path = "../nu-protocol", version = "0.65.1" } strip-ansi-escapes = "0.1.1" atty = "0.2.14" -tabled = { git = "https://github.com/zhiburt/tabled", rev = "0ac6b7b7b89b56b23ba479d1b1cfe1976b71fd62", features = ["color"] } +tabled = { git = "https://github.com/zhiburt/tabled", rev = "5ac9aa53196cd5bbf570daa5357a76aa238722c1", features = ["color"] } diff --git a/crates/nu-table/src/table.rs b/crates/nu-table/src/table.rs index 1f23ecd5d8..2e3d6af289 100644 --- a/crates/nu-table/src/table.rs +++ b/crates/nu-table/src/table.rs @@ -257,7 +257,12 @@ fn load_theme( with_footer: bool, with_header: bool, ) -> tabled::Table { - table = table.with(theme.theme.clone()); + let mut theme = theme.theme.clone(); + if !with_header { + theme.set_lines(HashMap::default()); + } + + table = table.with(theme); if let Some(color) = color_hm.get("separator") { let color = color.paint(" ").to_string(); @@ -274,10 +279,6 @@ fn load_theme( ); } - if !with_header { - table = table.with(RemoveHeaderLine); - } - table } diff --git a/crates/nu-table/src/table_theme.rs b/crates/nu-table/src/table_theme.rs index d0179eb41a..0d7974f64f 100644 --- a/crates/nu-table/src/table_theme.rs +++ b/crates/nu-table/src/table_theme.rs @@ -1,4 +1,4 @@ -use tabled::{papergrid::Line, style::RawStyle, Style}; +use tabled::style::{Line, RawStyle, Style}; #[derive(Debug, Clone)] pub struct TableTheme { @@ -20,7 +20,9 @@ impl TableTheme { pub fn light() -> TableTheme { Self { - theme: Style::blank().lines([(1, Line::short('─', '─'))]).into(), + theme: Style::blank() + .lines([(1, Line::new(Some('─'), Some('─'), None, None))]) + .into(), } } @@ -41,7 +43,7 @@ impl TableTheme { .top('❤') .bottom('❤') .vertical('❤') - .lines([(1, Line::short('❤', '❤'))]) + .lines([(1, Line::new(Some('❤'), Some('❤'), None, None))]) .into(), } } diff --git a/crates/nu-table/tests/style.rs b/crates/nu-table/tests/style.rs new file mode 100644 index 0000000000..c92ddab001 --- /dev/null +++ b/crates/nu-table/tests/style.rs @@ -0,0 +1,34 @@ +use std::collections::HashMap; + +use nu_protocol::Config; +use nu_table::{Alignments, StyledString, Table, TableTheme, TextStyle}; + +#[test] +fn test_rounded_style() { + let headers = vec![no_style_str("Hello"), no_style_str("World")]; + let data = vec![vec![no_style_str("1"), no_style_str("2")]]; + + let table = Table::new(headers, data.clone(), TableTheme::rounded()); + let table = table.draw_table( + &Config::default(), + &HashMap::default(), + Alignments::default(), + std::usize::MAX, + ); + + assert_eq!(table.as_deref(), Some("╭───────┬───────╮\n│ Hello │ World │\n├───────┼───────┤\n│ 1 │ 2 │\n╰───────┴───────╯")); + + let table = Table::new(Vec::new(), data, TableTheme::rounded()); + let table = table.draw_table( + &Config::default(), + &HashMap::default(), + Alignments::default(), + std::usize::MAX, + ); + + assert_eq!(table.as_deref(), Some("╭───┬───╮\n│ 1 │ 2 │\n╰───┴───╯")); +} + +fn no_style_str(text: &str) -> StyledString { + StyledString::new(text.to_owned(), TextStyle::default()) +}