-
Notifications
You must be signed in to change notification settings - Fork 87
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
Comments
Hi @jnmdf Ahhhhhhhhhhhhhhhhhhhhhhhh unfortunately there's no such a way. I think it well might be worth to be included in the lib. But otherwise I may do it myself and release it in 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));
}
|
OK I've done it. 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 |"
"+---+----------+----------+"
); |
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
The text was updated successfully, but these errors were encountered: