feat(bash): 优化输出截断逻辑,确保处理UTF-8字符边界
This commit is contained in:
parent
9cda2ab8d5
commit
c58644e7bc
@ -61,16 +61,22 @@ impl BashTool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn truncate_output(&self, output: &str) -> String {
|
fn truncate_output(&self, output: &str) -> String {
|
||||||
if output.len() <= MAX_OUTPUT_CHARS {
|
let char_count = output.chars().count();
|
||||||
|
if char_count <= MAX_OUTPUT_CHARS {
|
||||||
return output.to_string();
|
return output.to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
let half = MAX_OUTPUT_CHARS / 2;
|
let half = MAX_OUTPUT_CHARS / 2;
|
||||||
|
let head: String = output.chars().take(half).collect();
|
||||||
|
let tail: String = output
|
||||||
|
.chars()
|
||||||
|
.skip(char_count.saturating_sub(half))
|
||||||
|
.collect();
|
||||||
format!(
|
format!(
|
||||||
"{}...\n\n(... {} chars truncated ...)\n\n{}",
|
"{}...\n\n(... {} chars truncated ...)\n\n{}",
|
||||||
&output[..half],
|
head,
|
||||||
output.len() - MAX_OUTPUT_CHARS,
|
char_count - MAX_OUTPUT_CHARS,
|
||||||
&output[output.len() - half..]
|
tail
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -312,4 +318,15 @@ mod tests {
|
|||||||
assert!(!result.success);
|
assert!(!result.success);
|
||||||
assert!(result.error.unwrap().contains("timed out"));
|
assert!(result.error.unwrap().contains("timed out"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_truncate_output_handles_utf8_char_boundaries() {
|
||||||
|
let tool = BashTool::new();
|
||||||
|
let input = "全".repeat(MAX_OUTPUT_CHARS + 100);
|
||||||
|
|
||||||
|
let output = tool.truncate_output(&input);
|
||||||
|
|
||||||
|
assert!(output.contains("chars truncated"));
|
||||||
|
assert!(output.is_char_boundary(output.len()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user