feat: add ellipsis when truncating strings and only apply max_len for terminal output

Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-26 08:45:48 -03:00
parent 2a233b3d43
commit a92c22b58c
2 changed files with 75 additions and 39 deletions

View File

@@ -46,7 +46,30 @@ pub fn string_column(s: String, column_width: usize) -> String {
if column_width > 0 { if column_width > 0 {
match s.char_indices().nth(column_width) { match s.char_indices().nth(column_width) {
None => s.to_string(), None => s.to_string(),
Some((idx, _)) => s[..idx].to_string(), Some((idx, _)) => {
if column_width > 1 {
// Find where to cut to fit the ellipsis
let mut cut_idx = idx;
// We need to make room for the ellipsis character
// Find the character boundary before the cut index
for i in (0..idx).rev() {
if s.is_char_boundary(i) {
if idx - i >= 3 { // Make sure we have enough room
cut_idx = i;
break;
}
}
}
// If we can't find a good place, just truncate without ellipsis
if cut_idx >= 3 {
format!("{}", &s[..cut_idx - 1])
} else {
s[..idx].to_string()
}
} else {
s[..idx].to_string()
}
}
} }
} else { } else {
s.to_string() s.to_string()

View File

@@ -62,11 +62,17 @@ pub fn mode_list(
return show_list_structured(items_with_meta, data_path, settings, output_format); return show_list_structured(items_with_meta, data_path, settings, output_format);
} }
// Check if output is a terminal
let is_terminal = stdout().is_terminal();
debug!("Output is terminal: {}", is_terminal);
// Get terminal width once, default to 80 if not available // Get terminal width once, default to 80 if not available
let term_width = if let Ok(columns_env) = env::var("COLUMNS") { // Only use max_len when output is a terminal
let term_width = if is_terminal {
if let Ok(columns_env) = env::var("COLUMNS") {
debug!("COLUMNS environment variable: {:?}", columns_env); debug!("COLUMNS environment variable: {:?}", columns_env);
columns_env.parse::<usize>().unwrap_or(80) columns_env.parse::<usize>().unwrap_or(80)
} else if stdout().is_terminal() { } else {
// Try to get terminal size using termsize // Try to get terminal size using termsize
match termsize::get() { match termsize::get() {
Some(size) => { Some(size) => {
@@ -79,9 +85,10 @@ pub fn mode_list(
80 80
} }
} }
}
} else { } else {
debug!("Not a terminal, defaulting to 80"); debug!("Not a terminal, max_len will be ignored");
80 0 // Use 0 to indicate no truncation
}; };
debug!("Terminal width: {}", term_width); debug!("Terminal width: {}", term_width);
@@ -114,7 +121,9 @@ pub fn mode_list(
let mut meta_name: Option<&str> = None; let mut meta_name: Option<&str> = None;
// Parse max_len, handling numbers, percentages, and negative values // Parse max_len, handling numbers, percentages, and negative values
let column_width = if let Some(max_len_str) = &column.max_len { // Only apply max_len when output is a terminal
let column_width = if is_terminal {
if let Some(max_len_str) = &column.max_len {
debug!("Processing max_len for column '{}': {}", column.name, max_len_str); debug!("Processing max_len for column '{}': {}", column.name, max_len_str);
// Check if it's a negative number // Check if it's a negative number
if max_len_str.starts_with('-') { if max_len_str.starts_with('-') {
@@ -142,6 +151,10 @@ pub fn mode_list(
} else { } else {
debug!("No max_len specified for column '{}'", column.name); debug!("No max_len specified for column '{}'", column.name);
0 0
}
} else {
debug!("Output is not a terminal, ignoring max_len");
0
}; };
debug!("Final column width for '{}': {}", column.name, column_width); debug!("Final column width for '{}': {}", column.name, column_width);