Debian Codegraph @

bin/fzf-tmux

raw
1 #!/usr/bin/env bash
2 # fzf-tmux: starts fzf in a tmux pane
3 # usage: fzf-tmux [LAYOUT OPTIONS] [--] [FZF OPTIONS]
4
5 fail() {
6 >&2 echo "$1"
7 exit 2
8 }
9
10 fzf="$(command which fzf)" || fzf="$(dirname "$0")/fzf"
11 [[ -x $fzf ]] || fail 'fzf executable not found'
12
13 args=()
14 opt=""
15 skip=""
16 swap=""
17 close=""
18 term=""
19 [[ -n $LINES ]] && lines=$LINES || lines=$(tput lines) || lines=$(tmux display-message -p "#{pane_height}")
20 [[ -n $COLUMNS ]] && columns=$COLUMNS || columns=$(tput cols) || columns=$(tmux display-message -p "#{pane_width}")
21
22 tmux_version=$(tmux -V | sed 's/[^0-9.]//g')
23 tmux_32=$(awk '{print ($1 >= 3.2)}' <<< "$tmux_version" 2> /dev/null || bc -l <<< "$tmux_version >= 3.2")
24
25 help() {
26 >&2 echo 'usage: fzf-tmux [LAYOUT OPTIONS] [--] [FZF OPTIONS]
27
28 LAYOUT OPTIONS:
29 (default layout: -d 50%)
30
31 Popup window (requires tmux 3.2 or above):
32 -p [WIDTH[%][,HEIGHT[%]]] (default: 50%)
33 -w WIDTH[%]
34 -h HEIGHT[%]
35 -x COL
36 -y ROW
37
38 Split pane:
39 -u [HEIGHT[%]] Split above (up)
40 -d [HEIGHT[%]] Split below (down)
41 -l [WIDTH[%]] Split left
42 -r [WIDTH[%]] Split right
43 '
44 exit
45 }
46
47 while [[ $# -gt 0 ]]; do
48 arg="$1"
49 shift
50 [[ -z $skip ]] && case "$arg" in
51 -)
52 term=1
53 ;;
54 --help)
55 help
56 ;;
57 --version)
58 echo "fzf-tmux (with fzf $("$fzf" --version))"
59 exit
60 ;;
61 -p* | -w* | -h* | -x* | -y* | -d* | -u* | -r* | -l*)
62 if [[ $arg =~ ^-[pwhxy] ]]; then
63 [[ $opt =~ "-E" ]] || opt="-E"
64 elif [[ $arg =~ ^.[lr] ]]; then
65 opt="-h"
66 if [[ $arg =~ ^.l ]]; then
67 opt="$opt -d"
68 swap="; swap-pane -D ; select-pane -L"
69 close="; tmux swap-pane -D"
70 fi
71 else
72 opt=""
73 if [[ $arg =~ ^.u ]]; then
74 opt="$opt -d"
75 swap="; swap-pane -D ; select-pane -U"
76 close="; tmux swap-pane -D"
77 fi
78 fi
79 if [[ ${#arg} -gt 2 ]]; then
80 size="${arg:2}"
81 else
82 if [[ $1 =~ ^[0-9%,]+$ ]] || [[ $1 =~ ^[A-Z]$ ]]; then
83 size="$1"
84 shift
85 else
86 continue
87 fi
88 fi
89
90 if [[ $arg =~ ^-p ]]; then
91 if [[ -n $size ]]; then
92 w=${size%%,*}
93 h=${size##*,}
94 opt="$opt -w$w -h$h"
95 fi
96 elif [[ $arg =~ ^-[whxy] ]]; then
97 opt="$opt ${arg:0:2}$size"
98 elif [[ $size =~ %$ ]]; then
99 size=${size:0:${#size}-1}
100 if [[ $tmux_32 == 1 ]]; then
101 if [[ -n $swap ]]; then
102 opt="$opt -l $((100 - size))%"
103 else
104 opt="$opt -l $size%"
105 fi
106 else
107 if [[ -n $swap ]]; then
108 opt="$opt -p $((100 - size))"
109 else
110 opt="$opt -p $size"
111 fi
112 fi
113 else
114 if [[ -n $swap ]]; then
115 if [[ $arg =~ ^.l ]]; then
116 max=$columns
117 else
118 max=$lines
119 fi
120 size=$((max - size))
121 [[ $size -lt 0 ]] && size=0
122 opt="$opt -l $size"
123 else
124 opt="$opt -l $size"
125 fi
126 fi
127 ;;
128 --)
129 # "--" can be used to separate fzf-tmux options from fzf options to
130 # avoid conflicts
131 skip=1
132 continue
133 ;;
134 *)
135 args+=("$arg")
136 ;;
137 esac
138 [[ -n $skip ]] && args+=("$arg")
139 done
140
141 if [[ -z $TMUX ]]; then
142 "$fzf" "${args[@]}"
143 exit $?
144 fi
145
146 # * --height option is not allowed
147 # * CTRL-Z is also disabled
148 # * fzf-tmux script is not compatible with --tmux option in fzf 0.53.0 or later
149 args=("${args[@]}" "--no-height" "--bind=ctrl-z:ignore" "--no-tmux")
150
151 # Handle zoomed tmux pane without popup options by moving it to a temp window
152 if [[ ! $opt =~ "-E" ]] && tmux list-panes -F '#F' | grep -q Z; then
153 zoomed_without_popup=1
154 original_window=$(tmux display-message -p "#{window_id}")
155 tmp_window=$(tmux new-window -d -P -F "#{window_id}" "bash -c 'while :; do for c in \\| / - '\\;' do sleep 0.2; printf \"\\r\$c fzf-tmux is running\\r\"; done; done'")
156 tmux swap-pane -t $tmp_window \; select-window -t $tmp_window
157 fi
158
159 set -e
160
161 # Clean up named pipes on exit
162 tmpdir=$(mktemp -d "${TMPDIR:-/tmp}/fzf-tmux-XXXXXX")
163 argsf="$tmpdir/args"
164 fifo1="$tmpdir/fifo1"
165 fifo2="$tmpdir/fifo2"
166 fifo3="$tmpdir/fifo3"
167 if tmux_win_opts=$(tmux show-options -p remain-on-exit \; show-options -p synchronize-panes 2> /dev/null); then
168 tmux_win_opts=($(sed '/ off/d; s/synchronize-panes/set-option -p synchronize-panes/; s/remain-on-exit/set-option -p remain-on-exit/; s/$/ \\;/' <<< "$tmux_win_opts"))
169 tmux_off_opts='; set-option -p synchronize-panes off ; set-option -p remain-on-exit off'
170 else
171 tmux_win_opts=($(tmux show-window-options remain-on-exit \; show-window-options synchronize-panes | sed '/ off/d; s/^/set-window-option /; s/$/ \\;/'))
172 tmux_off_opts='; set-window-option synchronize-panes off ; set-window-option remain-on-exit off'
173 fi
174 cleanup() {
175 \rm -rf "$tmpdir"
176 # Restore tmux window options
177 if [[ ${#tmux_win_opts[@]} -gt 1 ]]; then
178 eval "tmux ${tmux_win_opts[*]}"
179 fi
180
181 # Remove temp window if we were zoomed without popup options
182 if [[ -n $zoomed_without_popup ]]; then
183 tmux display-message -p "#{window_id}" > /dev/null
184 tmux swap-pane -t $original_window \; \
185 select-window -t $original_window \; \
186 kill-window -t $tmp_window \; \
187 resize-pane -Z
188 fi
189
190 if [[ $# -gt 0 ]]; then
191 trap - EXIT
192 exit 130
193 fi
194 }
195 trap 'cleanup 1' SIGUSR1
196 trap 'cleanup' EXIT
197
198 envs="export TERM=$(printf %q "$TERM") "
199 if [[ $opt =~ "-E" ]]; then
200 if [[ $tmux_version == 3.2 ]]; then
201 FZF_DEFAULT_OPTS="--margin 0,1 $FZF_DEFAULT_OPTS"
202 elif [[ $tmux_32 == 1 ]]; then
203 FZF_DEFAULT_OPTS="--border $FZF_DEFAULT_OPTS"
204 opt="-B $opt"
205 else
206 echo "fzf-tmux: tmux 3.2 or above is required for popup mode" >&2
207 exit 2
208 fi
209 fi
210 envs="$envs FZF_DEFAULT_COMMAND=$(printf %q "$FZF_DEFAULT_COMMAND")"
211 envs="$envs FZF_DEFAULT_OPTS=$(printf %q "$FZF_DEFAULT_OPTS")"
212 envs="$envs FZF_DEFAULT_OPTS_FILE=$(printf %q "$FZF_DEFAULT_OPTS_FILE")"
213 [[ -n $RUNEWIDTH_EASTASIAN ]] && envs="$envs RUNEWIDTH_EASTASIAN=$(printf %q "$RUNEWIDTH_EASTASIAN")"
214 [[ -n $BAT_THEME ]] && envs="$envs BAT_THEME=$(printf %q "$BAT_THEME")"
215 echo "$envs;" > "$argsf"
216
217 # Build arguments to fzf
218 opts=$(printf "%q " "${args[@]}")
219
220 pppid=$$
221 echo -n "trap 'kill -SIGUSR1 -$pppid' EXIT SIGINT SIGTERM;" >> $argsf
222 close="; trap - EXIT SIGINT SIGTERM $close"
223
224 export TMUX=$(cut -d , -f 1,2 <<< "$TMUX")
225 mkfifo -m o+w $fifo2
226 if [[ $opt =~ "-E" ]]; then
227 cat $fifo2 &
228 if [[ -n $term ]] || [[ -t 0 ]]; then
229 cat <<< "\"$fzf\" $opts > $fifo2; out=\$? $close; exit \$out" >> $argsf
230 else
231 mkfifo $fifo1
232 cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; out=\$? $close; exit \$out" >> $argsf
233 cat <&0 > $fifo1 &
234 fi
235
236 tmux popup -d "$PWD" $opt "bash $argsf" > /dev/null 2>&1
237 exit $?
238 fi
239
240 mkfifo -m o+w $fifo3
241 if [[ -n $term ]] || [[ -t 0 ]]; then
242 cat <<< "\"$fzf\" $opts > $fifo2; echo \$? > $fifo3 $close" >> $argsf
243 else
244 mkfifo $fifo1
245 cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; echo \$? > $fifo3 $close" >> $argsf
246 cat <&0 > $fifo1 &
247 fi
248 tmux \
249 split-window -c "$PWD" $opt "bash -c 'exec -a fzf bash $argsf'" $swap \
250 $tmux_off_opts \
251 > /dev/null 2>&1 || {
252 "$fzf" "${args[@]}"
253 exit $?
254 }
255 cat $fifo2
256 exit "$(cat $fifo3)"