Skip to content

Commit 2ad84af

Browse files
committed
handle cyrillic characters in history
1 parent 9a1d8b4 commit 2ad84af

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

src/history.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,13 @@ impl HistoryManager {
126126
continue;
127127
}
128128

129-
return if preview.len() > MAX_PREVIEW_LENGTH {
130-
format!("{}...", &preview[..MAX_PREVIEW_LENGTH])
129+
return if preview.chars().count() > MAX_PREVIEW_LENGTH {
130+
let truncate_at = preview
131+
.char_indices()
132+
.nth(MAX_PREVIEW_LENGTH)
133+
.map(|(idx, _)| idx)
134+
.unwrap_or(preview.len());
135+
format!("{}...", &preview[..truncate_at])
131136
} else {
132137
preview.to_string()
133138
};
@@ -381,5 +386,15 @@ mod tests {
381386
let preview = HistoryManager::extract_preview(&messages);
382387
assert_eq!(preview.len(), MAX_PREVIEW_LENGTH + 3);
383388
assert!(preview.ends_with("..."));
389+
390+
// Test UTF-8 multi-byte characters (Cyrillic)
391+
let cyrillic_message = "създай текстов файл test-3.txt";
392+
let messages = vec![Message::user(cyrillic_message)];
393+
let preview = HistoryManager::extract_preview(&messages);
394+
// Should not panic and should truncate at character boundary
395+
assert!(preview.chars().count() <= MAX_PREVIEW_LENGTH + 3); // +3 for "..."
396+
if preview.ends_with("...") {
397+
assert!(preview.chars().count() <= MAX_PREVIEW_LENGTH + 3);
398+
}
384399
}
385400
}

0 commit comments

Comments
 (0)