#!/bin/sh
# DEBUG=1
test -n "$DEBUG" && set -x
die() {
tmux display-message "kweasy-pane error: $*"
sleep 1
exit 2
}
die-swap() {
tmux swap-pane -t $PANE
die "$@"
}
test -n "$TMUX_PANE" || exit 2
if test -n "$1"; then
PANE="$1"
else
PANE=$(tmux list-panes -F '#{?pane_active,#D,}' | tr -d '\n\r ')
fi
test -n "$PANE" || die "Could not detect active pane"
if test "$TMUX_PANE" = "$PANE"; then
die "This command needs to run in background pane ${TMUX_PANE//%/%%} | ${PANE//%/%}"
fi
# tmux display-message "kweasy-pane running: source=$PANE display=$TMUX_PANE"
if test -n "$FIND"; then
tmux capturep -b kweasy -t "$PANE" \; \
swap-pane -t "$PANE"
else
tmux capturep -b kweasy -t "$PANE" \; \
showb -b kweasy \; \
swap-pane -t "$PANE" | \
awk 'NR>1{print line} {line=$0} END{printf "%s", line}'
FIND=$(tty-readbyte)
fi
test -n "$DEBUG" && echo "Ready! »$FIND«" && read
test -n "$FIND" || die-swap "nothing to match"
# Workaround for byte/character counting mismatch: |tr "$uni" "$asc"|
# uni='─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟'
# asc='\-\-||\-\-||\-\-||++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\-\-||=|+++++++++++++++++++++++++++++++/\\X\-|\-|\-|\-|\-|\-|#____#######|||||###~###########'
tmux saveb -b kweasy - | kweasy.awk FIND="$FIND"
test -n "$DEBUG" && echo "Done! ($?)" && read
tmux swap-pane -t $PANE