[preview] Clean up preview scripts

- Use preview.sh instead of preview.rb by default
- Try bat syntax highlighter with the highest priority
- Remove "-v" option as the latest version of fzf sets up $LINES
- Allow users to customize the preview command via $FZF_PREVIEW_COMMAND
  - *EXPERIMENTAL / EVOLVING / UNDOCUMENTED*
  - Should be a command template with {} placeholder expression
    - e.g. "nl {}"
This commit is contained in:
Junegunn Choi
2018-11-09 15:57:17 +09:00
parent b73d141b74
commit 0dbcfb28c4
4 changed files with 28 additions and 40 deletions

View File

@@ -3,13 +3,8 @@
REVERSE="\x1b[7m"
RESET="\x1b[m"
if [ "$1" == "-v" ]; then
SPLIT=1
shift
fi
if [ -z "$1" ]; then
echo "usage: $0 [-v] FILENAME[:LINENO][:IGNORED]"
echo "usage: $0 FILENAME[:LINENO][:IGNORED]"
exit 1
fi
@@ -33,7 +28,7 @@ if [[ "$(file --mime "$FILE")" =~ binary ]]; then
fi
if [ -z "$CENTER" ]; then
CENTER=1
CENTER=0
fi
if [ -z "$LINES" ]; then
@@ -42,26 +37,18 @@ if [ -z "$LINES" ]; then
else
LINES=40
fi
if [ -n "$SPLIT" ]; then
LINES=$(($LINES/2)) # using horizontal split
fi
LINES=$(($LINES-2)) # remove preview border
fi
FIRST=$(($CENTER-$LINES/3))
FIRST=$(($FIRST < 1 ? 1 : $FIRST))
LAST=$((${FIRST}+${LINES}-1))
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
DEFAULT_COMMAND="bat --style=numbers --color=always {} || highlight -O ansi -l {} || coderay {} || rougify {} || cat {}"
CMD=${FZF_PREVIEW_COMMAND:-$DEFAULT_COMMAND}
CMD=${CMD//{\}/$(printf %q "$FILE")}
eval "$CMD" 2> /dev/null | 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); \
}"