zhiburt / tabled

An easy to use library for pretty print tables of Rust structs and enums.
MIT License
1.94k stars 77 forks source link

Using Span::column() with the Style::modern() causes virtual artifact on the intersection in the table #399

Open fbonin opened 5 months ago

fbonin commented 5 months ago

Using Span::column() with the Style::modern() causes virtual artifact on the intersection in the table

To reproduce :

let mut builder = Builder::default();

let mut title = Vec::with_capacity(1);
title.push("Title");
builder.push_record(title);

let mut header = Vec::with_capacity(4);
header.push("header-1");
header.push("header-2");
header.push("header-3");
header.push("header-4");
builder.push_record(header);

let mut record = Vec::with_capacity(4);
record.push("value-1");
record.push("value-2");
record.push("value-3");
record.push("value-4");
builder.push_record(record);

let mut table = builder.build();
table    
.modify((0, 0), Span::column(4))
    .modify((0, 0), Alignment::center())
    .with(Style::modern())
    .to_string();
println!("{}", table);

Artifacts on the header at the intersection :

image

Expected :

image
zhiburt commented 5 months ago

Hi @fbonin

That's actually by design.

Try to use tabled::settings::style::BorderSpanCorrection afterwards.

Let me know if it helped.


Yes I've just noticed it's not stated in README.md. If you want to you can open a PR

Take care

fbonin commented 5 months ago

Thank you !

mkatychev commented 4 months ago

Just a heads up, BorderSpanCorrection doesn't work on all styles:

use tabled::{
    settings::{
        style::{BorderSpanCorrection, Style},
        Modify, Span,
    },
    Table,
};

fn main() {
    let data = vec![("09", "June", "2022"), ("10", "July", "2022")];

    let mut table = Table::new(&data);
    table
        .with(Modify::new((0, 0)).with("date").with(Span::column(3)))
        .with(BorderSpanCorrection);
    println!("{}", table.to_string());
    table.with(Style::modern());
    println!("{}", table.to_string());
}
$ cargo run

+------------------+
| date             |
+----+------+------+
| 09 | June | 2022 |
+----+------+------+
| 10 | July | 2022 |
+----+------+------+
┌────┬──────┬──────┐
│ date             │
├────┼──────┼──────┤
│ 09 │ June │ 2022 │
├────┼──────┼──────┤
│ 10 │ July │ 2022 │
└────┴──────┴──────┘
zhiburt commented 4 months ago

Hi @mkatychev

Good observation, The thing is that you change style after BorderSpanCorrection make its changes.

So essentially you could call it one more time to fix it. Or if you would make them after applying the style, you'd get a different result.

Yesssssssssss, Surely this must be noted in documentation (ref #402)

You can help to update the doc yourself if you want to.

Let me know if that helps, and Take care.

use tabled::{
    settings::{
        style::{BorderSpanCorrection, Style},
        Modify, Span,
    },
    Table,
};

fn main() {
    let data = vec![("09", "June", "2022"), ("10", "July", "2022")];

    let mut table = Table::new(data);
    table
        .with(Modify::new((0, 0)).with("date").with(Span::column(3)))
        .with(Style::modern());

    println!("{table}");

    table.with(BorderSpanCorrection);

    println!("{table}");
}
┌────┬──────┬──────┐
│ date             │
├────┼──────┼──────┤
│ 09 │ June │ 2022 │
├────┼──────┼──────┤
│ 10 │ July │ 2022 │
└────┴──────┴──────┘
┌──────────────────┐
│ date             │
├────┬──────┬──────┤
│ 09 │ June │ 2022 │
├────┼──────┼──────┤
│ 10 │ July │ 2022 │
└────┴──────┴──────┘

PS: Once again a good observation.

zhiburt commented 4 months ago

Note

Could be done with Panel and Disable.

use tabled::{
    settings::{
        object::Rows,
        style::{BorderSpanCorrection, Style},
        Disable, Panel,
    },
    Table,
};

fn main() {
    let data = vec![("09", "June", "2022"), ("10", "July", "2022")];

    let mut table = Table::new(data);
    table
        .with(Disable::row(Rows::first()))
        .with(Panel::header("date"))
        .with(Style::modern());

    println!("{table}");

    table.with(BorderSpanCorrection);

    println!("{table}");
}
zhiburt commented 4 months ago

Or like this without Disable and using Table::from_iter.

use std::iter::FromIterator;

use tabled::{
    settings::{
        style::{BorderSpanCorrection, Style},
        Panel,
    },
    Table,
};

fn main() {
    let data = vec![["09", "June", "2022"], ["10", "July", "2022"]];

    let mut table = Table::from_iter(data);
    table.with(Panel::header("date")).with(Style::modern());

    println!("{table}");

    table.with(BorderSpanCorrection);

    println!("{table}");
}
mkatychev commented 4 months ago

I can confirm the first approach worked flawlessly with 3 or more columns, thanks @zhiburt. Panel::corrected_header would be valuable as a convenience method although it seems that one needs a &mut Table to do these inside of Table::with.