2015-03-06 09:51:50 +00:00
|
|
|
#!/usr/bin/env bash
|
2015-03-07 00:08:41 +00:00
|
|
|
# fzf-tmux: starts fzf in a tmux pane
|
2015-03-08 06:08:27 +00:00
|
|
|
# usage: fzf-tmux [-h [HEIGHT[%]]] [-w [WIDTH[%]]] [--] [FZF OPTIONS]
|
2015-03-06 09:51:50 +00:00
|
|
|
|
|
|
|
args=()
|
|
|
|
opt=""
|
2015-03-08 07:36:37 +00:00
|
|
|
skip=""
|
2015-03-06 09:51:50 +00:00
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
arg="$1"
|
|
|
|
case "$arg" in
|
|
|
|
-w*|-h*)
|
2015-03-08 07:36:37 +00:00
|
|
|
if [ -n "$skip" ]; then
|
|
|
|
args+=("$1")
|
|
|
|
shift
|
|
|
|
continue
|
|
|
|
fi
|
2015-03-08 06:08:27 +00:00
|
|
|
[[ "$arg" =~ ^-w ]] && opt="-h" || opt=""
|
2015-03-06 09:51:50 +00:00
|
|
|
if [ ${#arg} -gt 2 ]; then
|
|
|
|
size="${arg:2}"
|
|
|
|
else
|
|
|
|
shift
|
2015-03-08 06:08:27 +00:00
|
|
|
if [[ "$1" =~ ^[0-9]+%?$ ]]; then
|
|
|
|
size="$1"
|
|
|
|
else
|
2015-03-08 07:39:30 +00:00
|
|
|
[ -n "$1" -a "$1" != "--" ] && args+=("$1")
|
|
|
|
shift
|
2015-03-08 06:08:27 +00:00
|
|
|
continue
|
|
|
|
fi
|
2015-03-06 09:51:50 +00:00
|
|
|
fi
|
2015-03-08 06:08:27 +00:00
|
|
|
[[ "$size" =~ %$ ]] && opt="$opt -p ${size:0:((${#size}-1))}" ||
|
|
|
|
opt="$opt -l $size"
|
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 [ -z "$TMUX_PANE" ]; then
|
|
|
|
fzf "${args[@]}"
|
|
|
|
exit $?
|
|
|
|
fi
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# Build arguments to fzf
|
|
|
|
[ ${#args[@]} -gt 0 ] && fzf_args=$(printf '\\"%s\\" ' "${args[@]}"; echo '')
|
|
|
|
|
|
|
|
# Clean up named pipes on exit
|
|
|
|
id=$RANDOM
|
|
|
|
fifo1=/tmp/fzf-fifo1-$id
|
|
|
|
fifo2=/tmp/fzf-fifo2-$id
|
|
|
|
fifo3=/tmp/fzf-fifo3-$id
|
|
|
|
cleanup() {
|
|
|
|
rm -f $fifo1 $fifo2 $fifo3
|
|
|
|
}
|
|
|
|
trap cleanup EXIT SIGINT SIGTERM
|
|
|
|
|
2015-03-07 00:29:16 +00:00
|
|
|
fail() {
|
|
|
|
>&2 echo "$1"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
fzf=$(which fzf 2> /dev/null) || fail "fzf executable not found"
|
|
|
|
|
2015-03-06 09:51:50 +00:00
|
|
|
mkfifo $fifo2
|
|
|
|
mkfifo $fifo3
|
|
|
|
if [ -t 0 ]; then
|
2015-03-07 00:29:16 +00:00
|
|
|
tmux split-window $opt 'sh -c "'$fzf' '"$fzf_args"' > '$fifo2'; echo \$? > '$fifo3'"'
|
2015-03-06 09:51:50 +00:00
|
|
|
else
|
|
|
|
mkfifo $fifo1
|
2015-03-07 00:29:16 +00:00
|
|
|
tmux split-window $opt 'sh -c "'$fzf' '"$fzf_args"' < '$fifo1' > '$fifo2'; echo \$? > '$fifo3'"'
|
2015-03-06 09:51:50 +00:00
|
|
|
cat <&0 > $fifo1 &
|
|
|
|
fi
|
|
|
|
cat $fifo2
|
|
|
|
[ "$(cat $fifo3)" = '0' ]
|
|
|
|
|