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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user