refactor: Remove duplicated functionality by relying on comfy-table
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
@@ -42,39 +42,6 @@ pub fn format_size(size: u64, human_readable: bool) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
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, _)) => {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn size_column(size: u64, human_readable: bool, column_width: usize) -> String {
|
||||
string_column(format_size(size, human_readable), column_width)
|
||||
@@ -175,88 +142,3 @@ pub fn settings_output_format(settings: &config::Settings) -> OutputFormat {
|
||||
.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
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
debug!("Truncating '{}' to max_width: {}", s, max_width);
|
||||
if max_width == 0 {
|
||||
debug!("Max width is 0, returning empty string");
|
||||
return String::new();
|
||||
}
|
||||
|
||||
let char_count = s.chars().count();
|
||||
if char_count <= max_width {
|
||||
debug!("String fits within max_width ({} <= {}), returning original", char_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 {
|
||||
debug!("Max width is 1, returning ellipsis only");
|
||||
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('…');
|
||||
debug!("Truncated result: '{}'", result);
|
||||
result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user