feat: Improve info table rendering by truncating wide columns
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
@@ -221,3 +221,57 @@ pub fn get_terminal_width() -> usize {
|
||||
// Default to 80 if all else fails
|
||||
80
|
||||
}
|
||||
|
||||
/// Calculate the maximum width for the last column in a table
|
||||
/// Takes into account the widths of other columns and table borders
|
||||
pub fn calculate_last_column_width(column_widths: &[usize], terminal_width: usize) -> usize {
|
||||
if terminal_width == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Each column has 1 character padding on each side, and there are borders between columns
|
||||
// The table format uses '│' characters between columns, which take 1 character each
|
||||
let total_other_columns_width: usize = column_widths.iter().map(|&w| w + 2).sum();
|
||||
let border_characters = column_widths.len().saturating_sub(1);
|
||||
|
||||
// Total width used by other columns and their formatting
|
||||
let used_width = total_other_columns_width + border_characters;
|
||||
|
||||
if used_width >= terminal_width {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The last column also has 2 characters for padding
|
||||
terminal_width - used_width - 2
|
||||
}
|
||||
|
||||
/// Truncate a string to fit within a maximum width, adding an ellipsis if truncated
|
||||
pub fn truncate_with_ellipsis(s: &str, max_width: usize) -> String {
|
||||
if max_width == 0 {
|
||||
return String::new();
|
||||
}
|
||||
|
||||
if s.chars().count() <= max_width {
|
||||
return s.to_string();
|
||||
}
|
||||
|
||||
// We need to truncate and add an ellipsis
|
||||
// The ellipsis takes 1 character, so we can show max_width-1 characters
|
||||
if max_width == 1 {
|
||||
return "…".to_string();
|
||||
}
|
||||
|
||||
let mut result = String::new();
|
||||
let mut current_width = 0;
|
||||
|
||||
for c in s.chars() {
|
||||
if current_width + 1 > max_width - 1 {
|
||||
break;
|
||||
}
|
||||
result.push(c);
|
||||
current_width += 1;
|
||||
}
|
||||
|
||||
result.push('…');
|
||||
result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user