File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments