1#![expect(
2 clippy::string_slice,
3 unused_results,
4 reason = "Formatting uses ASCII delimiters and intentionally ignores infallible String mutation results."
5)]
6
7pub fn format_size(size: u64) -> String {
11 const KB: u64 = 1024;
12 const MB: u64 = KB * 1024;
13 const GB: u64 = MB * 1024;
14
15 if size >= GB {
16 format!("{:.1}GB", size as f64 / GB as f64)
17 } else if size >= MB {
18 format!("{:.1}MB", size as f64 / MB as f64)
19 } else if size >= KB {
20 format!("{:.1}KB", size as f64 / KB as f64)
21 } else {
22 format!("{size}B")
23 }
24}
25
26pub fn indent_block(text: &str, indent: &str) -> String {
28 if indent.is_empty() || text.is_empty() {
29 return text.to_string();
30 }
31 let mut indented = String::with_capacity(text.len() + indent.len() * text.lines().count());
32 for (idx, line) in text.split('\n').enumerate() {
33 if idx > 0 {
34 indented.push('\n');
35 }
36 if !line.is_empty() {
37 indented.push_str(indent);
38 }
39 indented.push_str(line);
40 }
41 indented
42}
43
44pub fn truncate_text(text: &str, max_len: usize, ellipsis: &str) -> String {
46 if text.chars().count() <= max_len {
47 return text.to_string();
48 }
49
50 let mut truncated = text.chars().take(max_len).collect::<String>();
51 truncated.push_str(ellipsis);
52 truncated
53}
54
55pub fn truncate_within(text: &str, max_len: usize, ellipsis: &str) -> String {
69 if text.chars().count() <= max_len {
70 return text.to_string();
71 }
72 let keep = max_len.saturating_sub(ellipsis.chars().count());
73 let mut truncated = text.chars().take(keep).collect::<String>();
74 truncated.push_str(ellipsis);
75 truncated
76}
77
78pub fn truncate_middle(text: &str, max_len: usize) -> String {
88 if max_len == 0 {
89 return String::new();
90 }
91 let sanitized: String = text
92 .chars()
93 .map(|c| if matches!(c, '\n' | '\r' | '\t') { ' ' } else { c })
94 .collect();
95 let char_count = sanitized.chars().count();
96 if char_count <= max_len {
97 return sanitized;
98 }
99 if max_len <= 1 {
100 return "…".to_string();
101 }
102 let head_len = max_len / 2;
103 let tail_len = max_len.saturating_sub(head_len + 1);
104
105 let head: String = sanitized.chars().take(head_len).collect();
106 let mut result = String::with_capacity(head.len() + tail_len + 1);
107 result.push_str(&head);
108 result.push('…');
109 if tail_len > 0 {
110 let mut tail_rev: Vec<char> = sanitized.chars().rev().take(tail_len).collect();
111 tail_rev.reverse();
112 let tail: String = tail_rev.into_iter().collect();
113 result.push_str(&tail);
114 }
115 result
116}
117
118pub fn truncate_path_middle(path: &str, max_len: usize) -> String {
125 if max_len == 0 {
126 return String::new();
127 }
128 let char_count = path.chars().count();
129 if char_count <= max_len {
130 return path.to_string();
131 }
132 if max_len <= 1 {
133 return "…".to_string();
134 }
135
136 let head_budget = max_len / 2;
138 let tail_budget = max_len.saturating_sub(head_budget + 1);
139
140 let head_str: String = path.chars().take(head_budget).collect();
144 let head_break = head_str.rfind('/').unwrap_or(head_budget);
145
146 let tail_chars: Vec<char> = path.chars().rev().take(tail_budget).collect();
148 let tail_str: String = tail_chars.iter().rev().collect();
149 let tail_break_from_end = tail_str.find('/').map(|pos| tail_str.len() - pos).unwrap_or(tail_budget);
150
151 let head: String = path.chars().take(head_break).collect();
152 let tail: String = path
153 .chars()
154 .rev()
155 .take(tail_break_from_end)
156 .collect::<Vec<_>>()
157 .into_iter()
158 .rev()
159 .collect();
160
161 format!("{head}…{tail}")
162}
163
164pub fn head_tail_truncate(value: &str, max_chars: usize, marker: &str) -> (String, bool) {
178 const SUFFIX: &str = " [truncated]";
179
180 let total_chars = value.chars().count();
181 if total_chars <= max_chars {
182 return (value.to_string(), false);
183 }
184
185 let marker_chars = marker.chars().count();
186 if max_chars <= marker_chars + 16 {
187 let suffix_len = SUFFIX.chars().count();
188 let truncated = if max_chars > suffix_len {
189 let available = max_chars - suffix_len;
190 let mut result = value.chars().take(available).collect::<String>();
191 result.push_str(SUFFIX);
192 result
193 } else {
194 value.chars().take(max_chars).collect::<String>()
195 };
196 return (truncated, true);
197 }
198
199 let available = max_chars.saturating_sub(marker_chars);
200 let head_chars = (available * 2) / 3;
201 let tail_chars = available.saturating_sub(head_chars);
202 let head = value.chars().take(head_chars).collect::<String>();
203 let tail = value.chars().skip(total_chars.saturating_sub(tail_chars)).collect::<String>();
204 let mut truncated = String::with_capacity(max_chars + 20);
205 truncated.push_str(&head);
206 truncated.push_str(marker);
207 truncated.push_str(&tail);
208 (truncated, true)
209}
210
211pub fn wrap_text_words(text: &str, first_width: usize, continuation_width: usize) -> Vec<String> {
225 let trimmed = text.trim();
226 if trimmed.is_empty() {
227 return Vec::new();
228 }
229
230 let mut result = Vec::new();
231 let mut remaining = trimmed;
232 let mut width = first_width.max(1);
233
234 while remaining.chars().take(width + 1).count() > width {
235 let split = split_at_word_boundary(remaining, width);
236 let (head, tail) = remaining.split_at(split);
237 let head = head.trim();
238 if head.is_empty() {
239 break;
240 }
241 result.push(head.to_string());
242 remaining = tail.trim_start();
243 if remaining.is_empty() {
244 break;
245 }
246 width = continuation_width.max(1);
247 }
248
249 if !remaining.is_empty() {
250 result.push(remaining.to_string());
251 }
252 result
253}
254
255fn split_at_word_boundary(input: &str, width: usize) -> usize {
256 let mut last_space: Option<usize> = None;
257 for (seen, (idx, ch)) in input.char_indices().enumerate() {
258 if seen > width {
259 break;
260 }
261 if ch.is_whitespace() {
262 last_space = Some(idx);
263 }
264 }
265 match last_space {
266 Some(pos) => pos,
267 None => byte_index_for_char_count(input, width),
268 }
269}
270
271fn byte_index_for_char_count(input: &str, chars: usize) -> usize {
272 if chars == 0 {
273 return 0;
274 }
275 let mut seen = 0usize;
276 for (idx, ch) in input.char_indices() {
277 seen += 1;
278 if seen == chars {
279 return idx + ch.len_utf8();
280 }
281 }
282 input.len()
283}
284
285pub fn truncate_byte_budget(text: &str, max_bytes: usize, suffix: &str) -> String {
289 if text.len() <= max_bytes {
290 return text.to_string();
291 }
292 let mut end = max_bytes.min(text.len());
293 while end > 0 && !text.is_char_boundary(end) {
294 end -= 1;
295 }
296 format!("{}{suffix}", &text[..end])
297}
298
299#[inline]
313pub fn is_markdown_fence_delimiter(line: &str) -> bool {
314 let indent = line.len() - line.trim_start().len();
315 if indent > 3 {
316 return false;
317 }
318 let trimmed = line.trim_start();
319 trimmed.starts_with("```") || trimmed.starts_with("~~~")
320}
321
322#[inline]
330pub fn collapse_whitespace(text: &str) -> String {
331 let mut result = String::with_capacity(text.len());
332 let mut pending_space = false;
333 for ch in text.chars() {
334 if ch.is_whitespace() {
335 pending_space = true;
336 } else {
337 if pending_space && !result.is_empty() {
338 result.push(' ');
339 }
340 result.push(ch);
341 pending_space = false;
342 }
343 }
344 result
345}
346
347pub fn clean_reasoning_text(text: &str) -> String {
356 text.lines()
357 .map(str::trim_end)
358 .filter(|line| !line.trim().is_empty())
359 .collect::<Vec<_>>()
360 .join("\n")
361}
362
363pub fn compact_reasoning_text(text: &str) -> String {
379 let mut out: Vec<&str> = Vec::with_capacity(text.lines().count());
380 let mut prev_blank = false;
381 for line in text.lines() {
382 let trimmed = line.trim();
383 let is_blank = trimmed.is_empty();
384 if is_blank {
385 if prev_blank {
386 continue;
387 }
388 out.push("");
389 prev_blank = true;
390 } else {
391 out.push(trimmed);
392 prev_blank = false;
393 }
394 }
395 while out.first().is_some_and(|l| l.trim().is_empty()) {
396 out.remove(0);
397 }
398 while out.last().is_some_and(|l| l.trim().is_empty()) {
399 out.pop();
400 }
401 out.join("\n")
402}
403
404#[cfg(test)]
405mod tests {
406 use super::*;
407
408 #[test]
409 fn truncate_byte_budget_ascii() {
410 assert_eq!(truncate_byte_budget("hello world", 5, "..."), "hello...");
411 assert_eq!(truncate_byte_budget("hi", 10, "..."), "hi");
412 }
413
414 #[test]
415 fn truncate_byte_budget_cjk_no_panic() {
416 let jp = "こんにちは";
418 assert_eq!(truncate_byte_budget(jp, 5, "…"), "こ…");
420 assert_eq!(truncate_byte_budget(jp, 6, "…"), "こん…");
422 }
423
424 #[test]
425 fn truncate_byte_budget_mixed_ascii_cjk() {
426 let mixed = "AB日本語CD";
427 assert_eq!(truncate_byte_budget(mixed, 4, ".."), "AB.."); assert_eq!(truncate_byte_budget(mixed, 5, ".."), "AB日.."); }
431
432 #[test]
433 fn truncate_byte_budget_emoji() {
434 let emoji = "👋🌍"; assert_eq!(truncate_byte_budget(emoji, 5, "!"), "👋!");
436 }
437
438 #[test]
439 fn truncate_byte_budget_zero() {
440 assert_eq!(truncate_byte_budget("abc", 0, "..."), "...");
441 }
442
443 #[test]
444 fn compact_reasoning_text_collapses_blank_runs() {
445 assert_eq!(compact_reasoning_text("line1\n\n\n\nline2\n"), "line1\n\nline2");
446 assert_eq!(compact_reasoning_text("a\n\n\n\n\n\nb"), "a\n\nb");
447 }
448
449 #[test]
450 fn compact_reasoning_text_preserves_single_paragraph_breaks() {
451 assert_eq!(compact_reasoning_text("para one\n\npara two\n"), "para one\n\npara two");
452 }
453
454 #[test]
455 fn compact_reasoning_text_trims_trailing_whitespace() {
456 assert_eq!(compact_reasoning_text(" a \n\n\n b \n"), "a\n\nb");
457 }
458
459 #[test]
460 fn compact_reasoning_text_strips_leading_trailing_blanks() {
461 assert_eq!(compact_reasoning_text("\n\n\nmid\n\n\n"), "mid");
462 assert_eq!(compact_reasoning_text("\n\n\n"), "");
463 assert_eq!(compact_reasoning_text(""), "");
464 }
465
466 #[test]
467 fn wrap_text_words_basic_and_continuation_width() {
468 assert_eq!(wrap_text_words("the quick brown fox", 9, 9), vec!["the quick", "brown fox"]);
469 assert_eq!(wrap_text_words("alpha beta gamma delta", 11, 5), vec!["alpha beta", "gamma", "delta"]);
471 }
472
473 #[test]
474 fn wrap_text_words_blank_and_unicode() {
475 assert!(wrap_text_words(" ", 5, 5).is_empty());
476 let wrapped = wrap_text_words("あいう えお かきく", 3, 3);
478 assert_eq!(wrapped, vec!["あいう", "えお", "かきく"]);
479 }
480
481 #[test]
482 fn truncate_within_reserves_ellipsis_budget() {
483 assert_eq!(truncate_within("hello world", 8, "..."), "hello...");
485 assert_eq!(truncate_within("hi", 8, "..."), "hi");
486 assert_eq!(truncate_within("abcdef", 4, "…"), "abc…");
489 }
490
491 #[test]
492 fn truncate_within_counts_chars() {
493 let jp = "あいうえお"; assert_eq!(truncate_within(jp, 5, "…"), jp);
495 assert_eq!(truncate_within(jp, 3, "…"), "あい…");
496 }
497
498 #[test]
499 fn head_tail_truncate_keeps_both_ends() {
500 let value = "0123456789".repeat(10); let (out, truncated) = head_tail_truncate(&value, 40, " ... [truncated] ... ");
502 assert!(truncated);
503 assert!(out.chars().count() <= 40);
504 assert!(out.starts_with("012"));
505 assert!(out.contains("[truncated]"));
506 assert!(out.ends_with('9'));
507 }
508
509 #[test]
510 fn head_tail_truncate_passes_through_when_short() {
511 let (out, truncated) = head_tail_truncate("short", 64, " ... ");
512 assert_eq!(out, "short");
513 assert!(!truncated);
514 }
515
516 #[test]
517 fn head_tail_truncate_small_budget_falls_back_to_prefix() {
518 let marker = " ... [truncated] ... ";
519 let (out, truncated) = head_tail_truncate("abcdefghij", 5, marker);
522 assert!(truncated);
523 assert_eq!(out, "abcde");
524
525 let long_text = "abcdefghijklmnopqrstuvwxyz";
528 let (out2, truncated2) = head_tail_truncate(long_text, 17, marker);
529 assert!(truncated2);
530 assert_eq!(out2, "abcde [truncated]");
531 assert_eq!(out2.chars().count(), 17);
532 }
533
534 #[test]
535 fn truncate_text_counts_chars_not_bytes() {
536 let jp = "あいうえお"; assert_eq!(truncate_text(jp, 3, "…"), "あいう…");
538 assert_eq!(truncate_text(jp, 5, "…"), "あいうえお");
539 }
540
541 #[test]
542 fn truncate_middle_keeps_both_ends() {
543 assert_eq!(truncate_middle("short", 80), "short");
544 assert_eq!(truncate_middle("abcdefghij", 5), "ab…ij");
545 assert_eq!(truncate_middle("a b c", 80), "a b c");
546 assert_eq!(truncate_middle("abc", 0), "");
548 assert_eq!(truncate_middle("abc", 1), "…");
549 assert_eq!(truncate_middle("a\nb\tc", 80), "a b c");
551 }
552
553 #[test]
554 fn truncate_path_middle_breaks_at_separator() {
555 assert_eq!(truncate_path_middle("src/lib.rs", 80), "src/lib.rs");
556 assert_eq!(truncate_path_middle("foo/bar/baz/qux", 12), "foo…/qux");
557 assert_eq!(truncate_path_middle("abc", 0), "");
558 }
559}