[preview] Use bat for syntax highlighting if available

In the preview.sh script (used when ruby is not installed),
`bat` (a clone of `cat` with syntax highlighting, etc.) can be
used for the sake of syntax highlighting. If bat is not available,
just fallback to the plain cat as before.

[bat]: https://github.com/sharkdp/bat
This commit is contained in:
Jongwook Choi
2018-10-07 16:21:12 -04:00
committed by Junegunn Choi
parent 50707b089b
commit 974d366b33

View File

@@ -52,4 +52,16 @@ FIRST=$(($CENTER-$LINES/3))
FIRST=$(($FIRST < 1 ? 1 : $FIRST))
LAST=$((${FIRST}+${LINES}-1))
awk "NR >= $FIRST && NR <= $LAST {if (NR == $CENTER) printf(\"$REVERSE%5d %s\n$RESET\", NR, \$0); else printf(\"%5d %s\n\", NR, \$0)}" $FILE
if which bat >/dev/null; then
CAT="bat --style=numbers --color=always"
$CAT $FILE | awk "NR >= $FIRST && NR <= $LAST { \
if (NR == $CENTER) \
{ gsub(/\x1b[[0-9;]*m/, \"&$REVERSE\"); printf(\"$REVERSE%s\n$RESET\", \$0); } \
else printf(\"$RESET%s\n\", \$0); \
}"
else
awk "NR >= $FIRST && NR <= $LAST { \
if (NR == $CENTER) printf(\"$REVERSE%5d %s\n$RESET\", NR, \$0); \
else printf(\"%5d %s\n\", NR, \$0); \
}" $FILE
fi