-
Notifications
You must be signed in to change notification settings - Fork 9
/
fzy-tmux
executable file
·153 lines (142 loc) · 3.27 KB
/
fzy-tmux
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#! /usr/bin/env zsh
# Starts fzy in a tmux pane:
# fzy-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZY OPTIONS]
opt=''
swap=''
close=''
args=()
term=false
skip=false
lines=${LINES}
if [[ -z ${lines} ]] ; then
lines=$(tput lines)
fi
while [[ $# -gt 0 ]] ; do
local arg=$1
case "${arg}" in
-)
term=true
;;
-w*|-h*|-d*|-u*|-r*|-l*)
if ${skip} ; then
args+=( "$1" )
shift
continue
fi
if [[ ${arg} =~ ^.[lrw] ]] ; then
opt='-h'
if [[ ${arg} =~ ^.l ]] ; then
opt="${opt} -d"
swap='; swap-pane -D ; select-pane -L'
close='; tmux swap-pane -D'
fi
else
opt=''
if [[ ${arg} =~ ^.u ]] ; then
opt="${opt} -d"
swap='; swap-pane -D ; select-pane -U'
close='; tmux swap-pane -D'
fi
fi
if [[ ${#arg[@]} -gt 2 ]] ; then
size=${arg:2}
else
shift
if [[ $1 =~ ^[0-9]+%?$ ]] ; then
size=$1
else
if [[ -n $1 && $1 != -- ]] ; then
args+=( "$1" )
fi
shift
continue
fi
fi
if [[ ${size} =~ %$ ]] ; then
size=${size:0:-1}
if [[ -n ${swap} ]] ; then
opt="${opt} -p $(( 100 - size ))"
else
opt="${opt} -p ${size}"
fi
else
if [[ -n ${swap} ]] ; then
local max
if [[ ${arg} =~ ^.l ]] ; then
max=${COLUMNS}
if [[ -z ${COLUMNS} ]] ; then
max=$(tput cols)
fi
else
max=${lines}
fi
size=$(( max - size ))
if [[ ${size} -lt 0 ]] ; then
size=0
fi
fi
opt="${opt} -l ${size}"
fi
;;
--)
skip=true
;;
esac
shift
done
if [[ -z ${TMUX} || ${lines} -le 15 ]] ; then
exec fzy "${args[@]}"
fi
zoomed=false
if tmux list-panes -F '#F' | grep -q Z ; then
zoomed=true
original_window=$(tmux display-message -p '#{window_id}')
tmp_window=$(tmux new-window -d -P -F '#{window_id}'
"zsh -c 'while :; do for c in \\| / - \\\\; do sleep 0.2; printf
\"\\r%s fzy-tmux is running\\r\" \"\${c}\"; done; done'")
tmux swap-pane -t ${tmp_window} \; select-window -t ${tmp_window}
fi
set -e
id="${RANDOM}:$$"
argsf="${TMPDIR:-/tmp}/fzy-argsf-${id}"
fifo1="${TMPDIR:-/tmp}/fzy-fifo1-${id}"
fifo2="${TMPDIR:-/tmp}/fzy-fifo2-${id}"
fifo3="${TMPDIR:-/tmp}/fzy-fifo3-${id}"
cleanup () {
rm -f "${argsf}" "${fifo1}" "${fifo2}" "${fifo3}"
if ${zoomed} ; then
tmux swap-pane -t ${original_window} \; \
select-window -t ${original_window} \; \
kill-window -t ${tmp_window} \; \
resize-pane -Z
fi
}
trap cleanup EXIT INT TERM
mkfifo -m o+w "${fifo2}"
mkfifo -m o+w "${fifo3}"
opts=""
for arg in "${args[@]}" ; do
arg=${arg//\\/\\\\}
arg=${arg//\"/\\\"}
arg=${arg//\`/\\\`}
opts="${opts} \"${arg}\""
done
if ${term} || [[ -t 0 ]] ; then
cat > "${argsf}" <<< "fzy ${opts} -l \${LINES} > '${fifo2}' ; echo \? > '${fifo3}' ${close}"
tmux \
set-window-option synchronize-panes off \; \
set-window-option remain-on-exit off \; \
split-window ${opt} "cd $(printf %q "${PWD}"); zsh '${argsf}'" ${swap} \
> /dev/null 2>&1
else
mkfifo "${fifo1}"
cat > "${argsf}" <<< "fzy ${opts} -l \${LINES} < '${fifo1}' > '${fifo2}' ; echo \? > '${fifo3}' ${close}"
tmux \
set-window-option synchronize-panes off \; \
set-window-option remain-on-exit off \; \
split-window ${opt} "zsh '${argsf}'" ${swap} \
> /dev/null 2>&1
cat <&0 > "${fifo1}" &
fi
cat "${fifo2}"
exit "$(cat "${fifo3}")"