use crate::CompatibilityMode;
pub fn resolve_width(
explicit_w: Option<u32>,
use_t: bool,
columns_env: Option<u32>,
is_tty: bool,
mode: CompatibilityMode,
) -> u32 {
if let Some(w) = explicit_w {
return w;
}
let auto_t = mode == CompatibilityMode::Default && is_tty;
if use_t || auto_t {
if let Some(w) = detect_terminal_width() {
return w;
}
if let Some(w) = columns_env {
return w;
}
}
80
}
pub fn detect_terminal_width() -> Option<u32> {
use std::io;
let (terminal_size::Width(cols), _) = terminal_size::terminal_size_of(io::stdout())?;
Some(u32::from(cols))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn explicit_w_wins() {
assert_eq!(
resolve_width(Some(120), true, Some(70), true, CompatibilityMode::Default),
120
);
}
#[test]
fn columns_fallback_when_terminal_lookup_fails() {
let got = resolve_width(None, true, Some(70), false, CompatibilityMode::Default);
assert!(got == 70 || got >= 1, "got = {got}");
}
#[test]
fn default_no_flags_is_eighty() {
assert_eq!(
resolve_width(None, false, None, false, CompatibilityMode::Default),
80
);
}
#[test]
fn strict_does_not_auto_apply_t() {
assert_eq!(
resolve_width(None, false, None, true, CompatibilityMode::Strict),
80
);
}
}