Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make the border text centralized #479

Open
jnmdf opened this issue Jan 20, 2025 · 2 comments
Open

How to make the border text centralized #479

jnmdf opened this issue Jan 20, 2025 · 2 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@jnmdf
Copy link

jnmdf commented Jan 20, 2025

let mut t = Table::new(output); t.with(LineText::new("DescribeTargetHealth", Rows::first()).offset(2))
tabled version 0.17.0
Any other approach to set alignment center instead of calculating the offset manually

@zhiburt
Copy link
Owner

zhiburt commented Jan 21, 2025

Hi @jnmdf

Ahhhhhhhhhhhhhhhhhhhhhhhh unfortunately there's no such a way.
And I can tell you it's pretty messy logic to do it right..... 😞

I think it well might be worth to be included in the lib.
If you wish you can contribute based on my example, just let me know.

But otherwise I may do it myself and release it in 0.18 which I am about to get done I wish.

You could do something like this

use std::iter::FromIterator;

use tabled::{
    grid::util::string::get_text_width,
    settings::{object::Rows, split::Split, style::LineText, Style},
    Table,
};

fn main() {
    let table = Table::from_iter(['a'..='z']);

    for layout in [2, 5, 7] {
        let mut table = table.clone();

        table.with(Split::column(layout));
        table.with(Style::modern());

        set_centered_text(&mut table, 0, "Alphabet");

        println!("{table}");
    }
}

fn set_centered_text(table: &mut Table, row: usize, text: &str) {
    let table_w = table.total_width();
    let text_w = get_text_width(text);

    let mut off = 0;
    if table_w > text_w {
        let center = table_w / 2;
        let text_center = text_w / 2;
        off = center.saturating_sub(text_center);
    }

    table.with(LineText::new(text, Rows::single(row)).offset(off));
}
Alphabet┐
│ a │ b │
├───┼───┤
│ c │ d │
├───┼───┤
│ e │ f │
├───┼───┤
│ g │ h │
├───┼───┤
│ i │ j │
├───┼───┤
│ k │ l │
├───┼───┤
│ m │ n │
├───┼───┤
│ o │ p │
├───┼───┤
│ q │ r │
├───┼───┤
│ s │ t │
├───┼───┤
│ u │ v │
├───┼───┤
│ w │ x │
├───┼───┤
│ y │ z │
└───┴───┘
┌───┬─Alphabet──┬───┐
│ a │ b │ c │ d │ e │
├───┼───┼───┼───┼───┤
│ f │ g │ h │ i │ j │
├───┼───┼───┼───┼───┤
│ k │ l │ m │ n │ o │
├───┼───┼───┼───┼───┤
│ p │ q │ r │ s │ t │
├───┼───┼───┼───┼───┤
│ u │ v │ w │ x │ y │
├───┼───┼───┼───┼───┤
│ z │   │   │   │   │
└───┴───┴───┴───┴───┘
┌───┬───┬─Alphabet──┬───┬───┐
│ a │ b │ c │ d │ e │ f │ g │
├───┼───┼───┼───┼───┼───┼───┤
│ h │ i │ j │ k │ l │ m │ n │
├───┼───┼───┼───┼───┼───┼───┤
│ o │ p │ q │ r │ s │ t │ u │
├───┼───┼───┼───┼───┼───┼───┤
│ v │ w │ x │ y │ z │   │   │
└───┴───┴───┴───┴───┴───┴───┘

@zhiburt zhiburt added enhancement New feature or request question Further information is requested labels Jan 21, 2025
@zhiburt
Copy link
Owner

zhiburt commented Jan 21, 2025

OK I've done it.
On the next release you could just call .align(Alignment::center()).

test_table!(
    border_text_alignment_test_0,
    Matrix::table(2, 2)
         .with(LineText::new("TABLE", Rows::first()).align(Alignment::center())),
    "+---+------TABLE----------+"
    "| N | column 0 | column 1 |"
    "+---+----------+----------+"
    "| 0 |   0-0    |   0-1    |"
    "+---+----------+----------+"
    "| 1 |   1-0    |   1-1    |"
    "+---+----------+----------+"
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants