Closed InnocentZero closed 2 months ago
The same problem also occurs with Alignment::right()
so it is breaking Alignment
in general.
Hi @InnocentZero
Yes, interesting case.
The issue is in that we the wrapped string contains the white-spaces,
to be exactly 81
whether possible.
You could trim them and it would work.
And then to set exact width use Width::increase
.
let width = 40;
let todos = vec!["AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AA"; 10];
let mut table = Table::new(todos);
table
.with(Width::wrap(width as usize).keep_words())
.with(Format::content(|s| {
s.lines().map(|l| l.trim()).collect::<Vec<_>>().join("\n")
}))
.with(Width::increase(40))
.with(Alignment::center())
.with(AlignmentStrategy::PerLine);
println!("{table}");
+--------------------------------------+
| &str |
+--------------------------------------+
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AA |
+--------------------------------------+
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AA |
+--------------------------------------+
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AA |
+--------------------------------------+
But overall I guess it's not sound enough behavior. Let me know what you think; and how it shall behave in your mind.
Take care.
Hi. I really appreciate the quick response and the fix also. This works for the time being, but is there a way to not do so many allocations? It's not a major problem, but I'd like to have fewer allocations if possible.
As for the behaviour, while your explanation makes sense, any person who comes across it would probably expect it to stay centred.
Thanks for the awesome library!
is there a way to not do so many allocations
Yessss; it's certainly a bummer
Haaaaa
Look what I've just got TrimStrategy
(see I've already started to forget that we have it :smile:)
It won't make allocations but as you may guess will be generally slower cause we need to do trim adjustments.
use tabled::{
settings::{formatting::AlignmentStrategy, formatting::TrimStrategy, Alignment, Width},
Table,
};
fn main() {
let width = 40;
let todos = vec!["AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AAAA AA"; 3];
let mut table = Table::new(&todos);
table
.with(Width::wrap(width as usize).keep_words())
.with(Alignment::center())
.with(AlignmentStrategy::PerLine)
.with(TrimStrategy::Horizontal);
println!("{table}");
let mut table = Table::new(todos);
table
.with(Width::wrap(width as usize).keep_words())
.with(Alignment::right())
.with(AlignmentStrategy::PerLine)
.with(TrimStrategy::Horizontal);
println!("{table}");
}
+--------------------------------------+
| &str |
+--------------------------------------+
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AA |
+--------------------------------------+
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AA |
+--------------------------------------+
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AA |
+--------------------------------------+
+--------------------------------------+
| &str |
+--------------------------------------+
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AA |
+--------------------------------------+
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AA |
+--------------------------------------+
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AAAA AAAA |
| AAAA AAAA AAAA AAAA AAAA AA |
+--------------------------------------+
This works perfectly! Thanks.
Smallest snippet to reproduce the issue.
This gives
On 81 columns. Removing
keep_words
fixes the issue but I'd like to keep it there because my table looks neater that way.