Skip to content

Fix hunk alignment for inline display#977

Open
bvergnaud wants to merge 1 commit into
Wilfred:masterfrom
bvergnaud:master
Open

Fix hunk alignment for inline display#977
bvergnaud wants to merge 1 commit into
Wilfred:masterfrom
bvergnaud:master

Conversation

@bvergnaud
Copy link
Copy Markdown

Hello there 👋🏻

The side-by-side displays use padding to align the line numbers of hunks, but the inline display did not. For a hunk that has line numbers transitioning to a higher order (e.g. lines 7-12, or 97-102), the file contents would end up misaligned by that one character difference.

To highlight the issue, here is a pair of plain text files.

[…/difftastic][01:55:28] > cat /tmp/test_left.txt
line D7037CABD97774A05FFCD38194093BFF
line 9E5441F1DD1FE52F1F5B5C73E853CC41
line 4624B6E1A7C2F94CE15D48E626AAA8C1
line C0673AF915DA0160838EBDBD95D2D32A
line 4DC6E37409A4EEC157F5A48DF044AEAE
line B6ADCBE985C6202E14AC388DA47D2C73
line 0ABAD62A62ABD2E2E220ED4BEC0D877D
line 4ED1B7FCDBA4BF863F47482A435A6FAB
line 84824623A6A4BF3A06715FCBB63585F5
line 4C827495A0D45CDC5FD1D400A641690B
line E80F6B161B2A7523FB5AFE053D24C217
line 4DAA9AE6CC0CAB9E0E0E369B77D2FCE2
line F496281D9F0B78EEDB4245DE0AEFC800

[…/difftastic][01:55:34] > cat /tmp/test_right.txt
line D7037CABD97774A05FFCD38194093BFF
line 9E5441F1DD1FE52F1F5B5C73E853CC41
line 4624B6E1A7C2F94CE15D48E626AAA8C1
line C0673AF915DA0160838EBDBD95D2D32A
line 4DC6E37409A4EEC157F5A48DF044AEAE
line B6ADCBE985C6202E14AC388DA47D2C73
line 0ABAD62A62ABD2E2E220ED4BEC0D877D
line 4ED1B7FCDBA4BF863F47482A435A6FAB modified
line 84824623A6A4BF3A06715FCBB63585F5 modified
line 4C827495A0D45CDC5FD1D400A641690B modified
line E80F6B161B2A7523FB5AFE053D24C217
line 4DAA9AE6CC0CAB9E0E0E369B77D2FCE2
line F496281D9F0B78EEDB4245DE0AEFC800

And here's what the inline diff looks like with the latest release of difftastic:

[…/difftastic][01:56:23] > difft --display=inline /tmp/test_left.txt /tmp/test_right.txt
/tmp/test_right.txt --- Text
4    line C0673AF915DA0160838EBDBD95D2D32A
5    line 4DC6E37409A4EEC157F5A48DF044AEAE
6    line B6ADCBE985C6202E14AC388DA47D2C73
7    line 0ABAD62A62ABD2E2E220ED4BEC0D877D
8    line 4ED1B7FCDBA4BF863F47482A435A6FAB
9    line 84824623A6A4BF3A06715FCBB63585F5
10    line 4C827495A0D45CDC5FD1D400A641690B
   8 line 4ED1B7FCDBA4BF863F47482A435A6FAB modified
   9 line 84824623A6A4BF3A06715FCBB63585F5 modified
   10 line 4C827495A0D45CDC5FD1D400A641690B modified
   11 line E80F6B161B2A7523FB5AFE053D24C217
   12 line 4DAA9AE6CC0CAB9E0E0E369B77D2FCE2
   13 line F496281D9F0B78EEDB4245DE0AEFC800

This commit re-uses the padding logic of the side-by-side displays in the inline display to fix that weird alignment.

[…/difftastic][01:59:47] > ./target/release/difft --display=inline /tmp/test_left.txt /tmp/test_right.txt
/tmp/test_right.txt --- Text
 4    line C0673AF915DA0160838EBDBD95D2D32A
 5    line 4DC6E37409A4EEC157F5A48DF044AEAE
 6    line B6ADCBE985C6202E14AC388DA47D2C73
 7    line 0ABAD62A62ABD2E2E220ED4BEC0D877D
 8    line 4ED1B7FCDBA4BF863F47482A435A6FAB
 9    line 84824623A6A4BF3A06715FCBB63585F5
10    line 4C827495A0D45CDC5FD1D400A641690B
    8 line 4ED1B7FCDBA4BF863F47482A435A6FAB modified
    9 line 84824623A6A4BF3A06715FCBB63585F5 modified
   10 line 4C827495A0D45CDC5FD1D400A641690B modified
   11 line E80F6B161B2A7523FB5AFE053D24C217
   12 line 4DAA9AE6CC0CAB9E0E0E369B77D2FCE2
   13 line F496281D9F0B78EEDB4245DE0AEFC800

I'm not sure if this fixes any specific issues. I've looked through a bunch that mention the inline display mode, none of them seem to identify that specific problem. But it has been annoying me for a while and I figured it was time to offer a fix. 😅

The side-by-side displays use padding to align the line numbers of
hunks, but the inline display did not.
For a hunk that has line numbers transitionning to a higher order
(e.g. lines 7-12), the file contents would end up misaligned by that
one character difference, which is especially annoying for code blocks.

This commit re-uses the padding logic of the side-by-side displays in
the inline display to fix that.

Signed-off-by: Benjamin VERGNAUD <ben@bvergnaud.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant