From c33ccec700fb998a3efd2b985d540f03ac6071d8 Mon Sep 17 00:00:00 2001 From: johnny Date: Thu, 30 Jul 2020 18:39:37 -0700 Subject: [PATCH] Reworked setting of bash JAVA_HOME This change will set JAVA_HOME before a command is executed, not when the prompt is displayed. The difference is subtle but it reduces terminal latency significantly. I also swapped out the call to asdf which for asdf where to reduce the amount of work necessary to set the JAVA_HOME as the value provided by asdf where provides the correct path. --- set-java-home.bash | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/set-java-home.bash b/set-java-home.bash index e2e68667..cb554aa2 100644 --- a/set-java-home.bash +++ b/set-java-home.bash @@ -1,19 +1,20 @@ asdf_update_java_home() { local java_path - java_path="$(asdf which java)" + java_path="$(asdf where java)" if [[ -n "${java_path}" ]]; then export JAVA_HOME - JAVA_HOME="$(dirname "$(dirname "$(realpath "${java_path}")")")" + JAVA_HOME="${java_path}" fi } -prompt_command() { - if [[ "${PWD}" == "${LAST_PWD}" ]]; then - return - fi - LAST_PWD="${PWD}" +preexec () { asdf_update_java_home } -export PROMPT_COMMAND="${PROMPT_COMMAND:+${PROMPT_COMMAND}; prompt_command}" -export PROMPT_COMMAND="${PROMPT_COMMAND:-prompt_command}" +preexec_invoke_exec () { + [ -n "${COMP_LINE}" ] && return # do nothing if completing + [ "${BASH_COMMAND}" = "${PROMPT_COMMAND}" ] && return # don't cause a preexec for $PROMPT_COMMAND + preexec +} + +trap 'preexec_invoke_exec' DEBUG