#!/bin/sh -e
#
# tt - tail latest with fzf

# 0) tee >(tail -1 | field NF | tr -d "\n"| pbcopy)
# 1) tee >(tail -1 | field $1 | tr -d "\n"| pbcopy)
# 2) tee >(tail -$2 | head -1 | field $1 | tr -d "\n" | pbcopy)

# shellcheck disable=SC2312,SC2249
case "$#" in
  0)
    tail "$(find . -type f -exec ls -tp -- {} + | head -1)"
    ;;
  1)
    find -L . -type f -exec ls -tp -- {} + | head -"$1" \
      | fzf --bind 'enter:become(tail {})'
    ;;
  2)
    find -L "$2" -type f -exec ls -tp -- {} + | head -n "$1" \
      | fzf --delimiter '/' --with-nth=-1 --bind 'enter:become(tail {})'
    ;;
esac
