1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use colored::Colorize;
use sem_core::model::change::ChangeType;
use sem_core::parser::differ::DiffResult;
use similar::{ChangeTag, TextDiff};
use std::collections::BTreeMap;
/// Runs word-level diff on two lines and returns (delete_line, insert_line)
/// with changed words highlighted (strikethrough+red / bold+green).
fn render_inline_diff(old_line: &str, new_line: &str) -> (String, String) {
let diff = TextDiff::from_words(old_line, new_line);
let mut del = String::new();
let mut ins = String::new();
for change in diff.iter_all_changes() {
let val = change.value();
match change.tag() {
ChangeTag::Equal => {
del.push_str(&val.dimmed().to_string());
ins.push_str(&val.dimmed().to_string());
}
ChangeTag::Delete => {
del.push_str(&val.red().strikethrough().bold().to_string());
}
ChangeTag::Insert => {
ins.push_str(&val.green().bold().to_string());
}
}
}
(del, ins)
}
pub fn format_terminal(result: &DiffResult, verbose: bool) -> String {
if result.changes.is_empty() {
return "No semantic changes detected.".dimmed().to_string();
}
let mut lines: Vec<String> = Vec::new();
// Group changes by file (BTreeMap for sorted output)
let mut by_file: BTreeMap<&str, Vec<usize>> = BTreeMap::new();
for (i, change) in result.changes.iter().enumerate() {
by_file.entry(&change.file_path).or_default().push(i);
}
for (file_path, indices) in &by_file {
// Skip files where all changes are orphans in non-verbose mode
if !verbose
&& indices
.iter()
.all(|&i| result.changes[i].entity_type == "orphan")
{
continue;
}
let header = format!("─ {file_path} ");
let pad_len = 55usize.saturating_sub(header.len());
lines.push(format!("┌{header}{}", "─".repeat(pad_len)).dimmed().to_string());
lines.push("│".dimmed().to_string());
for &idx in indices {
let change = &result.changes[idx];
// Orphan changes (module-level) only shown in verbose mode
if change.entity_type == "orphan" && !verbose {
continue;
}
let (symbol, tag) = match change.change_type {
ChangeType::Added => (
"⊕".green().to_string(),
"[added]".green().to_string(),
),
ChangeType::Modified => {
let is_cosmetic = change.structural_change == Some(false);
if is_cosmetic {
(
"~".dimmed().to_string(),
"[cosmetic]".dimmed().to_string(),
)
} else {
(
"∆".yellow().to_string(),
"[modified]".yellow().to_string(),
)
}
}
ChangeType::Deleted => (
"⊖".red().to_string(),
"[deleted]".red().to_string(),
),
ChangeType::Moved => (
"→".blue().to_string(),
"[moved]".blue().to_string(),
),
ChangeType::Renamed => (
"↻".cyan().to_string(),
"[renamed]".cyan().to_string(),
),
ChangeType::Reordered => (
"↕".magenta().to_string(),
"[reordered]".magenta().to_string(),
),
};
let type_label = format!("{:<10}", change.entity_type);
let base_name = if let Some(ref old_name) = change.old_entity_name {
format!("{old_name} -> {}", change.entity_name)
} else {
change.entity_name.clone()
};
let display_name = match &change.parent_name {
Some(p) => format!("{p}::{base_name}"),
None => base_name,
};
let truncated = if display_name.len() > 25 {
format!("{}…", display_name.char_indices().nth(24).map(|(i, _)| &display_name[..i]).unwrap_or(&display_name))
} else {
display_name
};
let name_label = format!("{:<25}", truncated);
lines.push(format!(
"{} {} {} {} {}",
"│".dimmed(),
symbol,
type_label.dimmed(),
name_label.bold(),
tag,
));
// Show content diff
if verbose {
match change.change_type {
ChangeType::Added => {
if let Some(ref content) = change.after_content {
for line in content.lines() {
lines.push(format!(
"{} {}",
"│".dimmed(),
format!("+ {line}").green(),
));
}
}
}
ChangeType::Deleted => {
if let Some(ref content) = change.before_content {
for line in content.lines() {
lines.push(format!(
"{} {}",
"│".dimmed(),
format!("- {line}").red(),
));
}
}
}
ChangeType::Modified | ChangeType::Renamed | ChangeType::Moved => {
if let (Some(before), Some(after)) =
(&change.before_content, &change.after_content)
{
let diff = TextDiff::from_lines(before.as_str(), after.as_str());
for hunk in diff.unified_diff().context_radius(2).iter_hunks() {
lines.push(format!(
"{} {}",
"│".dimmed(),
format!("{}", hunk.header()).dimmed(),
));
for op in hunk.ops() {
let mut deletes: Vec<String> = Vec::new();
let mut inserts: Vec<String> = Vec::new();
for diff_change in diff.iter_changes(op) {
let line = diff_change.value().trim_end_matches('\n');
match diff_change.tag() {
ChangeTag::Delete => deletes.push(line.to_string()),
ChangeTag::Insert => inserts.push(line.to_string()),
ChangeTag::Equal => {
lines.push(format!(
"{} {}",
"│".dimmed(),
format!(" {line}").dimmed(),
));
}
}
}
let paired = deletes.len().min(inserts.len());
for i in 0..paired {
let (del, ins) =
render_inline_diff(&deletes[i], &inserts[i]);
lines.push(format!(
"{} {} {}",
"│".dimmed(),
"-".red(),
del,
));
lines.push(format!(
"{} {} {}",
"│".dimmed(),
"+".green(),
ins,
));
}
for d in &deletes[paired..] {
lines.push(format!(
"{} {}",
"│".dimmed(),
format!("- {d}").red(),
));
}
for i in &inserts[paired..] {
lines.push(format!(
"{} {}",
"│".dimmed(),
format!("+ {i}").green(),
));
}
}
}
}
}
_ => {}
}
} else if change.change_type == ChangeType::Modified {
if let (Some(before), Some(after)) =
(&change.before_content, &change.after_content)
{
let before_lines: Vec<&str> = before.lines().collect();
let after_lines: Vec<&str> = after.lines().collect();
if before_lines.len() <= 3 && after_lines.len() <= 3 {
for line in &before_lines {
lines.push(format!(
"{} {}",
"│".dimmed(),
format!("- {}", line.trim()).red(),
));
}
for line in &after_lines {
lines.push(format!(
"{} {}",
"│".dimmed(),
format!("+ {}", line.trim()).green(),
));
}
}
}
}
// Show rename/move details
if matches!(
change.change_type,
ChangeType::Renamed | ChangeType::Moved
) {
if let Some(ref old_path) = change.old_file_path {
lines.push(format!(
"{} {}",
"│".dimmed(),
format!("from {old_path}").dimmed(),
));
} else if let Some(ref old_parent) = change.old_parent_id {
// Intra-file move: extract parent name from entity ID
let parent_name = old_parent.rsplit("::").next().unwrap_or(old_parent);
lines.push(format!(
"{} {}",
"│".dimmed(),
format!("moved from {parent_name}").dimmed(),
));
}
}
}
lines.push("│".dimmed().to_string());
lines.push(format!("└{}", "─".repeat(55)).dimmed().to_string());
lines.push(String::new());
}
// Summary
let mut parts: Vec<String> = Vec::new();
if result.added_count > 0 {
parts.push(format!("{} added", result.added_count).green().to_string());
}
if result.modified_count > 0 {
parts.push(
format!("{} modified", result.modified_count)
.yellow()
.to_string(),
);
}
if result.deleted_count > 0 {
parts.push(format!("{} deleted", result.deleted_count).red().to_string());
}
if result.moved_count > 0 {
parts.push(format!("{} moved", result.moved_count).blue().to_string());
}
if result.renamed_count > 0 {
parts.push(
format!("{} renamed", result.renamed_count)
.cyan()
.to_string(),
);
}
if result.reordered_count > 0 {
parts.push(
format!("{} reordered", result.reordered_count)
.magenta()
.to_string(),
);
}
let files_label = if result.file_count == 1 {
"file"
} else {
"files"
};
lines.push(format!(
"Summary: {} across {} {files_label}",
parts.join(", "),
result.file_count,
));
// Warn if fallback chunking was used (unsupported file extension)
let chunk_files: Vec<&str> = result
.changes
.iter()
.filter(|c| c.entity_type == "chunk")
.map(|c| c.file_path.as_str())
.collect::<std::collections::BTreeSet<_>>()
.into_iter()
.collect();
if !chunk_files.is_empty() {
lines.push(String::new());
lines.push(
format!(
"Warning: {} used line-based chunking (unsupported file extension).",
chunk_files.join(", ")
)
.yellow()
.to_string(),
);
lines.push(
"If this language should be supported, open an issue: https://github.com/Ataraxy-Labs/sem/issues"
.dimmed()
.to_string(),
);
}
lines.join("\n")
}