#!/usr/bin/env sh
#
# scrollback - output the tmux scrollback for the current pane in reverse order
#
# Meant as a shortcut for making ad-hoc pipelines. e.g.,
#
#   git fetch; scrollback | match-git-range | fzy | xargs git log --oneline
#
# View the scrollback for another pane:
#
#   scrollback 1 | grep foo
#   scrollback %12 | grep foo

while getopts 'h' opt; do
  case ${opt} in
    h | ?)
       awk 'NR == 1 { next } /^$/ { exit } { print substr($0, 3) }' "$0"
       exit 0
       ;;
  esac
done
shift "$((OPTIND - 1))"

tmux capture-pane ${1:+-t "$1"} -pJS -5000 | tac
