#!/bin/sh -e

tmux capture-pane -pS -1000 >/tmp/tmdump
# would like to add a uniq in here, but cannot do that without sort
# but if sorted we lose the order
tmline="$(grep ' ' /tmp/tmdump | grep -Ev '^.. {10}' | tac | tail -n+2 | fzf)"
# tmline="$(grep -E '[✨💥️]' /tmp/tmdump | grep -Ev '^.. {10}'| tac | tail -n+2 | fzf)"

copy=0
output=""
while read -r line; do
  if [ "${copy}" = 1 ]; then
    if echo "${line}" | grep -q " "; then
      copy=0
    fi
    output="${output}${line}\n"
  fi
  if echo "${line}" | grep -q "${tmline}"; then
    output=""
    copy=1
  fi
done </tmp/tmdump

{
  echo "${tmline}"
  echo "${output}"
} | pbcopy
