2015-03-06 04:51:50 -05:00
#!/usr/bin/env bash
2015-03-06 19:08:41 -05:00
# fzf-tmux: starts fzf in a tmux pane
2015-03-08 13:50:43 -04:00
# usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]
2015-03-06 04:51:50 -05:00
2016-07-10 01:28:58 -04:00
fail() {
>&2 echo "$1"
exit 2
}
fzf="$(command -v fzf 2> /dev/null)" || fzf="$(dirname "$0")/fzf"
[[ -x "$fzf" ]] || fail 'fzf executable not found'
2015-03-06 04:51:50 -05:00
args=()
opt=""
2015-03-08 03:36:37 -04:00
skip=""
2015-03-08 13:50:43 -04:00
swap=""
close=""
2015-03-09 12:41:35 -04:00
term=""
2016-07-10 01:34:29 -04:00
[[ -n "$LINES" ]] && lines=$LINES || lines=$(tput lines)
2016-10-12 10:09:21 -04:00
[[ -n "$COLUMNS" ]] && columns=$COLUMNS || columns=$(tput cols)
2016-07-10 01:41:06 -04:00
help() {
>&2 echo 'usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]
Layout
-u [HEIGHT[%]] Split above (up)
-d [HEIGHT[%]] Split below (down)
-l [WIDTH[%]] Split left
-r [WIDTH[%]] Split right
(default: -d 50%)
'
exit
}
while [[ $# -gt 0 ]]; do
2015-03-06 04:51:50 -05:00
arg="$1"
2016-07-10 01:41:06 -04:00
shift
[[ -z "$skip" ]] && case "$arg" in
2015-03-09 12:41:35 -04:00
-)
term=1
;;
2016-07-10 01:41:06 -04:00
--help)
help
;;
--version)
echo "fzf-tmux (with fzf $("$fzf" --version))"
exit
;;
2015-03-08 13:50:43 -04:00
-w*|-h*|-d*|-u*|-r*|-l*)
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
2016-07-10 01:41:06 -04:00
if [[ ${#arg} -gt 2 ]]; then
2015-03-06 04:51:50 -05:00
size="${arg:2}"
else
2015-03-08 01:08:27 -05:00
if [[ "$1" =~ ^[0-9]+%?$ ]]; then
size="$1"
2015-03-08 03:39:30 -04:00
shift
2016-07-10 01:41:06 -04:00
else
2015-03-08 01:08:27 -05:00
continue
fi
2015-03-06 04:51:50 -05:00
fi
2015-03-08 13:50:43 -04:00
if [[ "$size" =~ %$ ]]; then
size=${size:0:((${#size}-1))}
2016-07-10 01:41:06 -04:00
if [[ -n "$swap" ]]; then
2015-03-08 13:50:43 -04:00
opt="$opt -p $(( 100 - size ))"
else
opt="$opt -p $size"
fi
else
2016-07-10 01:41:06 -04:00
if [[ -n "$swap" ]]; then
2015-03-09 12:41:35 -04:00
if [[ "$arg" =~ ^.l ]]; then
2016-10-12 10:09:21 -04:00
max=$columns
2015-03-09 12:41:35 -04:00
else
2016-04-24 00:32:33 -04:00
max=$lines
2015-03-09 12:41:35 -04:00
fi
2015-03-08 13:50:43 -04:00
size=$(( max - size ))
2016-07-10 01:41:06 -04:00
[[ $size -lt 0 ]] && size=0
2015-03-08 13:50:43 -04:00
opt="$opt -l $size"
else
opt="$opt -l $size"
fi
fi
2015-03-06 04:51:50 -05:00
;;
--)
# "--" can be used to separate fzf-tmux options from fzf options to
# avoid conflicts
2015-03-08 03:36:37 -04:00
skip=1
2016-07-10 01:41:06 -04:00
continue
2015-03-06 04:51:50 -05:00
;;
*)
2016-07-10 01:41:06 -04:00
args+=("$arg")
2015-03-06 04:51:50 -05:00
;;
esac
2016-07-10 01:41:06 -04:00
[[ -n "$skip" ]] && args+=("$arg")
2015-03-06 04:51:50 -05:00
done
2016-10-12 21:42:26 -04:00
if [[ -z "$TMUX" || "$opt" =~ ^-h && "$columns" -le 40 || ! "$opt" =~ ^-h && "$lines" -le 15 ]]; then
2016-07-10 01:34:29 -04:00
"$fzf" "${args[@]}"
2015-03-06 04:51:50 -05:00
exit $?
fi
2017-01-07 11:30:31 -05:00
# --height option is not allowed
args+=("--no-height")
2015-12-07 20:45:22 -05:00
# Handle zoomed tmux pane by moving it to a temp window
if tmux list-panes -F '#F' | grep -q Z; then
zoomed=1
original_window=$(tmux display-message -p "#{window_id}")
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'")
tmux swap-pane -t $tmp_window \; select-window -t $tmp_window
fi
2015-03-06 04:51:50 -05:00
set -e
# Clean up named pipes on exit
id=$RANDOM
2015-10-05 00:01:09 -04:00
argsf="${TMPDIR:-/tmp}/fzf-args-$id"
fifo1="${TMPDIR:-/tmp}/fzf-fifo1-$id"
fifo2="${TMPDIR:-/tmp}/fzf-fifo2-$id"
fifo3="${TMPDIR:-/tmp}/fzf-fifo3-$id"
2015-03-06 04:51:50 -05:00
cleanup() {
2015-04-18 03:24:57 -04:00
rm -f $argsf $fifo1 $fifo2 $fifo3
2015-12-07 20:45:22 -05:00
# Remove temp window if we were zoomed
2016-07-10 01:34:29 -04:00
if [[ -n "$zoomed" ]]; then
2015-12-07 20:45:22 -05:00
tmux swap-pane -t $original_window \; \
select-window -t $original_window \; \
kill-window -t $tmp_window \; \
resize-pane -Z
fi
2015-03-06 04:51:50 -05:00
}
trap cleanup EXIT SIGINT SIGTERM
2015-07-28 01:17:04 -04:00
envs="env TERM=$TERM "
2016-07-10 01:34:29 -04:00
[[ -n "$FZF_DEFAULT_OPTS" ]] && envs="$envs FZF_DEFAULT_OPTS=$(printf %q "$FZF_DEFAULT_OPTS")"
[[ -n "$FZF_DEFAULT_COMMAND" ]] && envs="$envs FZF_DEFAULT_COMMAND=$(printf %q "$FZF_DEFAULT_COMMAND")"
2015-03-06 19:29:16 -05:00
2016-01-06 10:32:38 -05:00
mkfifo -m o+w $fifo2
mkfifo -m o+w $fifo3
2015-04-20 09:42:02 -04:00
# Build arguments to fzf
opts=""
for arg in "${args[@]}"; do
2016-05-15 04:07:34 -04:00
arg="${arg//\\/\\\\}"
2015-09-07 05:40:39 -04:00
arg="${arg//\"/\\\"}"
arg="${arg//\`/\\\`}"
2016-06-07 12:27:22 -04:00
arg="${arg//$/\\$}"
2015-09-07 05:40:39 -04:00
opts="$opts \"$arg\""
2015-04-20 09:42:02 -04:00
done
2016-07-10 01:34:29 -04:00
if [[ -n "$term" ]] || [[ -t 0 ]]; then
2016-06-02 23:09:31 -04:00
cat <<< "\"$fzf\" $opts > $fifo2; echo \$? > $fifo3 $close" > $argsf
2016-09-06 14:03:07 -04:00
TMUX=$(echo $TMUX | cut -d , -f 1,2) tmux set-window-option synchronize-panes off \;\
2016-03-15 08:18:15 -04:00
set-window-option remain-on-exit off \;\
2016-06-02 23:02:21 -04:00
split-window $opt "cd $(printf %q "$PWD");$envs bash $argsf" $swap \
2016-03-15 08:21:21 -04:00
> /dev/null 2>&1
2015-03-06 04:51:50 -05:00
else
mkfifo $fifo1
2016-06-02 23:09:31 -04:00
cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; echo \$? > $fifo3 $close" > $argsf
2016-09-06 14:03:07 -04:00
TMUX=$(echo $TMUX | cut -d , -f 1,2) tmux set-window-option synchronize-panes off \;\
2016-03-15 08:18:15 -04:00
set-window-option remain-on-exit off \;\
2016-06-02 23:02:21 -04:00
split-window $opt "$envs bash $argsf" $swap \
2016-03-15 08:21:21 -04:00
> /dev/null 2>&1
2015-03-06 04:51:50 -05:00
cat <&0 > $fifo1 &
fi
cat $fifo2
2015-09-17 21:26:04 -04:00
exit "$(cat $fifo3)"
2015-03-06 04:51:50 -05:00