Skip to content

Commit e1862da

Browse files
committed
refactor: consolidate truncate functions to use cortex-common
- Replace local truncate_model_name in billing_client.rs with cortex_common::truncate_model_name - Replace local truncate_command in permission/prompts.rs with cortex_common::truncate_command - Replace local truncate_for_display in edit_file.rs with cortex_common::truncate_for_display - Replace local truncate_string in edit_strategies.rs with cortex_common::truncate_for_display - Fix test expectations for truncate_model_name (correct max_len calculation) - Add comment clarifying unicode-aware truncation in text_utils.rs This reduces code duplication and ensures consistent truncation behavior across the codebase. All truncation functions now delegate to the centralized implementations in cortex-common.
1 parent 60bba58 commit e1862da

File tree

6 files changed

+21
-24
lines changed

6 files changed

+21
-24
lines changed

cortex-common/src/truncate.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,10 @@ pub fn truncate_model_name(name: &str, max_len: usize) -> Cow<'_, str> {
139139
// Try to keep the model family prefix if possible
140140
if let Some(slash_pos) = name.find('/') {
141141
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);
142+
// Check if we have enough room for prefix + "..." + at least 5 chars of suffix
143+
if prefix.len() + 3 + 5 <= max_len {
144+
let suffix_len = max_len - prefix.len() - 3;
145+
let suffix_start = name.len().saturating_sub(suffix_len);
145146
return Cow::Owned(format!("{}...{}", prefix, &name[suffix_start..]));
146147
}
147148
}
@@ -210,8 +211,16 @@ mod tests {
210211
#[test]
211212
fn test_truncate_model_name() {
212213
assert_eq!(truncate_model_name("gpt-4", 10).as_ref(), "gpt-4");
214+
// For max_len=20: prefix "anthropic/" (10) + "..." (3) + suffix (7) = 20
215+
// suffix_start = 32 - 7 = 25, so suffix is "0240229"
213216
assert_eq!(
214217
truncate_model_name("anthropic/claude-3-opus-20240229", 20).as_ref(),
218+
"anthropic/...0240229"
219+
);
220+
// For max_len=21: prefix "anthropic/" (10) + "..." (3) + suffix (8) = 21
221+
// suffix_start = 32 - 8 = 24, so suffix is "20240229"
222+
assert_eq!(
223+
truncate_model_name("anthropic/claude-3-opus-20240229", 21).as_ref(),
215224
"anthropic/...20240229"
216225
);
217226
}

cortex-engine/src/billing_client.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,7 @@ fn format_number(n: u64) -> String {
596596

597597
/// Truncates a model name to fit display width.
598598
fn truncate_model_name(name: &str, max_len: usize) -> String {
599-
if name.len() <= max_len {
600-
name.to_string()
601-
} else {
602-
format!("{}...", &name[..max_len - 3])
603-
}
599+
cortex_common::truncate_model_name(name, max_len).into_owned()
604600
}
605601

606602
// ============================================================================

cortex-engine/src/permission/prompts.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,7 @@ impl fmt::Display for PermissionPrompt {
214214

215215
/// Truncate a command for display.
216216
fn truncate_command(command: &str, max_len: usize) -> String {
217-
if command.len() <= max_len {
218-
command.to_string()
219-
} else {
220-
format!("{}...", &command[..max_len - 3])
221-
}
217+
cortex_common::truncate_command(command, max_len).into_owned()
222218
}
223219

224220
/// Format a permission list for display.
@@ -325,9 +321,11 @@ mod tests {
325321
#[test]
326322
fn test_truncate_command() {
327323
assert_eq!(truncate_command("short", 10), "short");
324+
// truncate_command tries to cut on word boundaries, so "this is a..." (12 chars)
325+
// instead of "this is a ve..." (14 chars) because we look for last space
328326
assert_eq!(
329327
truncate_command("this is a very long command", 15),
330-
"this is a ve..."
328+
"this is a..."
331329
);
332330
}
333331
}

cortex-engine/src/tools/handlers/edit_file.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,5 @@ pub fn diagnose_match(content: &str, search: &str) -> Vec<(&'static str, bool, f
314314

315315
/// Truncate string for display in error messages
316316
fn truncate_for_display(s: &str, max_len: usize) -> String {
317-
if s.len() <= max_len {
318-
s.to_string()
319-
} else {
320-
format!("{}...", &s[..max_len.saturating_sub(3)])
321-
}
317+
cortex_common::truncate_for_display(s, max_len).into_owned()
322318
}

cortex-engine/src/tools/handlers/edit_strategies.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -984,11 +984,7 @@ fn find_context_match(content: &str, search: &str, context_lines: usize) -> Opti
984984

985985
/// Truncates a string for display purposes.
986986
fn truncate_string(s: &str, max_len: usize) -> String {
987-
if s.len() <= max_len {
988-
s.to_string()
989-
} else {
990-
format!("{}...", &s[..max_len.saturating_sub(3)])
991-
}
987+
cortex_common::truncate_for_display(s, max_len).into_owned()
992988
}
993989

994990
// =============================================================================

cortex-tui/src/ui/text_utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ pub const MIN_TERMINAL_WIDTH: u16 = 40;
3737
/// assert_eq!(truncate_with_ellipsis("Test", 3), "...");
3838
/// ```
3939
pub fn truncate_with_ellipsis(text: &str, max_width: usize) -> String {
40+
// Use unicode-aware width calculation for proper handling of
41+
// CJK characters and other wide glyphs
4042
let text_width = UnicodeWidthStr::width(text);
4143

4244
if text_width <= max_width {

0 commit comments

Comments
 (0)