Skip to content

Commit 83e7aa7

Browse files
committed
refactor: use truncate_safe_in_place for UTF-8 safe truncation
Replace custom truncate logic in truncate_description with the existing truncate_safe_in_place utility function to ensure consistency across the codebase and leverage tested UTF-8 safe truncation logic.
1 parent bcc92ce commit 83e7aa7

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

crates/chat-cli/src/cli/chat/cli/prompts.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use unicode_width::UnicodeWidthStr;
3333

3434
use crate::cli::chat::cli::editor::open_editor_file;
3535
use crate::cli::chat::tool_manager::PromptBundle;
36+
use crate::cli::chat::util::truncate_safe_in_place;
3637
use crate::cli::chat::{
3738
ChatError,
3839
ChatSession,
@@ -248,23 +249,11 @@ fn format_description(description: Option<&String>) -> String {
248249
/// to ensure clean formatting. This function is UTF-8 safe and will not panic
249250
/// on multibyte characters (e.g., CJK languages).
250251
fn truncate_description(text: &str, max_length: usize) -> String {
251-
if text.len() <= max_length {
252-
text.to_string()
253-
} else {
254-
// UTF-8 safe truncation: find the last valid character boundary
255-
let target_len = max_length.saturating_sub(3);
256-
let mut truncate_at = 0;
257-
258-
for (idx, _) in text.char_indices() {
259-
if idx > target_len {
260-
break;
261-
}
262-
truncate_at = idx;
263-
}
264-
265-
let truncated = &text[..truncate_at];
266-
format!("{}...", truncated.trim_end())
267-
}
252+
let mut result = text.to_string();
253+
254+
truncate_safe_in_place(&mut result, max_length, "...");
255+
256+
result
268257
}
269258

270259
/// Represents parsed MCP error details for generating user-friendly messages.
@@ -2282,7 +2271,7 @@ mod tests {
22822271
// Edge case: max_length very small (result will be just "...")
22832272
let result = truncate_description("한국어", 5);
22842273
assert_eq!(result, "...");
2285-
2274+
22862275
let result = truncate_description("ABCDEF", 5);
22872276
assert_eq!(result, "AB...");
22882277

@@ -2302,7 +2291,7 @@ mod tests {
23022291
let result = truncate_description(mixed, 15);
23032292
assert!(result.len() <= 15);
23042293
assert!(result.ends_with("..."));
2305-
2294+
23062295
// Edge case: single CJK character that fits
23072296
let result = truncate_description("한", 10);
23082297
assert_eq!(result, "한");

0 commit comments

Comments
 (0)