|
| 1 | +//! Text truncation utilities. |
| 2 | +//! |
| 3 | +//! Provides centralized truncation functions used across the codebase. |
| 4 | +
|
| 5 | +use std::borrow::Cow; |
| 6 | + |
| 7 | +/// Truncates a string to a maximum length, adding ellipsis if truncated. |
| 8 | +/// |
| 9 | +/// # Arguments |
| 10 | +/// * `s` - The string to truncate |
| 11 | +/// * `max_len` - Maximum length (including ellipsis) |
| 12 | +/// |
| 13 | +/// # Returns |
| 14 | +/// The truncated string with "..." appended if truncation occurred. |
| 15 | +/// |
| 16 | +/// # Examples |
| 17 | +/// ``` |
| 18 | +/// use cortex_common::truncate::truncate_with_ellipsis; |
| 19 | +/// |
| 20 | +/// assert_eq!(truncate_with_ellipsis("hello", 10), "hello"); |
| 21 | +/// assert_eq!(truncate_with_ellipsis("hello world", 8), "hello..."); |
| 22 | +/// ``` |
| 23 | +pub fn truncate_with_ellipsis(s: &str, max_len: usize) -> Cow<'_, str> { |
| 24 | + if s.chars().count() <= max_len { |
| 25 | + Cow::Borrowed(s) |
| 26 | + } else { |
| 27 | + let truncated: String = s.chars().take(max_len.saturating_sub(3)).collect(); |
| 28 | + Cow::Owned(format!("{}...", truncated)) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// Truncates a string to a maximum length, adding unicode ellipsis (…) if truncated. |
| 33 | +/// |
| 34 | +/// # Arguments |
| 35 | +/// * `s` - The string to truncate |
| 36 | +/// * `max_len` - Maximum character count (including ellipsis) |
| 37 | +/// |
| 38 | +/// # Returns |
| 39 | +/// The truncated string with "…" appended if truncation occurred. |
| 40 | +/// |
| 41 | +/// # Examples |
| 42 | +/// ``` |
| 43 | +/// use cortex_common::truncate::truncate_with_unicode_ellipsis; |
| 44 | +/// |
| 45 | +/// assert_eq!(truncate_with_unicode_ellipsis("hello", 10), "hello"); |
| 46 | +/// assert_eq!(truncate_with_unicode_ellipsis("hello world", 6), "hello…"); |
| 47 | +/// ``` |
| 48 | +pub fn truncate_with_unicode_ellipsis(s: &str, max_len: usize) -> Cow<'_, str> { |
| 49 | + let char_count = s.chars().count(); |
| 50 | + if char_count <= max_len { |
| 51 | + Cow::Borrowed(s) |
| 52 | + } else { |
| 53 | + let truncated: String = s.chars().take(max_len.saturating_sub(1)).collect(); |
| 54 | + Cow::Owned(format!("{}…", truncated)) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Truncates an ID string to show first N characters with ellipsis. |
| 59 | +/// |
| 60 | +/// Commonly used for displaying tool call IDs, session IDs, etc. |
| 61 | +/// |
| 62 | +/// # Arguments |
| 63 | +/// * `id` - The ID string to truncate |
| 64 | +/// * `max_len` - Maximum length (default 10 if called with truncate_id_default) |
| 65 | +/// |
| 66 | +/// # Returns |
| 67 | +/// The truncated ID with "…" appended if truncation occurred. |
| 68 | +pub fn truncate_id(id: &str, max_len: usize) -> Cow<'_, str> { |
| 69 | + truncate_with_unicode_ellipsis(id, max_len) |
| 70 | +} |
| 71 | + |
| 72 | +/// Truncates an ID with default max length of 10. |
| 73 | +pub fn truncate_id_default(id: &str) -> Cow<'_, str> { |
| 74 | + truncate_id(id, 10) |
| 75 | +} |
| 76 | + |
| 77 | +/// Truncates text to a maximum length, taking only the first line. |
| 78 | +/// |
| 79 | +/// Useful for displaying task descriptions, messages, etc. |
| 80 | +/// |
| 81 | +/// # Arguments |
| 82 | +/// * `text` - The text to truncate |
| 83 | +/// * `max_len` - Maximum character count (including ellipsis) |
| 84 | +/// |
| 85 | +/// # Returns |
| 86 | +/// The first line truncated with "…" if it exceeds max_len. |
| 87 | +pub fn truncate_first_line(text: &str, max_len: usize) -> Cow<'_, str> { |
| 88 | + let first_line = text.lines().next().unwrap_or(text); |
| 89 | + truncate_with_unicode_ellipsis(first_line, max_len) |
| 90 | +} |
| 91 | + |
| 92 | +/// Truncates a command string for display, preserving important parts. |
| 93 | +/// |
| 94 | +/// # Arguments |
| 95 | +/// * `command` - The command to truncate |
| 96 | +/// * `max_len` - Maximum length |
| 97 | +/// |
| 98 | +/// # Returns |
| 99 | +/// The truncated command with "..." appended if truncation occurred. |
| 100 | +pub fn truncate_command(command: &str, max_len: usize) -> Cow<'_, str> { |
| 101 | + if command.len() <= max_len { |
| 102 | + Cow::Borrowed(command) |
| 103 | + } else { |
| 104 | + // Take the first part up to max_len - 3 (for "...") |
| 105 | + let truncated = &command[..max_len.saturating_sub(3).min(command.len())]; |
| 106 | + // Find last space to avoid cutting in middle of word |
| 107 | + if let Some(last_space) = truncated.rfind(' ') { |
| 108 | + Cow::Owned(format!("{}...", &truncated[..last_space])) |
| 109 | + } else { |
| 110 | + Cow::Owned(format!("{}...", truncated)) |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/// Truncates a string for display in UI widgets. |
| 116 | +/// |
| 117 | +/// # Arguments |
| 118 | +/// * `s` - The string to truncate |
| 119 | +/// * `max_len` - Maximum character width |
| 120 | +/// |
| 121 | +/// # Returns |
| 122 | +/// The truncated string for display. |
| 123 | +pub fn truncate_for_display(s: &str, max_len: usize) -> Cow<'_, str> { |
| 124 | + truncate_with_ellipsis(s, max_len) |
| 125 | +} |
| 126 | + |
| 127 | +/// Truncates a model name for display in compact spaces. |
| 128 | +/// |
| 129 | +/// # Arguments |
| 130 | +/// * `name` - The model name |
| 131 | +/// * `max_len` - Maximum length |
| 132 | +/// |
| 133 | +/// # Returns |
| 134 | +/// The truncated model name. |
| 135 | +pub fn truncate_model_name(name: &str, max_len: usize) -> Cow<'_, str> { |
| 136 | + if name.len() <= max_len { |
| 137 | + Cow::Borrowed(name) |
| 138 | + } else { |
| 139 | + // Try to keep the model family prefix if possible |
| 140 | + if let Some(slash_pos) = name.find('/') { |
| 141 | + let prefix = &name[..slash_pos + 1]; |
| 142 | + if prefix.len() < max_len.saturating_sub(5) { |
| 143 | + let remaining = max_len - prefix.len() - 3; |
| 144 | + let suffix_start = name.len().saturating_sub(remaining); |
| 145 | + return Cow::Owned(format!("{}...{}", prefix, &name[suffix_start..])); |
| 146 | + } |
| 147 | + } |
| 148 | + truncate_with_ellipsis(name, max_len) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +#[cfg(test)] |
| 153 | +mod tests { |
| 154 | + use super::*; |
| 155 | + |
| 156 | + #[test] |
| 157 | + fn test_truncate_with_ellipsis_short() { |
| 158 | + assert_eq!(truncate_with_ellipsis("short", 10).as_ref(), "short"); |
| 159 | + } |
| 160 | + |
| 161 | + #[test] |
| 162 | + fn test_truncate_with_ellipsis_exact() { |
| 163 | + assert_eq!(truncate_with_ellipsis("exactlen", 8).as_ref(), "exactlen"); |
| 164 | + } |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn test_truncate_with_ellipsis_long() { |
| 168 | + assert_eq!( |
| 169 | + truncate_with_ellipsis("this is a long string", 10).as_ref(), |
| 170 | + "this is..." |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + #[test] |
| 175 | + fn test_truncate_with_unicode_ellipsis() { |
| 176 | + assert_eq!( |
| 177 | + truncate_with_unicode_ellipsis("hello", 10).as_ref(), |
| 178 | + "hello" |
| 179 | + ); |
| 180 | + assert_eq!( |
| 181 | + truncate_with_unicode_ellipsis("hello world", 6).as_ref(), |
| 182 | + "hello…" |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + #[test] |
| 187 | + fn test_truncate_id() { |
| 188 | + assert_eq!(truncate_id("bg-123456789", 10).as_ref(), "bg-123456…"); |
| 189 | + assert_eq!(truncate_id("bg-1", 10).as_ref(), "bg-1"); |
| 190 | + } |
| 191 | + |
| 192 | + #[test] |
| 193 | + fn test_truncate_first_line() { |
| 194 | + assert_eq!(truncate_first_line("line1\nline2", 20).as_ref(), "line1"); |
| 195 | + assert_eq!( |
| 196 | + truncate_first_line("very long first line", 10).as_ref(), |
| 197 | + "very long…" |
| 198 | + ); |
| 199 | + } |
| 200 | + |
| 201 | + #[test] |
| 202 | + fn test_truncate_command() { |
| 203 | + assert_eq!(truncate_command("ls -la", 20).as_ref(), "ls -la"); |
| 204 | + assert_eq!( |
| 205 | + truncate_command("npm install --save-dev typescript", 20).as_ref(), |
| 206 | + "npm install..." |
| 207 | + ); |
| 208 | + } |
| 209 | + |
| 210 | + #[test] |
| 211 | + fn test_truncate_model_name() { |
| 212 | + assert_eq!(truncate_model_name("gpt-4", 10).as_ref(), "gpt-4"); |
| 213 | + assert_eq!( |
| 214 | + truncate_model_name("anthropic/claude-3-opus-20240229", 20).as_ref(), |
| 215 | + "anthropic/...20240229" |
| 216 | + ); |
| 217 | + } |
| 218 | +} |
0 commit comments