From cb133ed387bfd13131f8ba94bc65eecbb2560d64 Mon Sep 17 00:00:00 2001 From: German David <53343401+gmr458@users.noreply.github.com> Date: Thu, 1 May 2025 15:30:57 -0500 Subject: [PATCH] feat(table): Add new 'single' table mode (#15672) closes #15381 # Description Adds a new table mode called `single`, it looks like the `heavy` mode, but the key difference is that it uses thinner lines. I decided on the name `single` because it's one of the border styles Neovim uses, and they look practically the same. # User-Facing Changes New config option: ```nushell $env.config.table.mode = 'single' ``` # Tests + Formatting Added new tests in `crates/nu-table/tests/style.rs` to cover the single table mode. # After Submitting --- crates/nu-protocol/src/config/table.rs | 4 +- crates/nu-table/src/common.rs | 1 + crates/nu-table/src/table_theme.rs | 10 ++++ crates/nu-table/tests/style.rs | 46 +++++++++++++++++++ .../nu-utils/src/default_files/doc_config.nu | 2 +- 5 files changed, 61 insertions(+), 2 deletions(-) diff --git a/crates/nu-protocol/src/config/table.rs b/crates/nu-protocol/src/config/table.rs index baf4b65412..74893cd9c7 100644 --- a/crates/nu-protocol/src/config/table.rs +++ b/crates/nu-protocol/src/config/table.rs @@ -20,6 +20,7 @@ pub enum TableMode { Restructured, AsciiRounded, BasicCompact, + Single, } impl FromStr for TableMode { @@ -44,7 +45,8 @@ impl FromStr for TableMode { "restructured" => Ok(Self::Restructured), "ascii_rounded" => Ok(Self::AsciiRounded), "basic_compact" => Ok(Self::BasicCompact), - _ => Err("'basic', 'thin', 'light', 'compact', 'with_love', 'compact_double', 'rounded', 'reinforced', 'heavy', 'none', 'psql', 'markdown', 'dots', 'restructured', 'ascii_rounded', or 'basic_compact'"), + "single" => Ok(Self::Single), + _ => Err("'basic', 'thin', 'light', 'compact', 'with_love', 'compact_double', 'rounded', 'reinforced', 'heavy', 'none', 'psql', 'markdown', 'dots', 'restructured', 'ascii_rounded', 'basic_compact' or 'single'"), } } } diff --git a/crates/nu-table/src/common.rs b/crates/nu-table/src/common.rs index 2df8815bdc..6176cd2670 100644 --- a/crates/nu-table/src/common.rs +++ b/crates/nu-table/src/common.rs @@ -186,6 +186,7 @@ pub fn load_theme(mode: TableMode) -> TableTheme { TableMode::Restructured => TableTheme::restructured(), TableMode::AsciiRounded => TableTheme::ascii_rounded(), TableMode::BasicCompact => TableTheme::basic_compact(), + TableMode::Single => TableTheme::single(), } } diff --git a/crates/nu-table/src/table_theme.rs b/crates/nu-table/src/table_theme.rs index 5c9fb2e06f..58c0838eed 100644 --- a/crates/nu-table/src/table_theme.rs +++ b/crates/nu-table/src/table_theme.rs @@ -156,6 +156,16 @@ impl TableTheme { Self::new(theme, full) } + pub fn single() -> TableTheme { + let full = Style::modern() + .corner_top_left('┌') + .corner_top_right('┐') + .corner_bottom_left('└') + .corner_bottom_right('┘'); + + Self::new(Style::sharp(), full) + } + pub fn none() -> TableTheme { Self::new(Style::blank(), Style::blank()) } diff --git a/crates/nu-table/tests/style.rs b/crates/nu-table/tests/style.rs index abbaf653f3..9bb57fa676 100644 --- a/crates/nu-table/tests/style.rs +++ b/crates/nu-table/tests/style.rs @@ -451,6 +451,52 @@ fn test_with_love() { assert_eq!(create_table_with_size(vec![], true, theme::with_love()), ""); } +#[test] +fn test_single() { + assert_eq!( + create_table(vec![row(4); 3], true, theme::single()), + "┌───┬───┬───┬───┐\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ├───┼───┼───┼───┤\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + └───┴───┴───┴───┘" + ); + + assert_eq!( + create_table(vec![row(4); 2], true, theme::single()), + "┌───┬───┬───┬───┐\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + ├───┼───┼───┼───┤\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + └───┴───┴───┴───┘" + ); + + assert_eq!( + create_table(vec![row(4); 1], true, theme::single()), + "┌───┬───┬───┬───┐\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + └───┴───┴───┴───┘" + ); + + assert_eq!( + create_table(vec![row(4); 1], false, theme::single()), + "┌───┬───┬───┬───┐\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + └───┴───┴───┴───┘" + ); + + assert_eq!( + create_table(vec![row(4); 2], false, theme::single()), + "┌───┬───┬───┬───┐\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + │ 0 │ 1 │ 2 │ 3 │\n\ + └───┴───┴───┴───┘" + ); + + assert_eq!(create_table_with_size(vec![], true, theme::single()), ""); +} + fn create_table(data: Vec>>, with_header: bool, theme: theme) -> String { let mut case = TestCase::new(usize::MAX).theme(theme); if with_header { diff --git a/crates/nu-utils/src/default_files/doc_config.nu b/crates/nu-utils/src/default_files/doc_config.nu index 985c419bcc..0054db712f 100644 --- a/crates/nu-utils/src/default_files/doc_config.nu +++ b/crates/nu-utils/src/default_files/doc_config.nu @@ -300,7 +300,7 @@ $env.config.footer_mode = 25 # Specifies the visual display style of a table # One of: "default", "basic", "compact", "compact_double", "heavy", "light", "none", "reinforced", # "rounded", "thin", "with_love", "psql", "markdown", "dots", "restructured", "ascii_rounded", -# or "basic_compact" +# "basic_compact" or "single" # Can be overridden by passing a table to `| table --theme/-t` $env.config.table.mode = "default"