-
-
Notifications
You must be signed in to change notification settings - Fork 585
/
preview.sh
executable file
·90 lines (75 loc) · 1.96 KB
/
preview.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env bash
REVERSE="\x1b[7m"
RESET="\x1b[m"
if [[ $# -lt 1 ]]; then
echo "usage: $0 [--tag] FILENAME[:LINENO][:IGNORED]"
exit 1
fi
if [[ $1 = --tag ]]; then
shift
"$(dirname "${BASH_SOURCE[0]}")/tagpreview.sh" "$@"
exit $?
fi
# Ignore if an empty path is given
[[ -z $1 ]] && exit
IFS=':' read -r -a INPUT <<< "$1"
FILE=${INPUT[0]}
CENTER=${INPUT[1]}
if [[ "$1" =~ ^[A-Za-z]:\\ ]]; then
FILE=$FILE:${INPUT[1]}
CENTER=${INPUT[2]}
fi
if [[ -n "$CENTER" && ! "$CENTER" =~ ^[0-9] ]]; then
exit 1
fi
CENTER=${CENTER/[^0-9]*/}
# MS Win support
if [[ "$FILE" =~ '\' ]]; then
if [ -z "$MSWINHOME" ]; then
MSWINHOME="$HOMEDRIVE$HOMEPATH"
fi
if grep -qEi "(Microsoft|WSL)" /proc/version &> /dev/null ; then
MSWINHOME="${MSWINHOME//\\/\\\\}"
FILE="${FILE/#\~\\/$MSWINHOME\\}"
FILE=$(wslpath -u "$FILE")
elif [ -n "$MSWINHOME" ]; then
FILE="${FILE/#\~\\/$MSWINHOME\\}"
fi
fi
FILE="${FILE/#\~\//$HOME/}"
if [ ! -r "$FILE" ]; then
if [[ "${INPUT[0]}" != '[No Name]' ]]; then
echo "File not found ${FILE}"
fi
exit 1
fi
if [ -z "$CENTER" ]; then
CENTER=0
fi
# Sometimes bat is installed as batcat.
if [[ -z "$BATCAT" ]]; then
if command -v batcat > /dev/null; then
BATCAT="batcat"
elif command -v bat > /dev/null; then
BATCAT="bat"
fi
fi
if [ -z "$FZF_PREVIEW_COMMAND" ] && [ "${BATCAT:+x}" ]; then
${BATCAT} --style="${BAT_STYLE:-numbers}" --color=always --pager=never \
--highlight-line=$CENTER -- "$FILE"
exit $?
fi
FILE_LENGTH=${#FILE}
MIME=$(file --dereference --mime -- "$FILE")
if [[ "${MIME:FILE_LENGTH}" =~ binary ]]; then
echo "$MIME"
exit 0
fi
DEFAULT_COMMAND="highlight -O ansi -l {} || coderay {} || rougify {} || cat {}"
CMD=${FZF_PREVIEW_COMMAND:-$DEFAULT_COMMAND}
CMD=${CMD//{\}/"$(printf %q "$FILE")"}
eval "$CMD" 2> /dev/null | awk "{ \
if (NR == $CENTER) \
{ gsub(/\x1b[[0-9;]*m/, \"&$REVERSE\"); printf(\"$REVERSE%s\n$RESET\", \$0); } \
else printf(\"$RESET%s\n\", \$0); \
}"