# POSIX sh
# shell integration => spawning new terminal instances in the current working directory
# add the following to your $ENV or relevant startup script:
osc7_cd() {
  cd "$@" || return $?

  tmp="${PWD}"
  while [ -n "${tmp}" ]; do
    cut="${tmp#?}"
    c="${tmp%"${cut}"}"
    case "${c}" in
      [-/:_.!\'\(\)~[:alnum:]] ) enc="${enc}${c}" ;;
      * ) enc="${enc}$(printf '%%%02X' """\"${c}")"  ;;
    esac
    tmp="${cut}"
  done

  # shellcheck disable=SC2312
  printf "\033]7;file://%s%s\033\\" "$(hostname)" "${enc}"
}

osc7_cd "${PWD}" # first-run
alias cd=osc7_cd
