Skip to content

Instantly share code, notes, and snippets.

@dufferzafar
Created May 31, 2023 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dufferzafar/bd4b6bd5cee7cde98b4e411b057dec2f to your computer and use it in GitHub Desktop.
Save dufferzafar/bd4b6bd5cee7cde98b4e411b057dec2f to your computer and use it in GitHub Desktop.
Previewer for lf using batter
#!/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 [ -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 \
--line-range $FIRST:$LAST "$FILE" \
--style numbers,changes \
--terminal-width=$(tput cols)
# --style=plain,snip,numbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment