Skip to content

Instantly share code, notes, and snippets.

@tskinn
Created June 23, 2018 18:42
Show Gist options
  • Save tskinn/0ed07b1c43968807b24e0e585ae58ce2 to your computer and use it in GitHub Desktop.
Save tskinn/0ed07b1c43968807b24e0e585ae58ce2 to your computer and use it in GitHub Desktop.
#!/bin/bash
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]"
exit 1
fi
IFS=':' read -r -a INPUT <<< "$1"
FILE=${INPUT[0]}
CENTER=${INPUT[1]}
if [[ $1 =~ ^[A-Z]:\\ ]]; then
FILE=$FILE:${INPUT[1]}
CENTER=${INPUT[2]}
fi
if [ ! -r "$FILE" ]; then
echo "File not found ${FILE}"
exit 1
fi
if [[ "$(file --mime "$FILE")" =~ binary ]]; then
echo "$1 is a binary file"
exit 0
fi
if [ -z "$CENTER" ]; then
CENTER=1
fi
if [ -z "$LINES" ]; then
if [ -r /dev/tty ]; then
LINES=$(stty size < /dev/tty | awk '{print $1}')
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))
bat --color always --style header,numbers --line-range $FIRST:$LAST $FILE
@tskinn
Copy link
Author

tskinn commented Jun 23, 2018

This is basically just a copy of https://github.com/junegunn/fzf.vim/blob/master/bin/preview.sh but using a command call bat to print the preview with syntax highlighting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment