-
Notifications
You must be signed in to change notification settings - Fork 0
/
re
executable file
·132 lines (115 loc) · 2.76 KB
/
re
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/zsh
FZF_DEFAULT_OPTS=
FZF_DEFAULT_OPTS+=" --bind 'ctrl-i:toggle-out'"
FZF_DEFAULT_OPTS+=" --bind 'down:preview-down,up:preview-up'"
FZF_DEFAULT_OPTS+=" --bind 'ctrl-y:execute(printf $PWD/{} | pbcopy)'"
FZF_DEFAULT_OPTS+=" --bind '?:toggle-preview'"
is_git_repo() {
git rev-parse --is-inside-work-tree &>/dev/null
return $?
}
get_git_branch() {
is_git_repo && git rev-parse --abbrev-ref HEAD
}
get_lines() {
tput lines
}
get_cursor_pos() {
# based on a script from http://invisible-island.net/xterm/xterm.faq.html
exec </dev/tty
oldstty=$(stty -g)
stty raw -echo min 0
echo -en "\033[6n" >/dev/tty
IFS=';' read -r -d R -a pos
stty $oldstty
# change from one-based to zero based so they work with: tput cup $row $col
echo $((${pos[0]:2} - 1)) # strip off the esc-[
}
branch="$(get_git_branch)"
viewer=$(cat <<SHELL
if file {} | grep -q text; then
# pygmentize -g {} 2>/dev/null || less {}
highlight -O ansi -l {} 2>/dev/null || less {}
else
file {}
fi
SHELL
)
diff_files() {
if [[ $branch == 'master' ]]; then
finder
return $?
fi
{
echo -e "* $branch"
echo " ================"
git diff \
--name-only \
"origin/master...$branch"
} |
fzf \
--header-lines=2 \
--ansi \
--preview="git diff --color origin/master {}" \
--preview-window="$size" \
--bind "enter:execute-multi(vim -p {} </dev/tty >/dev/tty)" "$@"
}
finder() {
fzf \
--ansi \
--preview="$viewer" \
--preview-window="$size" \
--bind "enter:execute-multi(vim -p {})" "$@"
}
do_help() {
cat <<HELP >&2
$ re [-f]
HELP
}
main() {
local is_finder=false
local -a opts args
while (( $# > 0 ))
do
case "$1" in
-h|--help)
do_help
return 1
;;
-f)
is_finder=true
;;
-*)
opts+=("$1")
;;
"")
;;
*)
args+=("$1")
;;
esac
shift
done
if (( COLUMNS > LINES )); then
size="right:60%"
else
size="up:70%"
fi
if (( $(get_cursor_pos) < ($(get_lines)/2) )); then
opts+=("--reverse")
fi
if (( ${#args[@]} > 0 )); then
pt -l ${args[0]} 2>/dev/null \
| fzf \
--preview "grep -n --color=always ${args[0]} {}" \
--preview-window="$size" \
--bind "enter:execute-multi(vim -p {} </dev/tty >/dev/tty)" "$@"
else
if ! $is_finder && is_git_repo; then
diff_files "${opts[@]}"
else
finder "${opts[@]}"
fi
fi
}
main ${1:+"$@"}