From c58644e7bc1b92eee6976fecf4bf798266733fc4 Mon Sep 17 00:00:00 2001 From: ooodc <549496103@qq.com> Date: Wed, 22 Apr 2026 12:04:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(bash):=20=E4=BC=98=E5=8C=96=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E6=88=AA=E6=96=AD=E9=80=BB=E8=BE=91=EF=BC=8C=E7=A1=AE?= =?UTF-8?q?=E4=BF=9D=E5=A4=84=E7=90=86UTF-8=E5=AD=97=E7=AC=A6=E8=BE=B9?= =?UTF-8?q?=E7=95=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tools/bash.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/tools/bash.rs b/src/tools/bash.rs index d2c1e85..9b9cd92 100644 --- a/src/tools/bash.rs +++ b/src/tools/bash.rs @@ -61,16 +61,22 @@ impl BashTool { } 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(); } 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!( "{}...\n\n(... {} chars truncated ...)\n\n{}", - &output[..half], - output.len() - MAX_OUTPUT_CHARS, - &output[output.len() - half..] + head, + char_count - MAX_OUTPUT_CHARS, + tail ) } } @@ -312,4 +318,15 @@ mod tests { assert!(!result.success); 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())); + } }