difftastic icon indicating copy to clipboard operation
difftastic copied to clipboard

word-diff for the plaintext fallback parser

Open int opened this issue 2 years ago • 5 comments

Not a bug. Just a feature request (label: enhancement).

(1) A description of the issue. A screenshot is often helpful too.

Currently, difftastic treats plain text (the fallback) as a simple line-oriented diff. Would be nice if we can enable some kind of word-diff for it.

In real life, various config files are treated as plain text (fallback). And if we only change a small part of a line (e.g. theme="onedark" -> theme="twodark"), word-diff would make the output more pleasant than the plain line-diff.

Below are some screenshots of an "artificial" use case showing remove/change/insert.

difft a.txt b.txt: difft

difft --display inline a.txt b.txt: difft inline

git diff --no-index --color-words=. a.txt b.txt: git diff word-diff

Since we are advertising difft as more than word-diff at our front page, can we make it behave similarly for the fallback plain text?

(2) A copy of what you're diffing.

------a.txt-----
hi, this is a sample text.
you can edit it as freely as you can.
cheers

------b.txt-----
hi, this is a sample txt.
you can change it as freely as you can.
cheers!

(3) The version of difftastic you're using (see difft --version) and your operating system.

Difftastic 0.45.0 WSL 2 on Windows 10

int avatar Mar 14 '23 09:03 int

There is word-level diffing, but I'm guessing the bright and bold style isn't supported in your terminal.

Screenshot from 2023-03-14 08-16-08

That said, this is kinda subtle, especially for the changed !. Difftastic will underline changed words when diffing comments in code, but it can be overwhelming when most of the words on a line have changed.

Wilfred avatar Mar 14 '23 15:03 Wilfred

Do those colors have meaning? (It looks to me as if the red means deleted and the green means added, but maybe that's a notion I got from somewhere else. To a color blind person or a terminal that doesn't do bold, it looks like the entire line changed.)

joyously avatar Mar 14 '23 17:03 joyously

There is word-level diffing

Cool. So the functionality has already been there. :)

but I'm guessing the bright and bold style isn't supported in your terminal.

Yeah. Seems Windows Terminal doesn't enable it by default. Here for a fix.

That said, this is kinda subtle

Yes Indeed. How about we don't highlight those unchanged parts at all? Make it just like what we did for the "recognized" programming languages.

The --color-words (--word-diff-regex) is an extra feature -- it lets you define what a "word" is. In the screenshot of git above, it treats each char as a word.

And probably an independent topic (but kinda related with word-diff): Looks I was confused by the different modes of difft --display. Now I realize that for difft:

  • side-by-side is usually side-by-side-show-both unless lhs_changes or rhs_changes is empty
  • side-by-side-show-both is the REAL side by side for me, and then
  • inline is just good old "unified" diff.

A color-words (or merged) mode would be nice (see the git screenshot above). (Well, personally I call it "inline" - probably just me).

Keeping old names and semantics unchanged to avoid compatible issues, I'd suggest these three names/modes:

  • both -- alias for the old side-by-side-show-both
  • unified -- alias for the old inline
  • merged -- the old side-by-side when lhs/rhs empty + the new color-words

I for one would make both or merged as default. It's kinda surprising (at least for me) that side-by-side sometimes only show one side and we have to specify another suffix show-both to really show both sides.

int avatar Mar 16 '23 08:03 int

Related: for changes in comments of programming languages.

e.g. a repro: uncomment code.

  • difft difft

  • git diff --color-words=. git diff --color-words=.

int avatar Mar 17 '23 07:03 int