gitu icon indicating copy to clipboard operation
gitu copied to clipboard

Support external tool for adding color and syntax highlighting to diffs

Open dandavison opened this issue 1 year ago • 4 comments

Hi @altsem, gitu looks fantastic. Would it be possible to support using an external tool to add colors to diffs before display? I'm the author of https://github.com/dandavison/delta, and I'd love to be able to use gitu with colors supplied by delta. Delta works by taking diff format input on stdin, and writing the same diff to stdout with ANSI color escape sequences inserted. It has an option --color-only that guarantees not to make any structural alterations to the diff, other than adding colors (syntax highlighting and/or background colors). So the simplest implementation would be to allow users to configure an external binary that is passed the raw diff as text, just before gitu is ready to display it.

(Incidentally, delta can be used in this way to add syntax highlighting and colors to emacs magit, but of course the implementation there is more complex because the ANSI color escapes produced by delta have to be translated to the way emacs represents colored text: https://github.com/dandavison/magit-delta.)

dandavison avatar Mar 11 '24 13:03 dandavison

Hey! Happy you like the tool, I'm a user of Delta as well and find it great! :)

Do you mean kind of like: image ?

I was experimenting with this and I like the idea! It took just a hunk at a time, and sent it to Delta.

I also thought about the --color-only vs letting Delta do everything, while i liked the fact of showing side-by-side views in Gitu as well, it'd make it impossible to stage individual lines (in the future). Perhaps it could be configurable.

The reason I worked on moving away from this implementation I think was due to:

  • I wanted Gitu to work out of the box, without needing to install other software.
  • The implementation was a bit slow (opening a new process for each hunk).

...the implementation there is more complex because the ANSI color escapes produced by delta have to be translated to the way emacs represents colored text.

This is what I encountered too, as Ratatui doesn't read ansi escape codes, though I found there's a lib for doing so. I think I had some issues with it, and there was no way to distinguish between literal text containing escape codes, and the escape codes themselves. While when I just git diff the text would appear.

Anyway, I think this will fit right in as a configurable option (cli or the config-file)! The function responsible for formatting a hunk is now at https://github.com/altsem/gitu/blob/ecd068f77de43138f9a7a3070f567612ab40f948/src/items.rs#L88

altsem avatar Mar 11 '24 16:03 altsem

Great! Yes, supporting side-by-side would be nice if it can be done in a way that doesn't get in the way of basic magit functionality like staging selected lines. But I'd be happy with delta-colored standard diffs initially.

dandavison avatar Mar 12 '24 00:03 dandavison

is this supported yet?

tiejunhu avatar May 14 '24 02:05 tiejunhu

@tiejunhu Not yet, the code has been refactored since the attempt in #88 (not sure why that was not working). I'm not currently hacking on it, but if someone would like to pick it up I'm all for helping out.

We now have per-line staging/unstaging. So --color-only would be the way to go.

altsem avatar May 14 '24 15:05 altsem