#!/bin/sh
#
# Usage:
#     reptyr              # list vi process
#     reptyr <N>          # attach to N-th vi
#     reptyr <PID>        #
#
# ---
# see: https://github.com/chuangxiaomoo/bin

case "$1" in
  '')
    echo "UID        PID  PPID"
    ps -ef | grep -E --color "\<(n|)[v]i"
    exit 0
    ;;
  [1-9])
    N="$1"
    VI=$(ps -ef | grep -E '\<(n|)[v]i' | awk -v line="${N}" 'NR==line { print $2 }')
    ;;
  *)
    VI="${1}"
    ;;
esac

reptyr "${VI}"
