Last active
April 13, 2016 08:30
-
-
Save yuuan/c24d07d0708e37460e1ede30442251a3 to your computer and use it in GitHub Desktop.
fzf-tmux to peco
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# peco-tmux: starts peco in a tmux pane | |
# usage: peco-tmux [-u|-d [HEIGHT[%]]] [-l|-r [WIDTH[%]]] [--] [PECO 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 peco-tmux options from peco options to | |
# avoid conflicts | |
skip=1 | |
;; | |
*) | |
args+=("$1") | |
;; | |
esac | |
shift | |
done | |
if [ -z "$TMUX" ]; then | |
peco "${args[@]}" | |
exit $? | |
fi | |
# 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 peco-tmux is running\\r\"; done; done'") | |
tmux swap-pane -t $tmp_window \; select-window -t $tmp_window | |
fi | |
set -e | |
# Clean up named pipes on exit | |
id=$RANDOM | |
argsf="${TMPDIR:-/tmp}/peco-args-$id" | |
fifo1="${TMPDIR:-/tmp}/peco-fifo1-$id" | |
fifo2="${TMPDIR:-/tmp}/peco-fifo2-$id" | |
fifo3="${TMPDIR:-/tmp}/peco-fifo3-$id" | |
cleanup() { | |
rm -f $argsf $fifo1 $fifo2 $fifo3 | |
# 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 | |
} | |
trap cleanup EXIT SIGINT SIGTERM | |
fail() { | |
>&2 echo "$1" | |
exit 2 | |
} | |
peco="$(which peco 2> /dev/null)" || peco="$(dirname "$0")/peco" | |
[ -x "$peco" ] || fail "peco executable not found" | |
envs="env TERM=$TERM " | |
[ -n "$PECO_DEFAULT_OPTS" ] && envs="$envs PECO_DEFAULT_OPTS=$(printf %q "$PECO_DEFAULT_OPTS")" | |
[ -n "$PECO_DEFAULT_COMMAND" ] && envs="$envs PECO_DEFAULT_COMMAND=$(printf %q "$PECO_DEFAULT_COMMAND")" | |
mkfifo -m o+w $fifo2 | |
mkfifo -m o+w $fifo3 | |
# Build arguments to peco | |
opts="" | |
for arg in "${args[@]}"; do | |
arg="${arg//\"/\\\"}" | |
arg="${arg//\`/\\\`}" | |
opts="$opts \"$arg\"" | |
done | |
if [ -n "$term" -o -t 0 ]; then | |
cat <<< "$peco $opts > $fifo2; echo \$? > $fifo3 $close" > $argsf | |
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 \ | |
> /dev/null 2>&1 | |
else | |
mkfifo $fifo1 | |
cat <<< "$peco $opts < $fifo1 > $fifo2; echo \$? > $fifo3 $close" > $argsf | |
tmux set-window-option synchronize-panes off \;\ | |
set-window-option remain-on-exit off \;\ | |
split-window $opt "$envs bash $argsf" $swap \ | |
> /dev/null 2>&1 | |
cat <&0 > $fifo1 & | |
fi | |
cat $fifo2 | |
exit "$(cat $fifo3)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fork from fzf/fzf-tmux at 0.11.4 · junegunn/fzf.