Commit 5e84e90

mo khan <mo@mokhan.ca>
2021-02-05 23:37:24
feat: add shim to inject jive into the shell
1 parent 9cd7cb3
Changed files (1)
exe/jive.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+# shellcheck disable=1001,1012,2039,1090
+# 1001,1012 stop complaints about '\awk' syntax to bypass aliases.
+# 2039 stops complaints about array references not being POSIX.
+# 1090 stops complaints about sourcing non-constant paths.
+
+__shell="$(\ps -p $$ | \awk 'NR > 1 { sub(/^-/, "", $4); print $4 }')"
+__shellname="$(basename "${__shell}")"
+
+case "${__shellname}" in
+  bash)
+    __jive_source_dir="$(builtin cd "$(\dirname "${BASH_SOURCE[0]}")" && \pwd)"
+    ;;
+  zsh)
+    __jive_source_dir="$(\dirname "$0:A")"
+    ;;
+  *)
+    >&2 \echo "jive is not compatible with your shell (${__shell})"
+    \return 1
+    ;;
+esac
+
+__mtime_of_jive_script="$(\date -r "${__jive_source_dir}/jive.sh" +%s)"
+__jive_auto_reload() {
+  local current_mtime
+  current_mtime="$(\date -r "${__jive_source_dir}/jive.sh" +%s)"
+
+  if [[ "${current_mtime}" != "${__mtime_of_jive_script}" ]]; then
+    echo "Reloading... jive.sh"
+    . "${__jive_source_dir}/jive.sh"
+  fi
+}
+
+jive() {
+  __jive_auto_reload
+  __jive_exec "$@"
+}
+
+__jive_exec() {
+  echo "$@"
+}