fzf/bin/fzf-tmux

163 lines
4.0 KiB
Plaintext
Raw Normal View History

2015-03-06 09:51:50 +00:00
#!/usr/bin/env bash
# fzf-tmux: starts fzf in a tmux pane
# usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]
2015-03-06 09:51:50 +00: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 09:51:50 +00:00
args=()
opt=""
2015-03-08 07:36:37 +00:00
skip=""
swap=""
close=""
term=""
[ -n "$LINES" ] && lines=$LINES || lines=$(tput lines)
2015-03-06 09:51:50 +00:00
while [ $# -gt 0 ]; do
arg="$1"
case "$arg" in
-)
term=1
;;
-w*|-h*|-d*|-u*|-r*|-l*)
2015-03-08 07:36:37 +00:00
if [ -n "$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
2015-03-06 09:51:50 +00:00
if [ ${#arg} -gt 2 ]; then
size="${arg:2}"
else
shift
if [[ "$1" =~ ^[0-9]+%?$ ]]; then
size="$1"
else
[ -n "$1" -a "$1" != "--" ] && args+=("$1")
shift
continue
fi
2015-03-06 09:51:50 +00:00
fi
if [[ "$size" =~ %$ ]]; then
size=${size:0:((${#size}-1))}
if [ -n "$swap" ]; then
opt="$opt -p $(( 100 - size ))"
else
opt="$opt -p $size"
fi
else
if [ -n "$swap" ]; then
if [[ "$arg" =~ ^.l ]]; then
[ -n "$COLUMNS" ] && max=$COLUMNS || max=$(tput cols)
else
max=$lines
fi
size=$(( max - size ))
[ $size -lt 0 ] && size=0
opt="$opt -l $size"
else
opt="$opt -l $size"
fi
fi
2015-03-06 09:51:50 +00:00
;;
--)
# "--" can be used to separate fzf-tmux options from fzf options to
# avoid conflicts
2015-03-08 07:36:37 +00:00
skip=1
2015-03-06 09:51:50 +00:00
;;
*)
args+=("$1")
;;
esac
shift
done
if ! [ -n "$TMUX" -a "$lines" -gt 15 ]; then
2015-03-06 09:51:50 +00:00
fzf "${args[@]}"
exit $?
fi
2015-12-08 01:45:22 +00: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 09:51:50 +00:00
set -e
# Clean up named pipes on exit
id=$RANDOM
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 09:51:50 +00:00
cleanup() {
rm -f $argsf $fifo1 $fifo2 $fifo3
2015-12-08 01:45:22 +00:00
# Remove temp window if we were zoomed
if [ -n "$zoomed" ]; then
tmux swap-pane -t $original_window \; \
select-window -t $original_window \; \
kill-window -t $tmp_window \; \
resize-pane -Z
fi
2015-03-06 09:51:50 +00:00
}
trap cleanup EXIT SIGINT SIGTERM
envs="env TERM=$TERM "
[ -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")"
mkfifo -m o+w $fifo2
mkfifo -m o+w $fifo3
2015-04-20 13:42:02 +00:00
# Build arguments to fzf
opts=""
for arg in "${args[@]}"; do
arg="${arg//\\/\\\\}"
arg="${arg//\"/\\\"}"
arg="${arg//\`/\\\`}"
arg="${arg//$/\\$}"
opts="$opts \"$arg\""
2015-04-20 13:42:02 +00:00
done
if [ -n "$term" -o -t 0 ]; then
cat <<< "\"$fzf\" $opts > $fifo2; echo \$? > $fifo3 $close" > $argsf
2016-03-15 12:18:15 +00:00
tmux set-window-option synchronize-panes off \;\
set-window-option remain-on-exit off \;\
split-window $opt "cd $(printf %q "$PWD");$envs bash $argsf" $swap \
2016-03-15 12:21:21 +00:00
> /dev/null 2>&1
2015-03-06 09:51:50 +00:00
else
mkfifo $fifo1
cat <<< "\"$fzf\" $opts < $fifo1 > $fifo2; echo \$? > $fifo3 $close" > $argsf
2016-03-15 12:18:15 +00:00
tmux set-window-option synchronize-panes off \;\
set-window-option remain-on-exit off \;\
split-window $opt "$envs bash $argsf" $swap \
2016-03-15 12:21:21 +00:00
> /dev/null 2>&1
2015-03-06 09:51:50 +00:00
cat <&0 > $fifo1 &
fi
cat $fifo2
exit "$(cat $fifo3)"
2015-03-06 09:51:50 +00:00