lean_ctx/core/patterns/
gh.rs1macro_rules! static_regex {
2 ($pattern:expr) => {{
3 static RE: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
4 RE.get_or_init(|| {
5 regex::Regex::new($pattern).expect(concat!("BUG: invalid static regex: ", $pattern))
6 })
7 }};
8}
9
10fn pr_line_re() -> &'static regex::Regex {
11 static_regex!(r"#(\d+)\s+(.+?)\s{2,}(\S+)\s+(\S+)")
12}
13fn issue_line_re() -> &'static regex::Regex {
14 static_regex!(r"#(\d+)\s+(.+?)\s{2,}")
15}
16fn pr_created_re() -> &'static regex::Regex {
17 static_regex!(r"https://github\.com/\S+/pull/(\d+)")
18}
19
20pub fn compress(command: &str, output: &str) -> Option<String> {
21 if command.contains("pr") {
22 if command.contains("diff") {
23 return None;
24 }
25 if command.contains("list") {
26 return Some(compress_pr_list(output));
27 }
28 if command.contains("view") {
29 return Some(compress_pr_view(output));
30 }
31 if command.contains("create") {
32 return Some(compress_pr_create(output));
33 }
34 if command.contains("merge") {
35 return Some(compress_simple_action(output, "merged"));
36 }
37 if command.contains("close") {
38 return Some(compress_simple_action(output, "closed"));
39 }
40 if command.contains("checkout") || command.contains("co") {
41 return Some(compress_simple_action(output, "checked out"));
42 }
43 }
44 if command.contains("issue") {
45 if command.contains("list") {
46 return Some(compress_issue_list(output));
47 }
48 if command.contains("view") {
49 return Some(compress_issue_view(output));
50 }
51 if command.contains("create") {
52 return Some(compress_simple_action(output, "created"));
53 }
54 }
55 if command.contains("run") {
56 if command.contains("list") {
57 return Some(compress_run_list(output));
58 }
59 if command.contains("view") {
60 return Some(compress_run_view(output));
61 }
62 }
63 if command.contains("repo") {
64 return Some(compress_repo(output));
65 }
66 if command.contains("release") {
67 return Some(compress_release(output));
68 }
69
70 None
71}
72
73fn compress_pr_list(output: &str) -> String {
74 let trimmed = output.trim();
75 if trimmed.is_empty() || trimmed.contains("no pull requests") {
76 return "no PRs".to_string();
77 }
78
79 let mut prs = Vec::new();
80 for line in trimmed.lines() {
81 if let Some(caps) = pr_line_re().captures(line) {
82 let num = &caps[1];
83 let title = caps[2].trim();
84 let branch = &caps[3];
85 prs.push(format!("#{num} {title} ({branch})"));
86 } else {
87 let l = line.trim();
88 if !l.is_empty() && l.starts_with('#') {
89 prs.push(l.to_string());
90 }
91 }
92 }
93
94 if prs.is_empty() {
95 return compact_output(trimmed, 10);
96 }
97 format!("{} PRs:\n{}", prs.len(), prs.join("\n"))
98}
99
100fn compress_pr_view(output: &str) -> String {
101 let lines: Vec<&str> = output.lines().collect();
102 if lines.len() <= 5 {
103 return output.to_string();
104 }
105
106 let mut title = String::new();
107 let mut state = String::new();
108 let mut labels = Vec::new();
109 let mut checks = Vec::new();
110
111 for line in &lines {
112 let l = line.trim();
113 if l.starts_with("title:") || (title.is_empty() && l.starts_with('#')) {
114 title = l.replace("title:", "").replace('#', "").trim().to_string();
115 }
116 if l.starts_with("state:") {
117 state = l.replace("state:", "").trim().to_string();
118 }
119 if l.starts_with("labels:") {
120 labels = l
121 .replace("labels:", "")
122 .split(',')
123 .map(|s| s.trim().to_string())
124 .collect();
125 }
126 if l.contains("✓") || l.contains("✗") || l.contains("pass") || l.contains("fail") {
127 checks.push(l.to_string());
128 }
129 }
130
131 let mut parts = Vec::new();
132 if !title.is_empty() {
133 parts.push(title);
134 }
135 if !state.is_empty() {
136 parts.push(format!("state: {state}"));
137 }
138 if !labels.is_empty() {
139 parts.push(format!("labels: {}", labels.join(", ")));
140 }
141 if !checks.is_empty() && checks.len() <= 5 {
142 parts.push(format!("checks: {}", checks.join("; ")));
143 }
144
145 if parts.is_empty() {
146 return compact_output(output, 10);
147 }
148 parts.join("\n")
149}
150
151fn compress_pr_create(output: &str) -> String {
152 if let Some(caps) = pr_created_re().captures(output) {
153 return format!("#{} created", &caps[1]);
154 }
155 let trimmed = output.trim();
156 if trimmed.contains("http") {
157 for line in trimmed.lines() {
158 if line.contains("http") {
159 return format!("created: {}", line.trim());
160 }
161 }
162 }
163 compact_output(trimmed, 3)
164}
165
166fn compress_issue_list(output: &str) -> String {
167 let trimmed = output.trim();
168 if trimmed.is_empty() || trimmed.contains("no issues") {
169 return "no issues".to_string();
170 }
171
172 let mut issues = Vec::new();
173 for line in trimmed.lines() {
174 if let Some(caps) = issue_line_re().captures(line) {
175 let num = &caps[1];
176 let title = caps[2].trim();
177 issues.push(format!("#{num} {title}"));
178 } else {
179 let l = line.trim();
180 if !l.is_empty() && l.starts_with('#') {
181 issues.push(l.to_string());
182 }
183 }
184 }
185
186 if issues.is_empty() {
187 return compact_output(trimmed, 10);
188 }
189 format!("{} issues:\n{}", issues.len(), issues.join("\n"))
190}
191
192fn compress_issue_view(output: &str) -> String {
193 compact_output(output, 15)
194}
195
196fn compress_run_list(output: &str) -> String {
197 let trimmed = output.trim();
198 if trimmed.is_empty() {
199 return "no runs".to_string();
200 }
201
202 let mut runs = Vec::new();
203 for line in trimmed.lines() {
204 let l = line.trim();
205 if l.is_empty() || l.starts_with("STATUS") || l.starts_with("--") {
206 continue;
207 }
208 if l.contains("completed")
209 || l.contains("in_progress")
210 || l.contains("queued")
211 || l.contains("failure")
212 || l.contains("success")
213 {
214 runs.push(l.to_string());
215 }
216 }
217
218 if runs.is_empty() {
219 return compact_output(trimmed, 10);
220 }
221 format!("{} runs:\n{}", runs.len(), runs.join("\n"))
222}
223
224fn compress_run_view(output: &str) -> String {
225 compact_output(output, 15)
226}
227
228fn compress_repo(output: &str) -> String {
229 compact_output(output, 10)
230}
231
232fn compress_release(output: &str) -> String {
233 compact_output(output, 10)
234}
235
236fn compress_simple_action(output: &str, action: &str) -> String {
237 let trimmed = output.trim();
238 if trimmed.is_empty() {
239 return format!("ok ({action})");
240 }
241 for line in trimmed.lines() {
242 if line.contains("http") || line.contains('#') {
243 return format!("{action}: {}", line.trim());
244 }
245 }
246 action.to_string()
247}
248
249fn compact_output(text: &str, max: usize) -> String {
250 let lines: Vec<&str> = text.lines().filter(|l| !l.trim().is_empty()).collect();
251 if lines.len() <= max {
252 return lines.join("\n");
253 }
254 format!(
255 "{}\n... ({} more lines)",
256 lines[..max].join("\n"),
257 lines.len() - max
258 )
259}