From bdc17067888e94cede5c1f488dc2c379cec1c6f9 Mon Sep 17 00:00:00 2001 From: Harp404 <234041561+Harp404@users.noreply.github.com> Date: Mon, 30 Nov 2026 19:14:22 +0000 Subject: [PATCH] diff: add -T/--initial-tab Put a tab instead of a space between the line marker and the line in the normal, context and unified formats, as GNU diff does. --- src/params.rs | 6 ++++++ src/utils.rs | 23 ++++++++++++++++++++++- tests/by-util/test_diff.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/params.rs b/src/params.rs index 925b0320..7fcf1e36 100644 --- a/src/params.rs +++ b/src/params.rs @@ -50,6 +50,7 @@ pub struct Params { pub expand_tabs: bool, pub ignore_tab_expansion: bool, pub suppress_blank_empty: bool, + pub initial_tab: bool, pub tabsize: usize, pub width: usize, pub missing_file: MissingFile, @@ -103,6 +104,7 @@ impl Default for Params { expand_tabs: false, ignore_tab_expansion: false, suppress_blank_empty: false, + initial_tab: false, tabsize: 8, width: 130, missing_file: MissingFile::default(), @@ -281,6 +283,10 @@ pub fn parse_params>( params.ignore_tab_expansion = true; continue; } + if param == "-T" || param == "--initial-tab" { + params.initial_tab = true; + continue; + } if param == "--suppress-blank-empty" { params.suppress_blank_empty = true; continue; diff --git a/src/utils.rs b/src/utils.rs index 6d6d1395..60301ed2 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -167,15 +167,36 @@ impl<'a> PartialEq for ComparisonLine<'a> { /// The marker to write before a line of the normal, context or unified /// format: `prefix`, without its trailing blanks if `line` is empty and -/// `--suppress-blank-empty` is given. +/// `--suppress-blank-empty` is given, or ending in a tab instead of a space +/// with `-T` / `--initial-tab`. pub fn line_prefix<'a>(prefix: &'a [u8], line: &[u8], params: &Params) -> &'a [u8] { if params.suppress_blank_empty && line.is_empty() { prefix.trim_ascii_end() + } else if params.initial_tab { + initial_tab_prefix(prefix) } else { prefix } } +/// `prefix` with its trailing space turned into a tab, or a tab added after +/// it, as GNU diff writes it with `-T`: `< ` becomes `<\t`, the unified +/// context marker ` ` becomes `\t` and `+` becomes `+\t`. +fn initial_tab_prefix(prefix: &[u8]) -> &[u8] { + match prefix { + b"< " => b"<\t", + b"> " => b">\t", + b" " => b" \t", + b"! " => b"!\t", + b"+ " => b"+\t", + b"- " => b"-\t", + b" " => b"\t", + b"+" => b"+\t", + b"-" => b"-\t", + _ => prefix, + } +} + /// Quotes a file name for the headers of the diff output: a name containing /// spaces, double quotes, backslashes, control characters or non-ASCII bytes /// is written between double quotes with C-style escapes, other names are diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 314a0997..47651a71 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -48,6 +48,42 @@ fn no_differences_side_by_side() { .stdout_is("foo\t\t foo\nbar\t\t bar\n"); } +// -T puts a tab between the line marker and the line, as GNU diff does. +#[rstest] +#[case::normal(&["-T"], "2c2\n<\tb\n---\n>\tB\n4a5\n>\td\n")] +#[case::normal_long(&["--initial-tab"], "2c2\n<\tb\n---\n>\tB\n4a5\n>\td\n")] +#[case::unified(&["-T", "-u"], "@@ -1,4 +1,5 @@\n\ta\n-\tb\n+\tB\n\t\n\tc\tx\n+\td\n")] +#[case::unified_suppress_blank_empty( + &["-T", "-u", "--suppress-blank-empty"], + "@@ -1,4 +1,5 @@\n\ta\n-\tb\n+\tB\n\n\tc\tx\n+\td\n" +)] +#[case::unified_expand_tabs(&["-T", "-t", "-u"], "@@ -1,4 +1,5 @@\n\ta\n-\tb\n+\tB\n\t\n\tc x\n+\td\n")] +#[case::context( + &["-T", "-c"], + "***************\n*** 1,4 ****\n \ta\n!\tb\n \t\n \tc\tx\n--- 1,5 ----\n \ta\n!\tB\n \t\n \tc\tx\n+\td\n" +)] +#[case::ed(&["-T", "-e"], "4a\nd\n.\n2c\nB\n.\n")] +fn initial_tab(#[case] args: &[&str], #[case] expected: &str) { + let (at, ts) = at_and_ts!(); + at.write("file1", "a\nb\n\nc\tx\n"); + at.write("file2", "a\nB\n\nc\tx\nd\n"); + let result = ts + .ucmd() + .args(args) + .args(&["file1", "file2"]) + .fails_with_code(1); + // drop the file headers of the unified and context formats + let stdout = result.stdout_str(); + let body = if args.contains(&"-u") { + stdout.splitn(3, '\n').nth(2).unwrap() + } else if args.contains(&"-c") { + stdout.splitn(3, '\n').nth(2).unwrap() + } else { + stdout + }; + assert_eq!(body, expected); +} + #[rstest] fn differences(#[values(&[][..], &["-u"], &["-c"], &["-e"])] format: &[&str]) { let (at, ts) = at_and_ts!();