もた日記

くだらないことを真面目にやる

Linuxメモ : Rust製のdeltaでgit diff, diffをシンタックスハイライトして表示

delta

github.com

Rust製のdeltaを使うとgit diffなどのgit関連コマンドやdiffコマンドをシンタックスハイライトして表示できるようになる。また、行内での差分を検知して見やすくしてくれる。

f:id:wonder-wall:20200529224742p:plain


インストール

README.mdのインストール方法によるとexecutableのダウンロードなどでインストールできる。

$ delta --help
delta 0.1.1
Dan Davison <[email protected]>
A syntax-highlighter for git and diff output

USAGE:
    delta [FLAGS] [OPTIONS]

FLAGS:
        --color-only                 Do not alter the input in any way other than applying colors. Equivalent to
                                     `--keep-plus-minus-markers --width variable --tabs 0 --commit-style plain --file-
                                     style plain --hunk-style plain`.
        --dark                       Use default colors appropriate for a dark terminal background. For more control,
                                     see the other color options.
    -h, --help                       Prints help information
        --highlight-removed          Apply syntax highlighting to removed lines. The default is to apply syntax
                                     highlighting to unchanged and new lines only.
        --keep-plus-minus-markers    Prefix added/removed lines with a +/- character, respectively, exactly as git does.
                                     The default behavior is to output a space character in place of these markers.
        --light                      Use default colors appropriate for a light terminal background. For more control,
                                     see the other color options.
        --list-languages             List supported languages and associated file extensions.
        --list-theme-names           List available syntax-highlighting color themes.
        --list-themes                List available syntax highlighting themes, each with an example of highlighted diff
                                     output. If diff output is supplied on standard input then this will be used for the
                                     demo. For example: `git show --color=always | delta --list-themes`.
        --show-background-colors     Show the command-line arguments (RGB hex codes) for the background colors that are
                                     in effect. The hex codes are displayed with their associated background color. This
                                     option can be combined with --light and --dark to view the background colors for
                                     those modes. It can also be used to experiment with different RGB hex codes by
                                     combining this option with --minus-color, --minus-emph-color, --plus-color, --plus-
                                     emph-color.
    -V, --version                    Prints version information

OPTIONS:
        --commit-color <commit_color>              Color for the commit section of git output. [default: yellow]
        --commit-style <commit_style>
            Formatting style for the commit section of git output. Options are: plain, box. [default: plain]

        --file-color <file_color>                  Color for the file section of git output. [default: blue]
        --file-style <file_style>
            Formatting style for the file section of git output. Options are: plain, box, underline. [default:
            underline]
        --hunk-color <hunk_color>                  Color for the hunk-marker section of git output. [default: blue]
        --hunk-style <hunk_style>
            Formatting style for the hunk-marker section of git output. Options are: plain, box. [default: box]

        --max-line-distance <max_line_distance>
            The maximum distance between two lines for them to be inferred to be homologous. Homologous line pairs are
            highlighted according to the deletion and insertion operations transforming one into the other. [default:
            0.3]
        --minus-color <minus_color>                The background color to use for removed lines.
        --minus-emph-color <minus_emph_color>      The background color to use for emphasized sections of removed lines.
        --paging <paging_mode>
            Whether to use a pager when displaying output. Options are: auto, always, and never. The default pager is
            `less`: this can be altered by setting the environment variables BAT_PAGER or PAGER (BAT_PAGER has
            priority). [default: auto]
        --plus-color <plus_color>                  The background color to use for added lines.
        --plus-emph-color <plus_emph_color>        The background color to use for emphasized sections of added lines.
        --tabs <tab_width>
            The number of spaces to replace tab characters with. Use --tabs=0 to pass tab characters through directly,
            but note that in that case delta will calculate line widths assuming tabs occupy one character's width on
            the screen: if your terminal renders tabs as more than than one character wide then delta's output will look
            incorrect. [default: 4]
        --theme <theme>
            The code syntax highlighting theme to use. Use --theme=none to disable syntax highlighting. If the theme is
            not set using this option, it will be taken from the BAT_THEME environment variable, if that contains a
            valid theme name. Use --list-themes to view available themes. Note that the choice of theme only affects
            code syntax highlighting. See --commit-color, --file-color, --hunk-color to configure the colors of other
            parts of the diff output. [env: BAT_THEME=]
        --24-bit-color <true_color>
            Whether to emit 24-bit ("true color") RGB color codes. Options are auto, always, and never. "auto" means
            that delta will emit 24-bit color codes iff the environment variable COLORTERM has the value "truecolor" or
            "24bit". If your terminal application (the application you use to enter commands at a shell prompt) supports
            24 bit colors, then it probably already sets this environment variable, in which case you don't need to do
            anything. [default: auto]
    -w, --width <width>
            The width (in characters) of the background color highlighting. By default, the width is the current
            terminal width. Use --width=variable to apply background colors to the end of each line, without right
            padding to equal width.

Colors
------

All delta color options work the same way. There are three ways to specify a color:

1. RGB hex code

   An example of using an RGB hex code is:
   --file-color="#0e7c0e"

2. ANSI color name

   There are 8 ANSI color names:
   black, red, green, yellow, blue, magenta, cyan, white.

   In addition, all of them have a bright form:
   bright-black, bright-red, bright-green, bright-yellow, bright-blue, bright-magenta, bright-cyan, bright-white

   An example of using an ANSI color name is:
   --file-color="green"

   Unlike RGB hex codes, ANSI color names are just names: you can choose the exact color that each
   name corresponds to in the settings of your terminal application (the application you use to
   enter commands at a shell prompt). This means that if you use ANSI color names, and you change
   the color theme used by your terminal, then delta's colors will respond automatically, without
   needing to change the delta command line.

   "purple" is accepted as a synonym for "magenta". Color names and codes are case-insensitive.

3. ANSI color number

   An example of using an ANSI color number is:
   --file-color=28

   There are 256 ANSI color numbers: 0-255. The first 16 are the same as the colors described in
   the "ANSI color name" section above. See https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit.
   Specifying colors like this is useful if your terminal only supports 256 colors (i.e. doesn't
   support 24-bit color).


使い方

.gitconfigに以下のような設定を追加する。
--plus-colorは追加行の背景色、--minus-colorは削除行の背景色、--themeはテーマ。
[interactive]のdiffFilterはgit add --patchなどのインタラクティブモードの設定。

[core]
    pager = delta --plus-color="#012800" --minus-color="#340001" --theme='Monokai Extended'

[interactive]
    diffFilter = delta --color-only

設定を追加すれば下記コマンドなどでシンタックスハイライトして表示されるようになる。

  • git diff
  • git show
  • git log -p
  • git stash show -p
  • git reflog -p
  • git add -p

f:id:wonder-wall:20200529224843p:plain


diffコマンドに対して使いたい場合は以下のようにする。

$ diff -u a.py b.py | delta

f:id:wonder-wall:20200529224820p:plain


テーマ

--list-themesオプションを実行するとテーマのデモが確認できる。

f:id:wonder-wall:20200529224742p:plain

テーマ名の一覧を確認したいときは--list-theme-namesオプションを実行する。

$ delta --list-theme-names
Light themes:
    GitHub
    Monokai Extended Light
    OneHalfLight
    Solarized (light)
    ansi-light
Dark themes:
    1337
    DarkNeon
    Dracula
    Monokai Extended
    Monokai Extended Bright
    Monokai Extended Origin
    Nord
    OneHalfDark
    Solarized (dark)
    Sublime Snazzy
    TwoDark
    ansi-dark
    base16
    zenburn