#!/bin/sh
#
# editorwrap - editor wrapper
#
# ---
# see: https://github.com/mitchweaver/dots/blob/master/shell/development.shellrc

v() {
  if [ -n "$1" ] && [ -d "$1" ]; then
    >&2 printf 'Error: %s is a directory.\n' "${1%/}"
    return
  elif [ -n "$1" ] && [ ! -f "$1" ]; then
    printf "File '%s' does not exist.\nCreate it? (y/n): " "$1"
    read -r ans
    case ${ans} in
      *y*) ;;
      *) return ;;
    esac
  fi
  case $(uname) in
    Darwin)
      ${EDITOR:-nvim} "$@"
      ;;
    Linux)
      case ${EDITOR:-vi} in
        # terminal editor
        vi | vim | nvim)
          ${EDITOR} -n -- "$@" \
            && clear
          ;;
        *)
          # graphical editor
          (${EDITOR} "$@" >/dev/null 2>&1 &)
          ;;
      esac
      ;;
  esac
}
