Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support external tool for adding color and syntax highlighting to diffs #49

Open
dandavison opened this issue Mar 11, 2024 · 4 comments

Comments

@dandavison
Copy link

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.)

@altsem
Copy link
Owner

altsem commented Mar 11, 2024

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

fn format_diff_hunk_items(

@dandavison
Copy link
Author

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.

@tiejunhu
Copy link

is this supported yet?

@altsem
Copy link
Owner

altsem commented May 14, 2024

@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.

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

No branches or pull requests

3 participants