2015-03-06 12:51:50 +03:00
|
|
|
#!/usr/bin/env bash
|
2015-03-07 03:08:41 +03:00
|
|
|
# fzf-tmux: starts fzf in a tmux pane
|
2015-03-08 20:50:43 +03:00
|
|
|
# usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]
|
2015-03-06 12:51:50 +03:00
|
|
|
|
|
|
|
args=()
|
|
|
|
opt=""
|
2015-03-08 10:36:37 +03:00
|
|
|
skip=""
|
2015-03-08 20:50:43 +03:00
|
|
|
swap=""
|
|
|
|
close=""
|
2015-03-09 19:41:35 +03:00
|
|
|
term=""
|
2015-03-06 12:51:50 +03:00
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
arg="$1"
|
|
|
|
case "$arg" in
|
2015-03-09 19:41:35 +03:00
|
|
|
-)
|
|
|
|
term=1
|
|
|
|
;;
|
2015-03-08 20:50:43 +03:00
|
|
|
-w*|-h*|-d*|-u*|-r*|-l*)
|
2015-03-08 10:36:37 +03:00
|
|
|
if [ -n "$skip" ]; then
|
|
|
|
args+=("$1")
|
|
|
|
shift
|
|
|
|
continue
|
|
|
|
fi
|
2015-03-08 20:50:43 +03:00
|
|
|
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 12:51:50 +03:00
|
|
|
if [ ${#arg} -gt 2 ]; then
|
|
|
|
size="${arg:2}"
|
|
|
|
else
|
|
|
|
shift
|
2015-03-08 09:08:27 +03:00
|
|
|
if [[ "$1" =~ ^[0-9]+%?$ ]]; then
|
|
|
|
size="$1"
|
|
|
|
else
|
2015-03-08 10:39:30 +03:00
|
|
|
[ -n "$1" -a "$1" != "--" ] && args+=("$1")
|
|
|
|
shift
|
2015-03-08 09:08:27 +03:00
|
|
|
continue
|
|
|
|
fi
|
2015-03-06 12:51:50 +03:00
|
|
|
fi
|
2015-03-08 20:50:43 +03:00
|
|
|
|
|
|
|
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
|
2015-03-09 19:41:35 +03:00
|
|
|
if [[ "$arg" =~ ^.l ]]; then
|
|
|
|
[ -n "$COLUMNS" ] && max=$COLUMNS || max=$(tput cols)
|
|
|
|
else
|
|
|
|
[ -n "$LINES" ] && max=$LINES || max=$(tput lines)
|
|
|
|
fi
|
2015-03-08 20:50:43 +03:00
|
|
|
size=$(( max - size ))
|
|
|
|
[ $size -lt 0 ] && size=0
|
|
|
|
opt="$opt -l $size"
|
|
|
|
else
|
|
|
|
opt="$opt -l $size"
|
|
|
|
fi
|
|
|
|
fi
|
2015-03-06 12:51:50 +03:00
|
|
|
;;
|
|
|
|
--)
|
|
|
|
# "--" can be used to separate fzf-tmux options from fzf options to
|
|
|
|
# avoid conflicts
|
2015-03-08 10:36:37 +03:00
|
|
|
skip=1
|
2015-03-06 12:51:50 +03:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
args+=("$1")
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -z "$TMUX_PANE" ]; then
|
|
|
|
fzf "${args[@]}"
|
|
|
|
exit $?
|
|
|
|
fi
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# Clean up named pipes on exit
|
|
|
|
id=$RANDOM
|
2015-04-18 10:24:57 +03:00
|
|
|
argsf=/tmp/fzf-args-$id
|
2015-03-06 12:51:50 +03:00
|
|
|
fifo1=/tmp/fzf-fifo1-$id
|
|
|
|
fifo2=/tmp/fzf-fifo2-$id
|
|
|
|
fifo3=/tmp/fzf-fifo3-$id
|
|
|
|
cleanup() {
|
2015-04-18 10:24:57 +03:00
|
|
|
rm -f $argsf $fifo1 $fifo2 $fifo3
|
2015-03-06 12:51:50 +03:00
|
|
|
}
|
|
|
|
trap cleanup EXIT SIGINT SIGTERM
|
|
|
|
|
2015-04-18 10:24:57 +03:00
|
|
|
# Build arguments to fzf
|
2015-04-18 10:27:40 +03:00
|
|
|
str=""
|
|
|
|
for arg in "${args[@]}"; do
|
|
|
|
str="$str \"${arg//\"/\\\"}\""
|
|
|
|
done
|
|
|
|
cat <<< "$str" > $argsf
|
2015-04-18 10:24:57 +03:00
|
|
|
|
2015-03-07 03:29:16 +03:00
|
|
|
fail() {
|
|
|
|
>&2 echo "$1"
|
|
|
|
exit 1
|
|
|
|
}
|
2015-04-11 19:57:05 +03:00
|
|
|
fzf="$(which fzf 2> /dev/null)" || fzf="$(dirname "$0")/fzf"
|
|
|
|
[ -x "$fzf" ] || fail "fzf executable not found"
|
|
|
|
|
2015-03-13 16:45:28 +03:00
|
|
|
envs=""
|
|
|
|
[ -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-07 03:29:16 +03:00
|
|
|
|
2015-03-06 12:51:50 +03:00
|
|
|
mkfifo $fifo2
|
|
|
|
mkfifo $fifo3
|
2015-03-09 19:41:35 +03:00
|
|
|
if [ -n "$term" -o -t 0 ]; then
|
2015-03-08 20:50:43 +03:00
|
|
|
tmux set-window-option -q synchronize-panes off \;\
|
2015-04-20 10:23:15 +03:00
|
|
|
split-window $opt "cd $(printf %q "$PWD");$envs"' bash -c "'$fzf' $(cat '$argsf') > '$fifo2'; echo \$? > '$fifo3' '"$close"'"' $swap
|
2015-03-06 12:51:50 +03:00
|
|
|
else
|
|
|
|
mkfifo $fifo1
|
2015-03-08 20:50:43 +03:00
|
|
|
tmux set-window-option -q synchronize-panes off \;\
|
2015-04-20 10:23:15 +03:00
|
|
|
split-window $opt "$envs"' bash -c "'$fzf' $(cat '$argsf') < '$fifo1' > '$fifo2'; echo \$? > '$fifo3' '"$close"'"' $swap
|
2015-03-06 12:51:50 +03:00
|
|
|
cat <&0 > $fifo1 &
|
|
|
|
fi
|
|
|
|
cat $fifo2
|
|
|
|
[ "$(cat $fifo3)" = '0' ]
|
|
|
|
|