ui: allow to override terminal width with $COLUMNS variable

Writing text wrapping tests would be difficult without env/config overrides.
The logic is pretty much the same as ui.termwidth() of Mercurial.
This commit is contained in:
Yuya Nishihara 2023-03-10 19:06:09 +09:00
parent afdf9e19f2
commit 6539381155

View file

@ -15,7 +15,7 @@
use std::io::{Stderr, Stdout, Write};
use std::process::{Child, ChildStdin, Stdio};
use std::str::FromStr;
use std::{fmt, io, mem};
use std::{env, fmt, io, mem};
use crossterm::tty::IsTty;
use maplit::hashmap;
@ -289,7 +289,11 @@ impl Ui {
}
pub fn term_width(&self) -> Option<u16> {
crossterm::terminal::size().ok().map(|(cols, _)| cols)
if let Some(cols) = env::var("COLUMNS").ok().and_then(|s| s.parse().ok()) {
Some(cols)
} else {
crossterm::terminal::size().ok().map(|(cols, _)| cols)
}
}
/// Construct a guard object which writes `data` when dropped. Useful for