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:
@@ -46,7 +46,30 @@ pub fn string_column(s: String, column_width: usize) -> String {
|
||||
if column_width > 0 {
|
||||
match s.char_indices().nth(column_width) {
|
||||
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 {
|
||||
s.to_string()
|
||||
|
||||
@@ -62,11 +62,17 @@ pub fn mode_list(
|
||||
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
|
||||
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);
|
||||
columns_env.parse::<usize>().unwrap_or(80)
|
||||
} else if stdout().is_terminal() {
|
||||
} else {
|
||||
// Try to get terminal size using termsize
|
||||
match termsize::get() {
|
||||
Some(size) => {
|
||||
@@ -79,9 +85,10 @@ pub fn mode_list(
|
||||
80
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
debug!("Not a terminal, defaulting to 80");
|
||||
80
|
||||
debug!("Not a terminal, max_len will be ignored");
|
||||
0 // Use 0 to indicate no truncation
|
||||
};
|
||||
|
||||
debug!("Terminal width: {}", term_width);
|
||||
@@ -114,7 +121,9 @@ pub fn mode_list(
|
||||
let mut meta_name: Option<&str> = None;
|
||||
|
||||
// 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);
|
||||
// Check if it's a negative number
|
||||
if max_len_str.starts_with('-') {
|
||||
@@ -142,6 +151,10 @@ pub fn mode_list(
|
||||
} else {
|
||||
debug!("No max_len specified for column '{}'", column.name);
|
||||
0
|
||||
}
|
||||
} else {
|
||||
debug!("Output is not a terminal, ignoring max_len");
|
||||
0
|
||||
};
|
||||
|
||||
debug!("Final column width for '{}': {}", column.name, column_width);
|
||||
|
||||
Reference in New Issue
Block a user