refactor: Move terminal width detection to common utility function

Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-09-08 17:25:36 -03:00
parent 8b38c3e345
commit 44d039a7c2
2 changed files with 30 additions and 25 deletions

View File

@@ -8,8 +8,10 @@ use prettytable::format::TableFormat;
use regex::Regex;
use std::collections::HashMap;
use std::env;
use std::io::{stdout, IsTerminal};
use std::str::FromStr;
use strum::IntoEnumIterator;
use termsize;
#[derive(Debug, Clone, strum::EnumString, strum::Display, PartialEq)]
#[strum(ascii_case_insensitive)]
pub enum OutputFormat {
@@ -193,3 +195,29 @@ pub fn settings_output_format(settings: &config::Settings) -> OutputFormat {
.and_then(|s| OutputFormat::from_str(s).ok())
.unwrap_or(OutputFormat::Table)
}
/// Get the terminal width, considering environment variables and actual terminal size
/// Returns 0 if output is not a terminal or width cannot be determined
pub fn get_terminal_width() -> usize {
use std::io::{stdout, IsTerminal};
// Check if output is a terminal
if !stdout().is_terminal() {
return 0;
}
// Try to get width from COLUMNS environment variable first
if let Ok(columns_env) = env::var("COLUMNS") {
if let Ok(width) = columns_env.parse::<usize>() {
return width;
}
}
// Fall back to querying the terminal size
if let Some(size) = termsize::get() {
return size.cols as usize;
}
// Default to 80 if all else fails
80
}