From 5872aad1d0f841770db498cba9310d647c4aa376 Mon Sep 17 00:00:00 2001 From: Arjan Schrijver Date: Sat, 9 Apr 2022 21:45:30 +0200 Subject: [PATCH] git: add option to use diff-so-fancy as diff tool Add the option to use diff-so-fancy as diff tool in git. This makes the shown diffs human readable instead of machine readable. --- modules/programs/git.nix | 85 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 2 deletions(-) diff --git a/modules/programs/git.nix b/modules/programs/git.nix index 61792f866..c79bdae20 100644 --- a/modules/programs/git.nix +++ b/modules/programs/git.nix @@ -296,6 +296,65 @@ in { ''; }; }; + + diff-so-fancy = { + enable = mkEnableOption "" // { + description = '' + Enable the diff-so-fancy diff colorizer. + See . + ''; + }; + + markEmptyLines = mkOption { + type = types.bool; + default = true; + example = false; + description = '' + Whether the first block of an empty line should be colored. + ''; + }; + + changeHunkIndicators = mkOption { + type = types.bool; + default = true; + example = false; + description = '' + Simplify git header chunks to a more human readable format. + ''; + }; + + stripLeadingSymbols = mkOption { + type = types.bool; + default = true; + example = false; + description = '' + Whether the + or - at + line-start should be removed. + ''; + }; + + useUnicodeRuler = mkOption { + type = types.bool; + default = true; + example = false; + description = '' + By default, the separator for the file header uses Unicode + line-drawing characters. If this is causing output errors on + your terminal, set this to false to use ASCII characters instead. + ''; + }; + + rulerWidth = mkOption { + type = types.nullOr types.int; + default = null; + example = false; + description = '' + By default, the separator for the file header spans the full + width of the terminal. Use this setting to set the width of + the file header manually. + ''; + }; + }; }; }; @@ -303,9 +362,12 @@ in { { home.packages = [ cfg.package ]; assertions = [{ - assertion = !(cfg.delta.enable && cfg.difftastic.enable); + assertion = let + enabled = + [ cfg.delta.enable cfg.diff-so-fancy.enable cfg.difftastic.enable ]; + in count id enabled <= 1; message = - "Only one of 'programs.git.delta.enable' or 'programs.git.difftastic.enable' can be set to true at the same time."; + "Only one of 'programs.git.delta.enable' or 'programs.git.difftastic.enable' or 'programs.git.diff-so-fancy.enable' can be set to true at the same time."; }]; programs.git.iniContent.user = { @@ -432,5 +494,24 @@ in { delta = cfg.delta.options; }; }) + + (mkIf cfg.diff-so-fancy.enable { + home.packages = [ pkgs.diff-so-fancy ]; + + programs.git.iniContent = + let dsfCommand = "${pkgs.diff-so-fancy}/bin/diff-so-fancy"; + in { + core.pager = "${dsfCommand} | ${pkgs.less}/bin/less --tabs=4 -RFX"; + interactive.diffFilter = "${dsfCommand} --patch"; + diff-so-fancy = { + markEmptyLines = cfg.diff-so-fancy.markEmptyLines; + changeHunkIndicators = cfg.diff-so-fancy.changeHunkIndicators; + stripLeadingSymbols = cfg.diff-so-fancy.stripLeadingSymbols; + useUnicodeRuler = cfg.diff-so-fancy.useUnicodeRuler; + rulerWidth = mkIf (cfg.diff-so-fancy.rulerWidth != null) + (cfg.diff-so-fancy.rulerWidth); + }; + }; + }) ]); }