From 6539381155726ed38728e130134b89946c38302e Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 10 Mar 2023 19:06:09 +0900 Subject: [PATCH] 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. --- src/ui.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ui.rs b/src/ui.rs index e9919eac6..43f53630a 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -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 { - 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