Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -281,6 +283,10 @@ pub fn parse_params<I: Iterator<Item = OsString>>(
params.ignore_tab_expansion = true;
continue;
}
if param == "-T" || param == "--initial-tab" {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add -T / --initial-tab cases to the parse_params tests too, like -E

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added initial_tab_short and initial_tab_long cases next to the -E ones in f2f8416. I also merged main to fix the conflict with #444.

params.initial_tab = true;
continue;
}
if param == "--suppress-blank-empty" {
params.suppress_blank_empty = true;
continue;
Expand Down Expand Up @@ -753,6 +759,8 @@ mod tests {
&["--ignore-tab-expansion", "foo", "bar"],
Params { ignore_tab_expansion: true, ..foo_bar() },
)]
#[case::initial_tab_short(&["-T", "foo", "bar"], Params { initial_tab: true, ..foo_bar() })]
#[case::initial_tab_long(&["--initial-tab", "foo", "bar"], Params { initial_tab: true, ..foo_bar() })]
#[case::tabsize_separate(&["--tabsize", "42", "foo", "bar"], Params { tabsize: 42, ..foo_bar() })]
#[case::tabsize_equals(&["--tabsize=42", "foo", "bar"], Params { tabsize: 42, ..foo_bar() })]
#[case::width_short_separate(&["-W", "100", "foo", "bar"], Params { width: 100, ..foo_bar() })]
Expand Down
23 changes: 22 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions tests/by-util/test_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,40 @@ fn stdin_twice_normal_prints_nothing() {
.no_stdout();
}

// -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") || 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!();
Expand Down
Loading