fix: use UTF-8 safe truncation in bootstrap file preview
Fix panic when displaying workspace files containing multibyte UTF-8 characters by using char_indices().nth() to find safe character boundaries
This commit is contained in:
parent
a5241f34ea
commit
efe7ae53ce
1 changed files with 12 additions and 2 deletions
|
|
@ -209,8 +209,18 @@ fn inject_workspace_file(prompt: &mut String, workspace_dir: &std::path::Path, f
|
|||
return;
|
||||
}
|
||||
let _ = writeln!(prompt, "### {filename}\n");
|
||||
if trimmed.len() > BOOTSTRAP_MAX_CHARS {
|
||||
prompt.push_str(&trimmed[..BOOTSTRAP_MAX_CHARS]);
|
||||
// Use character-boundary-safe truncation for UTF-8
|
||||
let truncated = if trimmed.chars().count() > BOOTSTRAP_MAX_CHARS {
|
||||
trimmed
|
||||
.char_indices()
|
||||
.nth(BOOTSTRAP_MAX_CHARS)
|
||||
.map(|(idx, _)| &trimmed[..idx])
|
||||
.unwrap_or(trimmed)
|
||||
} else {
|
||||
trimmed
|
||||
};
|
||||
if truncated.len() < trimmed.len() {
|
||||
prompt.push_str(truncated);
|
||||
let _ = writeln!(
|
||||
prompt,
|
||||
"\n\n[... truncated at {BOOTSTRAP_MAX_CHARS} chars — use `read` for full file]\n"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue