Truncate text to fit within a maximum width.
(text: &str, max_width: usize)
| 420 | |
| 421 | /// Truncate text to fit within a maximum width. |
| 422 | fn truncate(text: &str, max_width: usize) -> String { |
| 423 | if max_width == 0 { |
| 424 | return text.to_string(); |
| 425 | } |
| 426 | |
| 427 | let text_width = console::measure_text_width(text); |
| 428 | if text_width <= max_width { |
| 429 | return text.to_string(); |
| 430 | } |
| 431 | |
| 432 | if max_width <= 3 { |
| 433 | return "...".chars().take(max_width).collect(); |
| 434 | } |
| 435 | |
| 436 | // Find a safe truncation point |
| 437 | let mut result = String::new(); |
| 438 | let mut current_width = 0; |
| 439 | let target_width = max_width - 3; // Leave room for "..." |
| 440 | |
| 441 | for ch in text.chars() { |
| 442 | let char_width = console::measure_text_width(&ch.to_string()); |
| 443 | if current_width + char_width > target_width { |
| 444 | break; |
| 445 | } |
| 446 | result.push(ch); |
| 447 | current_width += char_width; |
| 448 | } |
| 449 | |
| 450 | result.push_str("..."); |
| 451 | result |
| 452 | } |
| 453 | |
| 454 | /// Render the table to a string. |
| 455 | fn render(&self) -> String { |
no test coverage detected