#!/bin/sh
#
# repl - creates a FIFO at /tmp/COMMAND.repl, and pipes it into your program
# for use in vim
#
# usage: repl COMMAND [ARGS]
#
# note: python is an alias for `python3 -m code -q`
#
# ---
# source: https://patpatpat.xyz/data/repl

if [ "$#" -ne 1 ]; then
  printf '%s\n' "$(basename "$0") [command] [args]"
  exit 1
fi

cmd="$1"
echo "starting repl ${cmd} at /tmp/${cmd}.repl..."

rm -f "/tmp/${cmd}.repl" || exit 1
mkfifo "/tmp/${cmd}.repl" || exit 1

case "${cmd}" in
  python) tail -f "/tmp/${cmd}.repl" | python3 -m code -q ;;
  *) tail -f "/tmp/${cmd}.repl" | ${cmd} ;;
esac
