Commit 76c4989e authored by Arian Stolwijk's avatar Arian Stolwijk
Browse files

Use pygmentize for git diffs

parent 6cb0557b
Pipeline #15883 failed with stages
in 1 minute and 19 seconds
......@@ -27,6 +27,7 @@
rebase = true
[diff]
tool = meld
external = git-hldiff-driver
[difftool]
prompt = false
[difftool "meld"]
......
#!/bin/bash
set -euo pipefail
# Program suitable for GIT_EXTERNAL_DIFF, which will syntax-highlight differences.
hldiff -u "$2" "$5" || true
#!/bin/bash
set -euo pipefail
# Diff two files with syntax highlighting using pygmentize.
# pygmentize should be a pygmentize wrapper with your preferred
# pygmentize settings (formatter / style / filters)
function prepare() {
local fn=$1
# Work around https://bitbucket.org/birkenfeld/pygments-main/issues/1437
if [[ "$fn" == /dev/null ]]
then
return
fi
local lexer
lexer=$(pygmentize -N "$fn")
#printf "Detected lexer of %q as %q\n" "$fn" "$lexer" 1>&2
if [[ "$lexer" == text ]]
then
expand "$fn" | pygmentize-term -g
else
expand "$fn" | pygmentize-term -l "$lexer"
fi
}
# Use colordiff (instead of diff --color=always) solely because it
# produces consistent formatting on a per-line basis, saving us from
# having to keep track of state across lines
diff=(colordiff --color=yes)
for arg in "$@"
do
if [[ $arg == -* ]]
then
diff+=("$arg")
else
exec {fd}< <(prepare "$arg")
diff+=(/dev/fd/$fd)
fi
done
sed=(
sed
# replace "all attributes off" (used by Pygmentize to turn off
# bold) with "normal intensity"
-e 's/\x1b\[00m/\x1b[22m/g'
# Replace colordiff's foreground colors with some dark background
# colors. You can customize them here.
-e 's/^\x1b\[0;36m/\x1b[0;36;48;5;23m/' # cyan
-e 's/^\x1b\[0;31m/\x1b[0;31;48;5;52m/' # red
-e 's/^\x1b\[0;32m/\x1b[0;32;48;5;22m/' # green
# Extend background color across the entire terminal window width
-e 's/\x1b\[0;0m$/\x1b\[K\x1b[0m/'
)
"${diff[@]}" | "${sed[@]}"
#!/bin/sh
exec pygmentize -f terminal256 -O bg=dark,style=monokai "$@"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment