8 ms·
Show HN: A syntax highlighter for Git and diff output, written in Rust
- Myrmornis 7y agoDelta[0] applies syntax highlighting to the code sections of git and diff output, and uses background colors (instead of +/-) to indicate added / removed content. It's written in Rust, and uses the syntect[1] highlighting library. That's the same library that bat[2] uses, so delta supports all the same color themes, and any bat configuration that you already have in place (environment variables, custom themes/language support) is automatically honored by delta. Delta also infers and highlights substrings that differ between lines (similar to Github, Gitlab and other diff viewers) and optionally cleans up the raw diff output slightly to make it more human readable. [0] https://github.com/dandavison/delta https://github.com/dandavison/delta [1] https://github.com/trishume/syntect https://github.com/trishume/syntect [2] http://github.com/sharkdp/bat/ http://github.com/sharkdp/bat/
- frou_dh 7y agoDoes it work properly with `git add -p`? That's a longstanding issue of diff-so-fancy: https://github.com/so-fancy/diff-so-fancy/issues/35 https://github.com/so-fancy/diff-so-fancy/issues/35
- lzybkr 7y agoIt does not, but it seems like a popular request: https://github.com/dandavison/delta/issues/40 https://github.com/dandavison/delta/issues/40
- Myrmornis 7y agoYes it does. Thanks for asking, I hadn't tested this out. The key is the `--color-only` argument, which guarantees that, other than ANSI color escape sequences, the original output from git is unchanged: [interactive] diffFilter = delta --color-only
- OJFord 7y agoLooks like it works 'properly' in the sense that it does all that it can (colouring, incl. syntax highlighting, which d-s-f never does) without making the result wonky. The core issue is the same for delta as it is for d-s-f (or any other interactive pager) - there must be a 1:1 correspondence between lines out from git, and lines back into it. So, delta can do the fancier colouring interactively, but it can't mess with the header either, for the same underlying reason. (And so 'solved' it with a `--color-only` flag.)
- yboris 7y agoSeems related: diff2html https://diff2html.xyz/ https://diff2html.xyz/ npm-installed cli which shows you git diff in your browser
- nitsky 7y agoJust installed it, it looks great and is a big step up from diff-so-fancy. Thanks for making this!
- lzybkr 7y agoIf you to use this on Windows, you'll need to update `less.exe` with my patched version - https://github.com/lzybkr/less/releases/tag/fix_windows_vt https://github.com/lzybkr/less/releases/tag/fix_windows_vt
- kccqzy 7y agoThis might be just me but looking at the screenshots, I clearly prefer the GitHub one as it shows the line numbers of every line in both before and after. Line numbers make jumping between the diff and the editor fast and efficient.
- Myrmornis 7y agoThanks, that's definitely a feature that could be added. My original thought was that the line numbers in the after file are nearly always what one wants, since that's the version that your editor will most naturally be displaying. And I wanted to reduce visual clutter. Delta currently displays just the first line number of each diff hunk; not on every line. But that's also something that could be made configurable. EDIT: In fact an issue was opened for this very recently: https://github.com/dandavison/delta/issues/130 https://github.com/dandavison/delta/issues/130 Ideally, I think that a mouse click in an appropriate location in a terminal emulator window should open the user's editor on the correct line of the correct file.
- fallingmeat 7y agocan we get one written in Ada too?
- ninjaranter 7y agoIs there a way to see diff output side-by-side? I personally find that much easier to parse through with a widescreen monitor.
- Myrmornis 7y agoHi, thanks, this is an interesting question and there have been requests for it. Two possibilities come to mind: 1. Delta constructs the side-by-side diff itself (a parsed internal representation of it), and then colorizes it. 2. Delta gains the ability to parse side-by-side diffs being emitted by another tool. My currently feeling is that (1) is more attractive / easier.
- jwatt 7y ago#1 would be useful for sure.
- bdefore 7y agoAs a suggestion, I'd have the configuration step in 'Configure git to use delta' not reference a theme that doesn't come with the initial install as you've done with 'base-16.ocean.dark'. I had to investigate why, and (admittedly happily) then found out about bat and installed that and changed it to 'zenburn'. But I suppose most users would like to see it work without having to do that.
- nitsky 7y agoI ran into the same issue, and chose the same theme I use with bat.
- Myrmornis 7y agoThank you! Fixed that.
- bdefore 7y agoI don't know if this is better per-se but my diff-so-fancy config sat in [pager] rather than [core]. I verified that git-delta works as configured this way instead: [pager] diff = delta --dark --plus-color="#012800" --minus- color="#340001" --theme="zenburn"
- loevborg 7y agoThe light theme looks beautiful, thanks!
- Myrmornis 7y agoThanks. I like the light theme also. If anyone has suggestions of good {theme, background-highlight-colors} choices, especially for dark backgrounds, please open a PR/issue and we can add them to the README.
- Klasiaster 7y agoA comparison to http://www.colordiff.org/ http://www.colordiff.org/ would be good in the table of the README.
- apas 7y agoThis is neat. In a similar fashion I'm a big fan of diff-highlight, [0] which post-processes the git diff and highlights with color the differing segments. It's super lightweight and part of the official git repo; I've saved it as a standalone #!/bin/perl in my $PATH and then included it in my .gitconfig: [pager] log = /Users/user/.bin/diff-highlight | less show = /Users/user/.bin/diff-highlight | less diff = /Users/user/.bin/diff-highlight | less [0]: https://github.com/git/git/tree/master/contrib/diff-highlight https://github.com/git/git/tree/master/contrib/diff-highligh...
- kyzn 7y agoI love that too! My config is almost the same, but I have "pager = ..." under "[core]"
- mookid11 7y agoSort of related: as I never made diff-highlight work correctly on Windows, I wrote diffr[0] which goes a little bit behond what diff-highlight does. [0]: https://github.com/mookid/diffr https://github.com/mookid/diffr
- eximius 7y agoicdiff is my current favorite. This does look nice, though I'm a little confused by the choice to highlight the section below the function declaration change even though it doesnt appear to have a change.
- Myrmornis 7y agoIs it the hunk header you're referring to? That's something that git outputs, like this, before the diff hunk: @@ -62,17 +62,17 @@ class MyClass: Delta makes that more prominent by surrounding it with a box (by default) and getting rid of the for-machines characters that git puts there. It doesn't apply background colors to it though: delta only applies background red and green shading to removed/added lines. (I believe that one can customize the regular expressions that git uses to decide on the hunk header; they're sometimes appropriate and sometimes not, for the languages that I use.)
- j88439h84 7y agoCan anybody suggest a --plus-color that looks nice on a solarized-dark blue terminal?
- squaresmile 7y agoI settled on --plus-color="#157330" --minus-color="#731525".
- j88439h84 7y agoI can't tell if --dark is changing anything. That seems to be the default on my dark terminal?
- Myrmornis 7y agoYes, --dark is the default and it will choose a dark theme. To see a demo of other dark themes, you can do delta --list-themes --dark and then delta --theme <chosen theme> to select it.
- firloop 7y agoAnyone know how to get this working with vim-fugitive? https://github.com/tpope/vim-fugitive https://github.com/tpope/vim-fugitive
- airblade 7y agoNot sure about vim-fugitive, but vim-gitgutter's hunk previews have intra-line highlighting. I mention it because people often use the two plugins together (and I'm pleased with how vgg's highlighting turned out :) https://github.com/airblade/vim-gitgutter https://github.com/airblade/vim-gitgutter
- atombender 7y agoThis is fantastic. Kudos! A tip for people who (like me) don't like that Git pages by default: With Delta, you can disable this it by adding this to your environment: export BAT_PAGER=cat
- deleted 7y ago[deleted]
- highmastdon 7y agoI think there’s ‘tig’ as well. You can use it as ‘git diff | tig’
- willbw 7y agoAnyone else getting this issue? ⟩ git diff error: Found argument 'Extended' which wasn't expected, or isn't valid in this context USAGE: delta --dark --minus-color <minus_color> --plus-color <plus_color> --theme <theme> For more information try --help My ~/.gitconfig file has the copy pasted lines from the github example [core] pager = delta --dark --plus-color="#012800" --minus-color="#340001" --theme="Monokai Extended" [interactive] diffFilter = delta --color-only Installed with homebrew on macos.
- Myrmornis 7y agoThanks! I made a hasty edit to the README earlier. Try --theme='Monokai Extended' It seems that in ~/.gitconfig, a theme name with spaces must be quoted with single quotes. At least, that is working on MacOS.
- xixixao 7y agoFYI that dark theme example has unreadable blue for me in looking at it with night shift on. I'd suggest swapping out for a better theme.
- Myrmornis 7y agoI agree. If anyone has nice combinations of delta background colors and syntax highlighting theme for dark backgrounds, please send them my way! (Here, or PR/issue).
- axegon_ 7y agoStuff like that makes my daily code reviews so so SO much better! I can't thank you enough!
- Myrmornis 7y agoThis is good to hear :)