Skip to content

Commit 0b4f92f

Browse files
authored
fix: handle UTF-8 skill description truncation (anomalyco#836)
1 parent 5084049 commit 0b4f92f

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

  • packages/desktop/src-tauri/src/commands

‎packages/desktop/src-tauri/src/commands/skills.rs‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,41 @@ fn extract_description(raw: &str) -> Option<String> {
366366
}
367367

368368
let max = 180;
369-
if cleaned.len() > max {
370-
return Some(format!("{}...", &cleaned[..max]));
369+
let truncated: String = cleaned.chars().take(max).collect();
370+
if truncated.len() < cleaned.len() {
371+
return Some(format!("{}...", truncated));
371372
}
372373
return Some(cleaned);
373374
}
374375

375376
None
376377
}
377378

379+
#[cfg(test)]
380+
mod tests {
381+
use super::extract_description;
382+
383+
#[test]
384+
fn extract_description_truncates_multibyte_text_without_panicking() {
385+
let raw = &"て".repeat(181);
386+
387+
let description = extract_description(raw).expect("description should be present");
388+
389+
assert!(description.ends_with("..."));
390+
assert!(description.is_char_boundary(description.len()));
391+
assert_eq!(description.chars().count(), 183);
392+
}
393+
394+
#[test]
395+
fn extract_description_keeps_short_text_unchanged() {
396+
let raw = "Short description";
397+
398+
let description = extract_description(raw).expect("description should be present");
399+
400+
assert_eq!(description, "Short description");
401+
}
402+
}
403+
378404
#[tauri::command]
379405
pub fn list_local_skills(project_dir: String) -> Result<Vec<LocalSkillCard>, String> {
380406
let project_dir = project_dir.trim();

0 commit comments

Comments
 (0)