mirror of
https://github.com/Llewellynvdm/fzf.git
synced 2024-12-23 19:39:07 +00:00
137 lines
3.0 KiB
Bash
Executable File
137 lines
3.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# fzf-tmux: starts fzf in a tmux pane
|
|
# usage: fzf-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [FZF OPTIONS]
|
|
|
|
args=()
|
|
opt=""
|
|
skip=""
|
|
swap=""
|
|
close=""
|
|
term=""
|
|
while [ $# -gt 0 ]; do
|
|
arg="$1"
|
|
case "$arg" in
|
|
-)
|
|
term=1
|
|
;;
|
|
-w*|-h*|-d*|-u*|-r*|-l*)
|
|
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
|
|
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
|
|
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
|
|
[ -n "$LINES" ] && max=$LINES || max=$(tput lines)
|
|
fi
|
|
size=$(( max - size ))
|
|
[ $size -lt 0 ] && size=0
|
|
opt="$opt -l $size"
|
|
else
|
|
opt="$opt -l $size"
|
|
fi
|
|
fi
|
|
;;
|
|
--)
|
|
# "--" can be used to separate fzf-tmux options from fzf options to
|
|
# avoid conflicts
|
|
skip=1
|
|
;;
|
|
*)
|
|
args+=("$1")
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
if [ -z "$TMUX_PANE" ]; then
|
|
fzf "${args[@]}"
|
|
exit $?
|
|
fi
|
|
|
|
set -e
|
|
|
|
# Clean up named pipes on exit
|
|
id=$RANDOM
|
|
argsf=/tmp/fzf-args-$id
|
|
fifo1=/tmp/fzf-fifo1-$id
|
|
fifo2=/tmp/fzf-fifo2-$id
|
|
fifo3=/tmp/fzf-fifo3-$id
|
|
cleanup() {
|
|
rm -f $argsf $fifo1 $fifo2 $fifo3
|
|
}
|
|
trap cleanup EXIT SIGINT SIGTERM
|
|
|
|
fail() {
|
|
>&2 echo "$1"
|
|
exit 1
|
|
}
|
|
fzf="$(which fzf 2> /dev/null)" || fzf="$(dirname "$0")/fzf"
|
|
[ -x "$fzf" ] || fail "fzf executable not found"
|
|
|
|
envs="env "
|
|
[ -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 $fifo2
|
|
mkfifo $fifo3
|
|
|
|
# Build arguments to fzf
|
|
opts=""
|
|
for arg in "${args[@]}"; do
|
|
opts="$opts \"${arg//\"/\\\"}\""
|
|
done
|
|
|
|
if [ -n "$term" -o -t 0 ]; then
|
|
cat <<< "$fzf $opts > $fifo2; echo \$? > $fifo3 $close" > $argsf
|
|
tmux set-window-option -q synchronize-panes off \;\
|
|
split-window $opt "cd $(printf %q "$PWD");$envs bash $argsf" $swap
|
|
else
|
|
mkfifo $fifo1
|
|
cat <<< "$fzf $opts < $fifo1 > $fifo2; echo \$? > $fifo3 $close" > $argsf
|
|
tmux set-window-option -q synchronize-panes off \;\
|
|
split-window $opt "$envs bash $argsf" $swap
|
|
cat <&0 > $fifo1 &
|
|
fi
|
|
cat $fifo2
|
|
[ "$(cat $fifo3)" = '0' ]
|
|
|