Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Center Align numbers right-ish #883

Merged
merged 1 commit into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ To display line numbers, use --line-numbers.

Line numbers are displayed in two columns. Here's what it looks like by default:

1 ⋮ 1 │ unchanged line
2 ⋮ │ removed line
2 │ added line
1 ⋮ 1 │ unchanged line
2 ⋮ │ removed line
2 │ added line

In that output, the line numbers for the old (minus) version of the file appear in the left column,
and the line numbers for the new (plus) version of the file appear in the right column. In an
Expand Down
92 changes: 44 additions & 48 deletions src/features/line_numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,7 @@ fn format_and_paint_line_number_field<'a>(
min_field_width
};

let alignment_spec = placeholder
.alignment_spec
.as_ref()
.unwrap_or(&Align::Center);
let alignment_spec = placeholder.alignment_spec.unwrap_or(Align::Center);
match placeholder.placeholder {
Some(Placeholder::NumberMinus) => {
ansi_strings.push(styles[Minus].paint(format_line_number(
Expand Down Expand Up @@ -298,20 +295,19 @@ fn format_and_paint_line_number_field<'a>(
/// Return line number formatted according to `alignment` and `width`.
fn format_line_number(
line_number: Option<usize>,
alignment: &Align,
alignment: Align,
width: usize,
precision: Option<usize>,
plus_file: Option<&str>,
config: &config::Config,
) -> String {
let pad = |n| format::pad(n, width, alignment, precision);
match (line_number, config.hyperlinks, plus_file) {
(None, _, _) => pad(""),
(None, _, _) => " ".repeat(width),
(Some(n), true, Some(file)) => {
hyperlinks::format_osc8_file_hyperlink(file, line_number, &pad(&n.to_string()), config)
.to_string()
hyperlinks::format_osc8_file_hyperlink(file, line_number, &pad(n), config).to_string()
}
(Some(n), _, _) => pad(&n.to_string()),
(Some(n), _, _) => pad(n),
}
}

Expand Down Expand Up @@ -649,8 +645,8 @@ pub mod tests {
.expect_after_header(
r#"
#indent_mark
1 ⋮ │a = 1
2 ⋮ │b = 23456"#,
1 ⋮ │a = 1
2 ⋮ │b = 23456"#,
);
}

Expand All @@ -675,8 +671,8 @@ pub mod tests {
.expect_after_header(
r#"
#indent_mark
1 │a = 1
2 │b = 234567"#,
1 │a = 1
2 │b = 234567"#,
);
}

Expand All @@ -700,9 +696,9 @@ pub mod tests {
let output = run_delta(ONE_MINUS_ONE_PLUS_LINE_DIFF, &config);
let output = strip_ansi_codes(&output);
let mut lines = output.lines().skip(crate::config::HEADER_LEN);
assert_eq!(lines.next().unwrap(), " 1 ⋮ 1 │a = 1");
assert_eq!(lines.next().unwrap(), " 2 ⋮ │b = 2");
assert_eq!(lines.next().unwrap(), " ⋮ 2 │bb = 2");
assert_eq!(lines.next().unwrap(), " 1 ⋮ 1 │a = 1");
assert_eq!(lines.next().unwrap(), " 2 ⋮ │b = 2");
assert_eq!(lines.next().unwrap(), " ⋮ 2 │bb = 2");
}

#[test]
Expand All @@ -725,9 +721,9 @@ pub mod tests {
let output = run_delta(ONE_MINUS_ONE_PLUS_LINE_DIFF, &config);
let output = strip_ansi_codes(&output);
let mut lines = output.lines().skip(crate::config::HEADER_LEN);
assert_eq!(lines.next().unwrap(), " 1 1 ⋮ 1 │a = 1");
assert_eq!(lines.next().unwrap(), " 2 2 ⋮ │b = 2");
assert_eq!(lines.next().unwrap(), " ⋮ 2 │bb = 2");
assert_eq!(lines.next().unwrap(), " 1 1 ⋮ 1 │a = 1");
assert_eq!(lines.next().unwrap(), " 2 2 ⋮ │b = 2");
assert_eq!(lines.next().unwrap(), " ⋮ 2 │bb = 2");
}

#[test]
Expand All @@ -747,7 +743,7 @@ pub mod tests {
let output = run_delta(UNEQUAL_DIGIT_DIFF, &config);
let output = strip_ansi_codes(&output);
let mut lines = output.lines().skip(crate::config::HEADER_LEN);
assert_eq!(lines.next().unwrap(), "10000⋮9999 │a = 1");
assert_eq!(lines.next().unwrap(), "10000⋮ 9999│a = 1");
assert_eq!(lines.next().unwrap(), "10001⋮ │b = 2");
assert_eq!(lines.next().unwrap(), " ⋮10000│bb = 2");
}
Expand All @@ -758,8 +754,8 @@ pub mod tests {
let output = run_delta(TWO_MINUS_LINES_DIFF, &config);
let mut lines = output.lines().skip(5);
let (line_1, line_2) = (lines.next().unwrap(), lines.next().unwrap());
assert_eq!(strip_ansi_codes(line_1), " 1 ⋮ │-a = 1");
assert_eq!(strip_ansi_codes(line_2), " 2 ⋮ │-b = 23456");
assert_eq!(strip_ansi_codes(line_1), " 1 ⋮ │-a = 1");
assert_eq!(strip_ansi_codes(line_2), " 2 ⋮ │-b = 23456");
}

#[test]
Expand All @@ -768,13 +764,13 @@ pub mod tests {
let output = run_delta(TWO_LINE_DIFFS, &config);
let output = strip_ansi_codes(&output);
let mut lines = output.lines().skip(4);
assert_eq!(lines.next().unwrap(), " 1 ⋮ 1 │a = 1");
assert_eq!(lines.next().unwrap(), " 2 ⋮ │b = 2");
assert_eq!(lines.next().unwrap(), " ⋮ 2 │bb = 2");
assert_eq!(lines.next().unwrap(), " 1 ⋮ 1 │a = 1");
assert_eq!(lines.next().unwrap(), " 2 ⋮ │b = 2");
assert_eq!(lines.next().unwrap(), " ⋮ 2 │bb = 2");
assert_eq!(lines.next().unwrap(), "");
assert_eq!(lines.next().unwrap(), "499 ⋮499 │a = 3");
assert_eq!(lines.next().unwrap(), "500 ⋮ │b = 4");
assert_eq!(lines.next().unwrap(), " ⋮500 │bb = 4");
assert_eq!(lines.next().unwrap(), " 499⋮ 499│a = 3");
assert_eq!(lines.next().unwrap(), " 500⋮ │b = 4");
assert_eq!(lines.next().unwrap(), " ⋮ 500│bb = 4");
}

#[test]
Expand All @@ -783,9 +779,9 @@ pub mod tests {
.with_input(DIFF_PLUS_MINUS_WITH_1_CONTEXT_DIFF)
.expect_after_header(
r#"
1 │abc │ 1 │abc
2 │a = left side │ 2 │a = right side
3 │xyz │ 3 │xyz"#,
1 │abc │ 1 │abc
2 │a = left side │ 2 │a = right side
3 │xyz │ 3 │xyz"#,
);
}

Expand All @@ -804,10 +800,10 @@ pub mod tests {
.with_input(DIFF_PLUS_MINUS_WITH_1_CONTEXT_DIFF)
.expect_after_header(
r#"
1 │abc │ 1 │abc
2 │a = left @│ 2 │a = right@
1 │abc │ 1 │abc
2 │a = left @│ 2 │a = right@
│ │side │ │ side
3 │xyz │ 3 │xyz"#,
3 │xyz │ 3 │xyz"#,
);

let cfg = &[
Expand All @@ -825,43 +821,43 @@ pub mod tests {
.with_input(DIFF_WITH_LONGER_MINUS_1_CONTEXT)
.expect_after_header(
r#"
1 │abc │ 1 │abc
2 │a = one side │ 2 │a = one longer@
1 │abc │ 1 │abc
2 │a = one side │ 2 │a = one longer@
│ │ │ │ side
3 │xyz │ 3 │xyz"#,
3 │xyz │ 3 │xyz"#,
);

DeltaTest::with_args(cfg)
.with_input(DIFF_WITH_LONGER_PLUS_1_CONTEXT)
.expect_after_header(
r#"
1 │abc │ 1 │abc
2 │a = one longer@│ 2 │a = one side
1 │abc │ 1 │abc
2 │a = one longer@│ 2 │a = one side
│ │ side │ │
3 │xyz │ 3 │xyz"#,
3 │xyz │ 3 │xyz"#,
);

DeltaTest::with_args(cfg)
.with_input(DIFF_MISMATCH_LONGER_MINUS_1_CONTEXT)
.expect_after_header(
r#"
1 │abc │ 1 │abc
2 │a = left side @│ │
1 │abc │ 1 │abc
2 │a = left side @│ │
│ │which is longer│ │
│ │ │ 2 │a = other one
3 │xyz │ 3 │xyz"#,
│ │ │ 2 │a = other one
3 │xyz │ 3 │xyz"#,
);

DeltaTest::with_args(cfg)
.with_input(DIFF_MISMATCH_LONGER_PLUS_1_CONTEXT)
.expect_after_header(
r#"
1 │abc │ 1 │abc
2 │a = other one │ │
│ │ │ 2 │a = right side@
1 │abc │ 1 │abc
2 │a = other one │ │
│ │ │ 2 │a = right side@
│ │ │ │ which is long@
│ │ │ │er
3 │xyz │ 3 │xyz"#,
3 │xyz │ 3 │xyz"#,
);
}

Expand Down
32 changes: 16 additions & 16 deletions src/features/side_by_side.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ pub mod tests {
.with_input(TWO_MINUS_LINES_DIFF)
.expect_after_header(
r#"
1 │a = 1 │ │
2 │b = 23456 │ │"#,
1 │a = 1 │ │
2 │b = 23456 │ │"#,
);
}

Expand All @@ -620,8 +620,8 @@ pub mod tests {
.with_input(TWO_MINUS_LINES_DIFF)
.expect_after_header(
r#"
1 │a = 1 │ │
2 │b = 234>│ │"#,
1 │a = 1 │ │
2 │b = 234>│ │"#,
);
}

Expand All @@ -636,8 +636,8 @@ pub mod tests {
.with_input(TWO_PLUS_LINES_DIFF)
.expect_after_header(
r#"
│ │ │ 1 │a = 1
│ │ │ 2 │b = 234567 "#,
│ │ │ 1 │a = 1
│ │ │ 2 │b = 234567 "#,
);
}

Expand All @@ -652,8 +652,8 @@ pub mod tests {
.explain_ansi()
.with_input(TWO_PLUS_LINES_DIFF)
.expect_after_header(r#"
(blue)│(88) (blue)│(normal) (blue)│(28) 1 (blue)│(231 22)a (203)=(231) (141)1(normal 22) (normal)
(blue)│(88) (blue)│(normal) (blue)│(28) 2 (blue)│(231 22)b (203)=(231) (141)234567(normal 22) (normal)"#);
(blue)│(88) (blue)│(normal) (blue)│(28) 1 (blue)│(231 22)a (203)=(231) (141)1(normal 22) (normal)
(blue)│(88) (blue)│(normal) (blue)│(28) 2 (blue)│(231 22)b (203)=(231) (141)234567(normal 22) (normal)"#);

DeltaTest::with_args(&[
"--side-by-side",
Expand All @@ -664,8 +664,8 @@ pub mod tests {
.explain_ansi()
.with_input(TWO_PLUS_LINES_DIFF)
.expect_after_header(r#"
(blue)│(88) (blue)│(normal) (blue) │(28) 1 (blue)│(231 22)a (203)=(231) (141)1(normal)
(blue)│(88) (blue)│(normal) (blue) │(28) 2 (blue)│(231 22)b (203)=(231) (141)234567(normal)"#);
(blue)│(88) (blue)│(normal) (blue) │(28) 1 (blue)│(231 22)a (203)=(231) (141)1(normal)
(blue)│(88) (blue)│(normal) (blue) │(28) 2 (blue)│(231 22)b (203)=(231) (141)234567(normal)"#);
}

#[test]
Expand All @@ -683,8 +683,8 @@ pub mod tests {
let output = run_delta(TWO_PLUS_LINES_DIFF, &config);
let mut lines = output.lines().skip(crate::config::HEADER_LEN);
let (line_1, line_2) = (lines.next().unwrap(), lines.next().unwrap());
assert_eq!("│ │ │ 1 │a = 1 ", strip_ansi_codes(line_1));
assert_eq!("│ │ │ 2 │b = 2345>", strip_ansi_codes(line_2));
assert_eq!("│ │ │ 1 │a = 1 ", strip_ansi_codes(line_1));
assert_eq!("│ │ │ 2 │b = 2345>", strip_ansi_codes(line_2));
}

#[test]
Expand All @@ -695,8 +695,8 @@ pub mod tests {
let mut lines = output.lines().skip(crate::config::HEADER_LEN);
let (line_1, line_2) = (lines.next().unwrap(), lines.next().unwrap());
let sac = strip_ansi_codes; // alias to help with `cargo fmt`-ing:
assert_eq!("│ │ │ 1 │a = 1", sac(line_1));
assert_eq!("│ │ │ 2 │b = 234567", sac(line_2));
assert_eq!("│ │ │ 1 │a = 1", sac(line_1));
assert_eq!("│ │ │ 2 │b = 234567", sac(line_2));
}

#[test]
Expand All @@ -710,8 +710,8 @@ pub mod tests {
.with_input(ONE_MINUS_ONE_PLUS_LINE_DIFF)
.expect_after_header(
r#"
1 │a = 1 │ 1 │a = 1
2 │b = 2 │ 2 │bb = 2 "#,
1 │a = 1 │ 1 │a = 1
2 │b = 2 │ 2 │bb = 2 "#,
);
}
}
Loading