Skip to content

Commit 8fc1f81

Browse files
committed
Implement --tabsize option
1 parent 58785da commit 8fc1f81

File tree

9 files changed

+182
-33
lines changed

9 files changed

+182
-33
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ path = "src/main.rs"
1616

1717
[dependencies]
1818
diff = "0.1.10"
19+
regex = "1.10.3"
1920
same-file = "1.0.6"
2021
unicode-width = "0.1.11"
2122

src/context_diff.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ pub fn diff(
263263
actual_filename: &str,
264264
context_size: usize,
265265
expand_tabs: bool,
266+
tabsize: usize,
266267
) -> Vec<u8> {
267268
let mut output = format!("*** {expected_filename}\t\n--- {actual_filename}\t\n").into_bytes();
268269
let diff_results = make_diff(expected, actual, context_size);
@@ -304,19 +305,19 @@ pub fn diff(
304305
match line {
305306
DiffLine::Context(e) => {
306307
write!(output, " ").expect("write to Vec is infallible");
307-
do_write_line(&mut output, &e, expand_tabs)
308+
do_write_line(&mut output, &e, expand_tabs, tabsize)
308309
.expect("write to Vec is infallible");
309310
writeln!(output).unwrap();
310311
}
311312
DiffLine::Change(e) => {
312313
write!(output, "! ").expect("write to Vec is infallible");
313-
do_write_line(&mut output, &e, expand_tabs)
314+
do_write_line(&mut output, &e, expand_tabs, tabsize)
314315
.expect("write to Vec is infallible");
315316
writeln!(output).unwrap();
316317
}
317318
DiffLine::Add(e) => {
318319
write!(output, "- ").expect("write to Vec is infallible");
319-
do_write_line(&mut output, &e, expand_tabs)
320+
do_write_line(&mut output, &e, expand_tabs, tabsize)
320321
.expect("write to Vec is infallible");
321322
writeln!(output).unwrap();
322323
}
@@ -334,19 +335,19 @@ pub fn diff(
334335
match line {
335336
DiffLine::Context(e) => {
336337
write!(output, " ").expect("write to Vec is infallible");
337-
do_write_line(&mut output, &e, expand_tabs)
338+
do_write_line(&mut output, &e, expand_tabs, tabsize)
338339
.expect("write to Vec is infallible");
339340
writeln!(output).unwrap();
340341
}
341342
DiffLine::Change(e) => {
342343
write!(output, "! ").expect("write to Vec is infallible");
343-
do_write_line(&mut output, &e, expand_tabs)
344+
do_write_line(&mut output, &e, expand_tabs, tabsize)
344345
.expect("write to Vec is infallible");
345346
writeln!(output).unwrap();
346347
}
347348
DiffLine::Add(e) => {
348349
write!(output, "+ ").expect("write to Vec is infallible");
349-
do_write_line(&mut output, &e, expand_tabs)
350+
do_write_line(&mut output, &e, expand_tabs, tabsize)
350351
.expect("write to Vec is infallible");
351352
writeln!(output).unwrap();
352353
}
@@ -420,6 +421,7 @@ mod tests {
420421
&format!("{target}/alef"),
421422
2,
422423
false,
424+
8,
423425
);
424426
File::create(&format!("{target}/ab.diff"))
425427
.unwrap()
@@ -499,6 +501,7 @@ mod tests {
499501
&format!("{target}/alef_"),
500502
2,
501503
false,
504+
8,
502505
);
503506
File::create(&format!("{target}/ab_.diff"))
504507
.unwrap()
@@ -581,6 +584,7 @@ mod tests {
581584
&format!("{target}/alefx"),
582585
2,
583586
false,
587+
8,
584588
);
585589
File::create(&format!("{target}/abx.diff"))
586590
.unwrap()
@@ -666,6 +670,7 @@ mod tests {
666670
&format!("{target}/alefr"),
667671
2,
668672
false,
673+
8,
669674
);
670675
File::create(&format!("{target}/abr.diff"))
671676
.unwrap()

src/ed_diff.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ fn make_diff(expected: &[u8], actual: &[u8]) -> Result<Vec<Mismatch>, DiffError>
105105
Ok(results)
106106
}
107107

108-
pub fn diff(expected: &[u8], actual: &[u8], expand_tabs: bool) -> Result<Vec<u8>, DiffError> {
108+
pub fn diff(
109+
expected: &[u8],
110+
actual: &[u8],
111+
expand_tabs: bool,
112+
tabsize: usize,
113+
) -> Result<Vec<u8>, DiffError> {
109114
let mut output = Vec::new();
110115
let diff_results = make_diff(expected, actual)?;
111116
let mut lines_offset = 0;
@@ -139,7 +144,7 @@ pub fn diff(expected: &[u8], actual: &[u8], expand_tabs: bool) -> Result<Vec<u8>
139144
if actual == b"." {
140145
writeln!(&mut output, "..\n.\ns/.//\na").unwrap();
141146
} else {
142-
do_write_line(&mut output, actual, expand_tabs).unwrap();
147+
do_write_line(&mut output, actual, expand_tabs, tabsize).unwrap();
143148
writeln!(&mut output).unwrap();
144149
}
145150
}
@@ -154,7 +159,7 @@ mod tests {
154159
use super::*;
155160
use pretty_assertions::assert_eq;
156161
pub fn diff_w(expected: &[u8], actual: &[u8], filename: &str) -> Result<Vec<u8>, DiffError> {
157-
let mut output = diff(expected, actual, false)?;
162+
let mut output = diff(expected, actual, false, 8)?;
158163
writeln!(&mut output, "w {filename}").unwrap();
159164
Ok(output)
160165
}
@@ -163,7 +168,7 @@ mod tests {
163168
fn test_basic() {
164169
let from = b"a\n";
165170
let to = b"b\n";
166-
let diff = diff(from, to, false).unwrap();
171+
let diff = diff(from, to, false, 8).unwrap();
167172
let expected = vec!["1c", "b", ".", ""].join("\n");
168173
assert_eq!(diff, expected.as_bytes());
169174
}

src/main.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ fn main() -> ExitCode {
3131
format,
3232
report_identical_files,
3333
expand_tabs,
34+
tabsize,
3435
} = parse_params(opts).unwrap_or_else(|error| {
3536
eprintln!("{error}");
3637
exit(2);
@@ -66,14 +67,15 @@ fn main() -> ExitCode {
6667
};
6768
// run diff
6869
let result: Vec<u8> = match format {
69-
Format::Normal => normal_diff::diff(&from_content, &to_content, expand_tabs),
70+
Format::Normal => normal_diff::diff(&from_content, &to_content, expand_tabs, tabsize),
7071
Format::Unified => unified_diff::diff(
7172
&from_content,
7273
&from.to_string_lossy(),
7374
&to_content,
7475
&to.to_string_lossy(),
7576
context_count,
7677
expand_tabs,
78+
tabsize,
7779
),
7880
Format::Context => context_diff::diff(
7981
&from_content,
@@ -82,13 +84,13 @@ fn main() -> ExitCode {
8284
&to.to_string_lossy(),
8385
context_count,
8486
expand_tabs,
87+
tabsize,
8588
),
86-
Format::Ed => {
87-
ed_diff::diff(&from_content, &to_content, expand_tabs).unwrap_or_else(|error| {
89+
Format::Ed => ed_diff::diff(&from_content, &to_content, expand_tabs, tabsize)
90+
.unwrap_or_else(|error| {
8891
eprintln!("{error}");
8992
exit(2);
90-
})
91-
}
93+
}),
9294
};
9395
io::stdout().write_all(&result).unwrap();
9496
if result.is_empty() {

src/normal_diff.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn make_diff(expected: &[u8], actual: &[u8]) -> Vec<Mismatch> {
112112
}
113113

114114
#[must_use]
115-
pub fn diff(expected: &[u8], actual: &[u8], expand_tabs: bool) -> Vec<u8> {
115+
pub fn diff(expected: &[u8], actual: &[u8], expand_tabs: bool, tabsize: usize) -> Vec<u8> {
116116
// See https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Normal.html
117117
// for details on the syntax of the normal format.
118118
let mut output = Vec::new();
@@ -182,7 +182,7 @@ pub fn diff(expected: &[u8], actual: &[u8], expand_tabs: bool) -> Vec<u8> {
182182
}
183183
for expected in &result.expected {
184184
write!(&mut output, "< ").unwrap();
185-
do_write_line(&mut output, expected, expand_tabs).unwrap();
185+
do_write_line(&mut output, expected, expand_tabs, tabsize).unwrap();
186186
writeln!(&mut output).unwrap();
187187
}
188188
if result.expected_missing_nl {
@@ -193,7 +193,7 @@ pub fn diff(expected: &[u8], actual: &[u8], expand_tabs: bool) -> Vec<u8> {
193193
}
194194
for actual in &result.actual {
195195
write!(&mut output, "> ").unwrap();
196-
do_write_line(&mut output, actual, expand_tabs).unwrap();
196+
do_write_line(&mut output, actual, expand_tabs, tabsize).unwrap();
197197
writeln!(&mut output).unwrap();
198198
}
199199
if result.actual_missing_nl {
@@ -214,7 +214,7 @@ mod tests {
214214
a.write_all(b"a\n").unwrap();
215215
let mut b = Vec::new();
216216
b.write_all(b"b\n").unwrap();
217-
let diff = diff(&a, &b, false);
217+
let diff = diff(&a, &b, false, 8);
218218
let expected = b"1c1\n< a\n---\n> b\n".to_vec();
219219
assert_eq!(diff, expected);
220220
}
@@ -267,7 +267,7 @@ mod tests {
267267
}
268268
// This test diff is intentionally reversed.
269269
// We want it to turn the alef into bet.
270-
let diff = diff(&alef, &bet, false);
270+
let diff = diff(&alef, &bet, false, 8);
271271
File::create(&format!("{target}/ab.diff"))
272272
.unwrap()
273273
.write_all(&diff)
@@ -359,7 +359,7 @@ mod tests {
359359
}
360360
// This test diff is intentionally reversed.
361361
// We want it to turn the alef into bet.
362-
let diff = diff(&alef, &bet, false);
362+
let diff = diff(&alef, &bet, false, 8);
363363
File::create(&format!("{target}/abn.diff"))
364364
.unwrap()
365365
.write_all(&diff)
@@ -433,7 +433,7 @@ mod tests {
433433
}
434434
// This test diff is intentionally reversed.
435435
// We want it to turn the alef into bet.
436-
let diff = diff(&alef, &bet, false);
436+
let diff = diff(&alef, &bet, false, 8);
437437
File::create(&format!("{target}/ab_.diff"))
438438
.unwrap()
439439
.write_all(&diff)
@@ -511,7 +511,7 @@ mod tests {
511511
}
512512
// This test diff is intentionally reversed.
513513
// We want it to turn the alef into bet.
514-
let diff = diff(&alef, &bet, false);
514+
let diff = diff(&alef, &bet, false, 8);
515515
File::create(&format!("{target}/abr.diff"))
516516
.unwrap()
517517
.write_all(&diff)

0 commit comments

Comments
 (0)