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 regex::Regex;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::io::{stdout, IsTerminal};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use strum::IntoEnumIterator;
|
use strum::IntoEnumIterator;
|
||||||
|
use termsize;
|
||||||
#[derive(Debug, Clone, strum::EnumString, strum::Display, PartialEq)]
|
#[derive(Debug, Clone, strum::EnumString, strum::Display, PartialEq)]
|
||||||
#[strum(ascii_case_insensitive)]
|
#[strum(ascii_case_insensitive)]
|
||||||
pub enum OutputFormat {
|
pub enum OutputFormat {
|
||||||
@@ -193,3 +195,29 @@ pub fn settings_output_format(settings: &config::Settings) -> OutputFormat {
|
|||||||
.and_then(|s| OutputFormat::from_str(s).ok())
|
.and_then(|s| OutputFormat::from_str(s).ok())
|
||||||
.unwrap_or(OutputFormat::Table)
|
.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
|
||||||
|
}
|
||||||
|
|||||||
@@ -57,31 +57,8 @@ pub fn mode_list(
|
|||||||
let is_terminal = stdout().is_terminal();
|
let is_terminal = stdout().is_terminal();
|
||||||
debug!("Output is terminal: {}", is_terminal);
|
debug!("Output is terminal: {}", is_terminal);
|
||||||
|
|
||||||
// Get terminal width once, default to 80 if not available
|
// Get terminal width using the common function
|
||||||
// Only use max_len when output is a terminal
|
let term_width = crate::modes::common::get_terminal_width();
|
||||||
let term_width = if is_terminal {
|
|
||||||
if let Ok(columns_env) = env::var("COLUMNS") {
|
|
||||||
debug!("COLUMNS environment variable: {:?}", columns_env);
|
|
||||||
columns_env.parse::<usize>().unwrap_or(80)
|
|
||||||
} else {
|
|
||||||
// Try to get terminal size using termsize
|
|
||||||
match termsize::get() {
|
|
||||||
Some(size) => {
|
|
||||||
let width = size.cols as usize;
|
|
||||||
debug!("Terminal size detected: {} columns", width);
|
|
||||||
width
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
debug!("Failed to get terminal size, defaulting to 80");
|
|
||||||
80
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
debug!("Not a terminal, max_len will be ignored");
|
|
||||||
0 // Use 0 to indicate no truncation
|
|
||||||
};
|
|
||||||
|
|
||||||
debug!("Terminal width: {}", term_width);
|
debug!("Terminal width: {}", term_width);
|
||||||
|
|
||||||
let mut table = Table::new();
|
let mut table = Table::new();
|
||||||
|
|||||||
Reference in New Issue
Block a user