Fix issues with incorrect terminal width

For some reason, the terminal width isn't always valid, and
then ends up returning `80`.

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier 2022-11-18 10:56:32 +01:00
parent fd936becf1
commit 1d798a80b4
No known key found for this signature in database
3 changed files with 16 additions and 6 deletions

View File

@ -56,9 +56,10 @@ pub fn tip_args(args: fmt::Arguments) {
);
}
pub fn width() -> usize {
let (_, rows) = console::Term::stdout().size();
rows as usize
pub fn width() -> Option<usize> {
console::Term::stdout()
.size_checked()
.map(|(_, cols)| cols as usize)
}
pub fn headline(headline: &str) {

View File

@ -57,7 +57,14 @@ impl<const W: usize> Table<W> {
.ok();
}
}
println!("{}", console::truncate_str(&output, width - 1, ""));
println!(
"{}",
if let Some(width) = width {
console::truncate_str(&output, width - 1, "")
} else {
output.into()
}
);
}
}

View File

@ -39,8 +39,10 @@ impl fmt::Display for TextBox {
.max()
.unwrap_or(0)
+ 2;
if term::width() < width + 2 {
width = term::width() - 2
if let Some(max) = term::width() {
if max < width + 2 {
width = max - 2
}
}
let (connector, header_width) = if !self.first {