#!/bin/sh
#
# e - quick shortcut to an editor
#
# usage:
#
#   $ e
#   # => opens the current directory in your editor
#
#   $ e .
#   $ e /usr/local
#   # => opens the specified directory in your editor
#
#   $ e rg PATTERN [FOLDER]
#   # => opens the files matching PATTERN in your editor
#
# ---
# see: https://github.com/holman/dotfiles
# source: https://github.com/pengwynn/dotfiles
#   exec "${EDITOR-}" "${1:-.}"

if [ "$#" -gt 0 ]; then
  case "$1" in
    g | gr | r | rg)
      shift
      # shellcheck disable=SC2312,SC2046
      ${EDITOR:-vi} $(rg --files-with-matches "$@")
      ;;
    *)
      if [ -d "$1" ]; then
        ${EDITOR:-vi} -c ":lcd %:p:h" "${1:-.}"
      else
        ${EDITOR:-vi} "${@:-.}"
      fi
      ;;
  esac
else
  exec "${EDITOR-}" "${@:-.}"
fi
