dandavison / delta

A syntax-highlighting pager for git, diff, grep, and blame output
https://dandavison.github.io/delta/
MIT License
21.34k stars 359 forks source link

Parse filename unambiguously using color escape sequences #1634

Closed joshtriplett closed 4 months ago

joshtriplett commented 4 months ago

Parse filename unambiguously using color escape sequences, to handle cases where separators like - appear in the filename.

git grep, by default, emits ANSI color escape sequences around the filename, separator, and line number. Parse these if available.

Add tests for filenames that previously failed to parse correctly.

Fixes: #1083


Before: delta-before

After: delta-after


This currently assumes the default colors, and will fall back to the previous parsing if any of the colors have been changed. This is an issue elsewhere in delta as well; for instance, if color.grep.match has been changed then delta won't highlight matches. I think this would be easy to fix, by parsing git's color configuration (which I implemented in anstyle-git), rendering those color sequences to ANSI escapes, and using those in the regex rather than hardcoded ones. However, I think that should be a separate PR.

dandavison commented 4 months ago

Brilliant! This is a great advance. I had switched to using rg --json exclusively but I know people will want to use git grep and other greps. (And the git grep -W option is interesting; delta tries to be helpful with it in combination with --navigate, allowing jumping between search hits with n/N)

imbrish commented 4 months ago

Hey @joshtriplett, thanks for this.

I was also thinking about reading custom git grep colors from .gitconfig to parse the output correctly.

Do you plan a follow up pull request for that?

dandavison commented 4 months ago

If anyone does work on reading colors from .gitconfig, I just want to point out this hack that's been in the codebase for a long time. What it's doing is checking whether git has added any colors other than the default added/removed colors; if the answer is "yes" then we output a raw line e.g. because this might be git outputting "color-moved" colors. The hack is that the added/removed colors are hardcoded as red/green. Just mentioning in case it can also be made more robust (although I don't think anyone's ever complained).

https://github.com/dandavison/delta/blob/dcae5bcc2428d1fb6f5ff7b9cddd7f268d9a3735/src/style.rs#L359-L366