so-fancy / diff-so-fancy

Good-lookin' diffs. Actually… nah… The best-lookin' diffs. :tada:
MIT License
17.36k stars 335 forks source link

guidance for python implementation? #496

Closed simonLeary42 closed 1 month ago

simonLeary42 commented 1 month ago

I'm writing an ansible callback plugin which uses python standard library difflib, and I would really like to have fancy diffs built in. The only feature I care about is the highlighting of specific changes within a line. Has anyone tried this? Has this already been done and I just couldn't find it? Are there specific files in this codebase which I could read for reference?

I'm not sure if it's feasible to take difflib output and modify it, or if I would have to reinvent difflib. I'm assuming that difflib only returns a string, so then I would have to parse each change by looking for \n\n@@, then find the longest common substrings, then insert some ANSI color escape characters.

scottchiefbaker commented 1 month ago

Pretty much the entire reason d-s-f exists is because I like that intra-line highlighting. The only library I'm aware of that does this is DiffHighlight.pm, which is what d-s-f uses. I'm sure there are other libraries out there, but that's the one we've been using for years.

Good luck.

OJFord commented 1 month ago

Delta has its own implementation I believe, but not sure it can be used as a library. Not that that would be easy from python anyway.

simonLeary42 commented 1 month ago

I ended up calling diffr in a subprocess: https://gist.github.com/simonLeary42/aaf1ba0198905fb9927d143204ca6386

I tried homebrewing it with difflib, but encountered a ton of visual bugs and then when I tried it with a real world comparison the algorithm scaled terribly: https://gist.github.com/simonLeary42/ff794cf4425a4bea52a9ff17c1bc31c3