nu-table/ table --collapse style fix (#8041)

close #8033

So as I said they suppose to be there;

I've basically added a change of style if --collapse is used.

PS: I guess it's worth to add tests so hold on if you'll plan to merge
it.

---------

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
This commit is contained in:
Maxim Zhiburt 2023-02-16 22:32:07 +03:00 committed by GitHub
parent 1fd1a3a456
commit daeb3e5187
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 274 additions and 4 deletions

View File

@ -1,6 +1,6 @@
use nu_test_support::fs::Stub::FileWithContent;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
use nu_test_support::{nu, nu_repl_code, pipeline};
#[test]
fn table_0() {
@ -21,7 +21,213 @@ fn table_collapse_0() {
let actual = nu!(r#"[[a b, c]; [1 2 3] [4 5 [1 2 3]]] | table --collapse"#);
assert_eq!(
actual.out,
"╭───┬───┬───╮│ a │ b │ c │ ─── ─── ─── │ 1 │ 2 │ 3 │ ─── ─── ─── │ 4 │ 5 │ 1 ││ │ ─── │ │ │ 2 ││ │ ─── │ │ │ 3 │╰───┴───┴───╯"
"╭───┬───┬───╮\
a b c \
\
1 2 3 \
\
4 5 1 \
\
2 \
\
3 \
"
);
}
#[test]
fn table_collapse_basic() {
let actual = nu!(nu_repl_code(&[
"let-env config = { table_mode: basic };",
"[[a b, c]; [1 2 3] [4 5 [1 2 3]]] | table --collapse"
]));
assert_eq!(
actual.out,
"+---+---+---+\
| a | b | c |\
+---+---+---+\
| 1 | 2 | 3 |\
+---+---+---+\
| 4 | 5 | 1 |\
| | +---+\
| | | 2 |\
| | +---+\
| | | 3 |\
+---+---+---+"
);
}
#[test]
fn table_collapse_heavy() {
let actual = nu!(nu_repl_code(&[
"let-env config = { table_mode: heavy };",
"[[a b, c]; [1 2 3] [4 5 [1 2 3]]] | table --collapse"
]));
assert_eq!(
actual.out,
"┏━━━┳━━━┳━━━┓\
a b c \
\
1 2 3 \
\
4 5 1 \
\
2 \
\
3 \
"
);
}
#[test]
fn table_collapse_compact() {
let actual = nu!(nu_repl_code(&[
"let-env config = { table_mode: compact };",
"[[a b, c]; [1 2 3] [4 5 [1 2 3]]] | table --collapse"
]));
assert_eq!(
actual.out,
"┌───┬───┬───┐\
a b c \
\
1 2 3 \
\
4 5 1 \
\
2 \
\
3 \
"
);
}
#[test]
fn table_collapse_compact_double() {
let actual = nu!(nu_repl_code(&[
"let-env config = { table_mode: compact_double };",
"[[a b, c]; [1 2 3] [4 5 [1 2 3]]] | table --collapse"
]));
assert_eq!(
actual.out,
"╔═══╦═══╦═══╗\
a b c \
\
1 2 3 \
\
4 5 1 \
\
2 \
\
3 \
"
);
}
#[test]
fn table_collapse_compact_light() {
let actual = nu!(nu_repl_code(&[
"let-env config = { table_mode: light };",
"[[a b, c]; [1 2 3] [4 5 [1 2 3]]] | table --collapse"
]));
assert_eq!(
actual.out,
"┌───┬───┬───┐\
a b c \
\
1 2 3 \
\
4 5 1 \
\
2 \
\
3 \
"
);
}
#[test]
fn table_collapse_none() {
let actual = nu!(nu_repl_code(&[
"let-env config = { table_mode: none };",
"[[a b, c]; [1 2 3] [4 5 [1 2 3]]] | table --collapse"
]));
assert_eq!(
actual.out,
concat!(
" a b c ",
" 1 2 3 ",
" 4 5 1 ",
" 2 ",
" 3 ",
)
);
}
#[test]
fn table_collapse_compact_reinforced() {
let actual = nu!(nu_repl_code(&[
"let-env config = { table_mode: reinforced };",
"[[a b, c]; [1 2 3] [4 5 [1 2 3]]] | table --collapse"
]));
assert_eq!(
actual.out,
"┏───┬───┬───┓\
a b c \
\
1 2 3 \
\
4 5 1 \
\
2 \
\
3 \
"
);
}
#[test]
fn table_collapse_compact_thin() {
let actual = nu!(nu_repl_code(&[
"let-env config = { table_mode: thin };",
"[[a b, c]; [1 2 3] [4 5 [1 2 3]]] | table --collapse"
]));
assert_eq!(
actual.out,
"┌───┬───┬───┐\
a b c \
\
1 2 3 \
\
4 5 1 \
\
2 \
\
3 \
"
);
}
#[test]
fn table_collapse_hearts() {
let actual = nu!(nu_repl_code(&[
"let-env config = { table_mode: with_love };",
"[[a b, c]; [1 2 3] [4 5 [1 2 3]]] | table --collapse"
]));
assert_eq!(
actual.out,
concat!(
"❤❤❤❤❤❤❤❤❤❤❤❤❤",
"❤ a ❤ b ❤ c ❤",
"❤❤❤❤❤❤❤❤❤❤❤❤❤",
"❤ 1 ❤ 2 ❤ 3 ❤",
"❤❤❤❤❤❤❤❤❤❤❤❤❤",
"❤ 4 ❤ 5 ❤ 1 ❤",
"❤ ❤ ❤❤❤❤❤",
"❤ ❤ ❤ 2 ❤",
"❤ ❤ ❤❤❤❤❤",
"❤ ❤ ❤ 3 ❤",
"❤❤❤❤❤❤❤❤❤❤❤❤❤",
)
);
}

View File

@ -201,7 +201,7 @@ fn load_theme<R>(table: &mut tabled::Table<R>, style_computer: &StyleComputer, t
where
R: Records,
{
let mut theme = theme.theme.clone();
let mut theme = theme.into_full().unwrap_or_else(|| theme.theme.clone());
theme.set_horizontals(HashMap::default());
table.with(theme);

View File

@ -7,12 +7,14 @@ use tabled::{
pub struct TableTheme {
pub(crate) theme: RawStyle,
has_inner: bool,
full_theme: Option<RawStyle>,
}
impl TableTheme {
pub fn basic() -> TableTheme {
Self {
theme: Style::ascii().into(),
full_theme: None,
has_inner: true,
}
}
@ -20,6 +22,7 @@ impl TableTheme {
pub fn thin() -> TableTheme {
Self {
theme: Style::modern().into(),
full_theme: None,
has_inner: true,
}
}
@ -32,7 +35,7 @@ impl TableTheme {
Line::new(Some('─'), Some('─'), None, None),
)])
.into(),
full_theme: Some(Style::modern().into()),
has_inner: true,
}
}
@ -47,6 +50,7 @@ impl TableTheme {
.left(None)
.right(None)])
.into(),
full_theme: Some(Style::modern().into()),
has_inner: true,
}
}
@ -62,6 +66,25 @@ impl TableTheme {
Line::new(Some('❤'), Some('❤'), None, None),
)])
.into(),
full_theme: Some(
Style::empty()
.top('❤')
.bottom('❤')
.vertical('❤')
.horizontal('❤')
.left('❤')
.right('❤')
.top_intersection('❤')
.top_left_corner('❤')
.top_right_corner('❤')
.bottom_intersection('❤')
.bottom_left_corner('❤')
.bottom_right_corner('❤')
.right_intersection('❤')
.left_intersection('❤')
.inner_intersection('❤')
.into(),
),
has_inner: true,
}
}
@ -76,6 +99,7 @@ impl TableTheme {
.left(None)
.right(None)])
.into(),
full_theme: Some(Style::extended().into()),
has_inner: true,
}
}
@ -83,6 +107,14 @@ impl TableTheme {
pub fn rounded() -> TableTheme {
Self {
theme: Style::rounded().into(),
full_theme: Some(
Style::modern()
.top_left_corner('╭')
.top_right_corner('╮')
.bottom_left_corner('╰')
.bottom_right_corner('╯')
.into(),
),
has_inner: true,
}
}
@ -96,6 +128,14 @@ impl TableTheme {
.bottom_right_corner('┛')
.off_horizontal()
.into(),
full_theme: Some(
Style::modern()
.top_left_corner('┏')
.top_right_corner('┓')
.bottom_left_corner('┗')
.bottom_right_corner('┛')
.into(),
),
has_inner: true,
}
}
@ -116,6 +156,25 @@ impl TableTheme {
.bottom_right_corner('┛')
.horizontals([HorizontalLine::new(1, Line::full('━', '╋', '┣', '┫'))])
.into(),
full_theme: Some(
Style::modern()
.top('━')
.bottom('━')
.vertical('┃')
.left('┃')
.right('┃')
.top_intersection('┳')
.bottom_intersection('┻')
.top_left_corner('┏')
.top_right_corner('┓')
.bottom_left_corner('┗')
.bottom_right_corner('┛')
.horizontal('━')
.left_intersection('┣')
.right_intersection('┫')
.inner_intersection('╋')
.into(),
),
has_inner: true,
}
}
@ -123,6 +182,7 @@ impl TableTheme {
pub fn none() -> TableTheme {
Self {
theme: Style::blank().into(),
full_theme: None,
has_inner: true,
}
}
@ -151,4 +211,8 @@ impl TableTheme {
pub fn has_inner(&self) -> bool {
self.has_inner
}
pub fn into_full(&self) -> Option<RawStyle> {
self.full_theme.clone()
}
}