Skip to content

Commit 2962ce8

Browse files
authored
Support dump diff in --check mode (#32)
Originally, when a difference existed between the original and formatted file under --check mode, the resulting diff was not shown to the user. This patch enables dumping the diff to the console, providing clear visibility into what changes were made by the formatting process. Before: $ dtsfmt --check minimal.dts minimal.dts Errors found while formatting! After: $ dtsfmt --check minimal.dts minimal.dts - - + + - - - - clock-frequency = <5000000>; /* the baudrate divisor is ignored */ - }; - -reboot { value = <0x7777>; offset = <0x00>; regmap = <0x04>; compatible = "syscon-reboot"; }; + clock-frequency = <5000000>; + /* the baudrate divisor is ignored */ + }; + reboot { + value = <0x7777>; + offset = <0x00>; + regmap = <0x04>; + compatible = "syscon-reboot"; + }; - Errors found while formatting!
1 parent 69891ed commit 2962ce8

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/emitter/files.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use std::fs;
22

3+
use console::Style;
4+
use similar::{ChangeTag, TextDiff};
5+
36
use super::*;
47

58
#[derive(Debug, Default)]
@@ -14,10 +17,32 @@ impl FilesEmitter {
1417
impl Emitter for FilesEmitter {
1518
fn emit_check(
1619
&mut self,
17-
FormattedFile { filename, .. }: FormattedFile<'_>,
20+
FormattedFile {
21+
filename,
22+
original_text,
23+
formatted_text,
24+
}: FormattedFile<'_>,
1825
) -> Result<EmitterResult, io::Error> {
1926
println!("{}", filename.display());
2027

28+
let diff = TextDiff::from_lines(original_text, formatted_text);
29+
30+
for op in diff.ops() {
31+
for change in diff.iter_changes(op) {
32+
let (sign, style) = match change.tag() {
33+
ChangeTag::Delete => ("-", Style::new().red()),
34+
ChangeTag::Insert => ("+", Style::new().green()),
35+
ChangeTag::Equal => continue,
36+
};
37+
38+
print!(
39+
"{}{}",
40+
style.apply_to(sign).bold(),
41+
style.apply_to(change),
42+
);
43+
}
44+
}
45+
2146
Ok(EmitterResult::default())
2247
}
2348

0 commit comments

Comments
 (0)