pixie_pi/tools/
truncate.rs1use serde::{Deserialize, Serialize};
8
9pub const DEFAULT_MAX_LINES: usize = 2000;
11pub const DEFAULT_MAX_BYTES: usize = 50 * 1024;
13pub const GREP_MAX_LINE_LENGTH: usize = 500;
15
16#[derive(Debug, Clone, Default, Serialize, Deserialize)]
17pub struct TruncationResult {
18 pub content: String,
19 pub truncated: bool,
20 pub truncated_by: Option<String>, pub output_lines: usize,
22 pub total_lines: usize,
23 pub max_lines: Option<usize>,
24 pub max_bytes: Option<usize>,
25 pub first_line_exceeds_limit: bool,
26 pub output_bytes: usize,
27 pub last_line_partial: bool,
28}
29
30pub fn truncate_head(content: &str, max_lines: Option<usize>, max_bytes: Option<usize>) -> TruncationResult {
33 let max_lines = max_lines.unwrap_or(DEFAULT_MAX_LINES);
34 let max_bytes = max_bytes.unwrap_or(DEFAULT_MAX_BYTES);
35 let mut lines: Vec<&str> = content.split('\n').collect();
36 if content.ends_with('\n') {
42 lines.pop();
43 }
44 let total_lines = lines.len();
45
46 if !lines.is_empty() {
47 let first_line_bytes = lines[0].len();
48 if first_line_bytes > max_bytes {
49 return TruncationResult {
50 first_line_exceeds_limit: true,
51 total_lines,
52 max_bytes: Some(max_bytes),
53 max_lines: Some(max_lines),
54 ..Default::default()
55 };
56 }
57 }
58
59 let mut out: Vec<&str> = Vec::new();
60 let mut bytes = 0usize;
61 let mut truncated_by: Option<String> = None;
62
63 for line in &lines {
64 let line_bytes = line.len() + 1; if out.len() + 1 > max_lines {
66 truncated_by = Some("lines".into());
67 break;
68 }
69 if bytes + line_bytes > max_bytes {
70 truncated_by = Some("bytes".into());
71 break;
72 }
73 bytes += line_bytes;
74 out.push(line);
75 }
76
77 let truncated = truncated_by.is_some();
78 let content = out.join("\n");
79 TruncationResult {
80 output_bytes: content.len(),
81 content,
82 truncated,
83 truncated_by,
84 output_lines: out.len(),
85 total_lines,
86 max_lines: Some(max_lines),
87 max_bytes: Some(max_bytes),
88 first_line_exceeds_limit: false,
89 last_line_partial: false,
90 }
91}
92
93pub fn truncate_tail(content: &str, max_lines: Option<usize>, max_bytes: Option<usize>) -> TruncationResult {
95 let max_lines = max_lines.unwrap_or(DEFAULT_MAX_LINES);
96 let max_bytes = max_bytes.unwrap_or(DEFAULT_MAX_BYTES);
97 let mut lines: Vec<&str> = content.split('\n').collect();
98 if content.ends_with('\n') {
104 lines.pop();
105 }
106 let total_lines = lines.len();
107
108 let start = total_lines.saturating_sub(max_lines);
110 let mut kept: Vec<&str> = lines[start..].to_vec();
111 let mut truncated_by = if start > 0 { Some("lines".into()) } else { None };
112
113 let total_bytes: usize = kept.iter().map(|l| l.len() + 1).sum();
115 if total_bytes > max_bytes {
116 let mut bytes = 0usize;
117 let mut cut_from = 0;
118 for (i, line) in kept.iter().enumerate().rev() {
119 bytes += line.len() + 1;
120 if bytes > max_bytes {
121 cut_from = i + 1;
122 truncated_by = Some("bytes".into());
123 break;
124 }
125 }
126 if cut_from >= kept.len() {
127 cut_from = kept.len().saturating_sub(1);
128 }
129 kept = kept[cut_from..].to_vec();
130 }
131
132 let truncated = truncated_by.is_some();
133 let content = kept.join("\n");
134 let output_lines = kept.len();
135 TruncationResult {
136 output_bytes: content.len(),
137 content,
138 truncated,
139 truncated_by,
140 output_lines,
141 total_lines,
142 max_lines: Some(max_lines),
143 max_bytes: Some(max_bytes),
144 first_line_exceeds_limit: false,
145 last_line_partial: false,
146 }
147}
148
149pub fn truncate_line(line: &str, max: Option<usize>) -> (String, bool) {
151 let max = max.unwrap_or(GREP_MAX_LINE_LENGTH);
152 let chars: Vec<char> = line.chars().collect();
154 if chars.len() <= max {
155 return (line.to_string(), false);
156 }
157 let truncated: String = chars[..max].iter().collect();
158 (format!("{truncated}…"), true)
159}
160
161pub fn format_size(bytes: usize) -> String {
163 const KB: f64 = 1024.0;
164 const MB: f64 = KB * 1024.0;
165 let b = bytes as f64;
166 if b >= MB {
167 format!("{:.1}MB", b / MB)
168 } else if b >= KB {
169 format!("{:.1}KB", b / KB)
170 } else {
171 format!("{}B", bytes)
172 }
173}
174
175#[cfg(test)]
176mod tests {
177 use super::*;
178
179 #[test]
180 fn head_truncates_by_lines() {
181 let content = "a\nb\nc\nd\ne";
182 let r = truncate_head(content, Some(2), Some(1024));
183 assert_eq!(r.content, "a\nb");
184 assert!(r.truncated);
185 assert_eq!(r.truncated_by.as_deref(), Some("lines"));
186 }
187
188 #[test]
189 fn tail_keeps_recent_lines() {
190 let content = "a\nb\nc\nd\ne";
191 let r = truncate_tail(content, Some(2), Some(1024));
192 assert_eq!(r.content, "d\ne");
193 assert!(r.truncated);
194 }
195
196 #[test]
197 fn line_truncation_is_char_safe() {
198 let s = "日本語".repeat(1000);
199 let (t, truncated) = truncate_line(&s, Some(10));
200 assert!(truncated);
201 assert!(t.chars().count() <= 11);
202 }
203
204 #[test]
205 fn head_does_not_inflate_line_count_for_trailing_newline() {
206 let r = truncate_head("a\nb\nc\n", Some(2000), Some(1024));
209 assert_eq!(r.total_lines, 3);
210 assert_eq!(r.output_lines, 3);
211 assert!(!r.truncated);
212 }
213
214 #[test]
215 fn tail_does_not_inflate_line_count_for_trailing_newline() {
216 let r = truncate_tail("a\nb\nc\n", Some(2000), Some(1024));
220 assert_eq!(r.total_lines, 3);
221 assert_eq!(r.output_lines, 3);
222 assert!(!r.truncated);
223 }
224
225 #[test]
226 fn tail_truncation_reports_an_exact_line_range() {
227 let r = truncate_tail("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n", Some(3), Some(1024));
231 assert_eq!(r.total_lines, 10);
232 assert_eq!(r.output_lines, 3);
233 assert_eq!(r.content, "8\n9\n10");
234 assert!(r.truncated);
235 }
236}