From 55a4742308cd4c981652c827aa5e45b48bfb5a74 Mon Sep 17 00:00:00 2001 From: Richard Scott Date: Thu, 25 May 2023 14:35:29 +0100 Subject: [PATCH 1/6] Saving changes --- src/{bash => bash.sh} | 0 src/dircmd.sh | 68 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) rename src/{bash => bash.sh} (100%) create mode 100644 src/dircmd.sh diff --git a/src/bash b/src/bash.sh similarity index 100% rename from src/bash rename to src/bash.sh diff --git a/src/dircmd.sh b/src/dircmd.sh new file mode 100644 index 0000000..e35bd77 --- /dev/null +++ b/src/dircmd.sh @@ -0,0 +1,68 @@ +# +# https://github.com/dircmd/dircmd +# +export DIRCMD_VERSION="3.0.0" + +# Detect Shell Type +unset SHELL_TYPE +if [[ -n "${BASH_VERSION}" ]]; then + export SHELL_TYPE="bash" +fi +if [[ -n "${ZSH_VERSION}" ]]; then + export SHELL_TYPE="zsh" +fi +if [[ -z "${SHELL_TYPE}" ]]; then + return 0 +fi + +_dircmd_search_tree() { + + local _start_dir="${1}" + local _end_dir="${2}" + local _reverse_list="${3:-0}" + local _search_list=() + + while ! echo "${_start_dir}" | grep -qE "^${_end_dir}$"; do + if [[ -d "${_start_dir}/.dircmd" ]]; then + _search_list+=("${_start_dir}/.dircmd") + fi + _start_dir="$(dirname "${_start_dir}")" + done + + if [[ ${_reverse_list} -eq 0 ]]; then + local _squence=$(seq --separator=" " 0 $(("${#_search_list[@]}" - 1))) + local _mode="exit" + else + local _squence=$(seq --separator=" " 0 $(("${#_search_list[@]}" - 1)) | rev) + local _mode="entry" + fi + + for i in ${_squence}; do + echo "${i}: ${_search_list[${i}]}/${_mode}" + done + +} + +_dircmd_hook() { + if [[ -z "${DIRCMD_OLDPWD}" ]] || echo "${DIRCMD_OLDPWD}" | grep -qE "^${PWD}$"; then + true + else + if echo "${OLDPWD}" | grep -qE "^${PWD}"; then + _dircmd_search_tree "${OLDPWD}" "${PWD}" 0 + elif echo "${PWD}" | grep -qE "^${OLDPWD}"; then + _dircmd_search_tree "${PWD}" "${OLDPWD}" 1 + else + _dircmd_search_tree "${OLDPWD}" "/" 0 + _dircmd_search_tree "${PWD}" "/" 1 + fi + fi + export DIRCMD_OLDPWD="${PWD}" +} + +if [[ ! ${PROMPT_COMMAND[*]} =~ '_dircmd_hook' ]]; then + PROMPT_COMMAND+=("_dircmd_hook") +fi +prmptcmd() { + eval "${PROMPT_COMMAND}" +} +precmd_functions=(prmptcmd) From 971118fd05019ce9fffd8fdabf2fb539c567ef30 Mon Sep 17 00:00:00 2001 From: Richard Scott Date: Thu, 25 May 2023 17:04:38 +0100 Subject: [PATCH 2/6] - --- src/dircmd.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dircmd.sh b/src/dircmd.sh index e35bd77..0a5c5fd 100644 --- a/src/dircmd.sh +++ b/src/dircmd.sh @@ -30,10 +30,10 @@ _dircmd_search_tree() { done if [[ ${_reverse_list} -eq 0 ]]; then - local _squence=$(seq --separator=" " 0 $(("${#_search_list[@]}" - 1))) + local _squence=$(seq --separator=" " 0 "${#_search_list[@]}") local _mode="exit" else - local _squence=$(seq --separator=" " 0 $(("${#_search_list[@]}" - 1)) | rev) + local _squence=$(seq --separator=" " 0 "${#_search_list[@]}" | rev) local _mode="entry" fi From df60a02cafbd21995367ecaa0e7fbad4cac1604c Mon Sep 17 00:00:00 2001 From: Richard Scott Date: Thu, 25 May 2023 18:06:02 +0100 Subject: [PATCH 3/6] Updated code --- src/dircmd.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/dircmd.sh b/src/dircmd.sh index 0a5c5fd..bb75081 100644 --- a/src/dircmd.sh +++ b/src/dircmd.sh @@ -38,9 +38,10 @@ _dircmd_search_tree() { fi for i in ${_squence}; do - echo "${i}: ${_search_list[${i}]}/${_mode}" + for ES in $(ls ${_search_list[${i}]}/*${_mode} 2>/dev/null); do + source "${ES}" Date: Thu, 25 May 2023 18:06:25 +0100 Subject: [PATCH 4/6] Updated codebase --- src/bash.sh | 153 ---------------------------------------------------- 1 file changed, 153 deletions(-) delete mode 100644 src/bash.sh diff --git a/src/bash.sh b/src/bash.sh deleted file mode 100644 index 60e105a..0000000 --- a/src/bash.sh +++ /dev/null @@ -1,153 +0,0 @@ -# -# https://github.com/dircmd/dircmd -# -#export DIRCMD_DEBUG=1 -export DIRCMD_VERSION="2.0.2" -if echo "${SHELL}" | grep -E -q "/bash$"; then - export DIRCMD_PATH="${BASH_SOURCE[0]}" -fi - -if ! declare -p DIRCMD_STORAGE >/dev/null 2>&1; then - if uname -s | grep -i -q 'darwin'; then - declare -a DIRCMD_STORAGE - else - declare -A DIRCMD_STORAGE - fi -fi - -_dircmd_get_hash() { - echo "DIRCMD_$(echo ${PWD} | sha1sum | awk '{print $1}')" -} - -_dircmd_source() { - if [[ -e .dircmd ]]; then - if [[ $(find .dircmd -type f 2>/dev/null | wc -l) -gt 0 ]]; then - for ES in .dircmd/*${1}; do - if [[ -n "${DIRCMD_DEBUG}" ]]; then - echo "Sourcing: ${PWD}/${ES}" - fi - export DIRCMD_HASH="$(_dircmd_get_hash)" - source "${ES}" >> [up]" - fi - cd "${OLD_DIR}" - while ! echo "${PWD}" | grep -E -q "^(/|${NEW_DIR})$"; do - if [[ -n "${DIRCMD_DEBUG}" ]]; then - echo "PWD: ${PWD}" - fi - _dircmd_source "exit" - cd .. - done - elif echo "${PWD}" | grep -E -q "^${OLD_DIR}"; then - if echo "${OLD_DIR}" | grep -E -q "^${PWD}$"; then - if [[ -n "${DIRCMD_DEBUG}" ]]; then - echo ">>> [first]" - fi - OLD_DIR="/" - fi - if [[ -n "${DIRCMD_DEBUG}" ]]; then - echo ">>> [down]" - fi - while ! echo "${PWD}" | grep -E -q "^${OLD_DIR}$"; do - if [[ -e .dircmd ]]; then - local DIRCMD_LIST[${#DIRCMD_LIST[@]}]="${PWD}" - fi - cd .. - done - cd "${NEW_DIR}" - for ((i = ${#DIRCMD_LIST[@]}; i >= 0; i--)); do - cd "${DIRCMD_LIST[${i}]}" - _dircmd_source "entry" - done - cd "${NEW_DIR}" - else - if [[ -n "${DIRCMD_DEBUG}" ]]; then - echo ">>> [out]" - fi - cd "${OLD_DIR}" - while ! echo "${PWD}" | grep -E -q "^(/|${NEW_DIR})$"; do - if [[ -n "${DIRCMD_DEBUG}" ]]; then - echo "PWD: ${PWD}" - fi - _dircmd_source "exit" - cd .. - done - cd "${NEW_DIR}" - OLD_DIR="/" - while ! echo "${PWD}" | grep -E -q "^${OLD_DIR}$"; do - if [[ -e .dircmd ]]; then - local DIRCMD_LIST[${#DIRCMD_LIST[@]}]="${PWD}" - fi - cd .. - done - for ((i = ${#DIRCMD_LIST[@]}; i >= 0; i--)); do - cd "${DIRCMD_LIST[${i}]}" - _dircmd_source "entry" - done - cd "${NEW_DIR}" - fi - else - if [[ -n "${DIRCMD_DEBUG}" ]]; then - echo ">>> [same]" - fi - fi - exec 6<&0 - export DIRCMD_HASH="$(_dircmd_get_hash)" - return $previous_exit_status -} - -if ! [[ "${PROMPT_COMMAND}" =~ _dircmd_hook ]]; then - PROMPT_COMMAND="_dircmd_hook; ${PROMPT_COMMAND}" -fi - -_dircmd_save() { - VAR_NAME="${DIRCMD_HASH}_${1}" - VAR_DATA="${!1}" - DIRCMD_STORAGE[${VAR_NAME}]="${VAR_DATA}" -} - -_dircmd_restore() { - VAR_NAME="${DIRCMD_HASH}_${1}" - VAR_DATA="${DIRCMD_STORAGE[${VAR_NAME}]}" - if [[ -n "${VAR_DATA}" ]]; then - unset ${1} - export ${1}="${VAR_DATA}" - fi -} From 207485b9fffd5ef9d643b2b61bebde12d4174222 Mon Sep 17 00:00:00 2001 From: Richard Scott Date: Thu, 15 Jun 2023 09:32:47 +0100 Subject: [PATCH 5/6] Added save/restore functions back in. --- src/dircmd.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/dircmd.sh b/src/dircmd.sh index bb75081..1259e0b 100644 --- a/src/dircmd.sh +++ b/src/dircmd.sh @@ -60,6 +60,21 @@ _dircmd_hook() { export DIRCMD_OLDPWD="${PWD}" } +_dircmd_save() { + VAR_NAME="${DIRCMD_HASH}_${1}" + VAR_DATA="${!1}" + DIRCMD_STORAGE[${VAR_NAME}]="${VAR_DATA}" +} + +_dircmd_restore() { + VAR_NAME="${DIRCMD_HASH}_${1}" + VAR_DATA="${DIRCMD_STORAGE[${VAR_NAME}]}" + if [[ -n "${VAR_DATA}" ]]; then + unset ${1} + export ${1}="${VAR_DATA}" + fi +} + if [[ ! ${PROMPT_COMMAND[*]} =~ '_dircmd_hook' ]]; then PROMPT_COMMAND+=("_dircmd_hook") fi From 21c1529d501b33b4e63e581ab085cb36723d7494 Mon Sep 17 00:00:00 2001 From: Richard Scott Date: Tue, 26 Sep 2023 10:59:22 +0100 Subject: [PATCH 6/6] Updating script to work with PROMPT_COMMAND arrays --- src/dircmd.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/dircmd.sh b/src/dircmd.sh index 1259e0b..0c10275 100644 --- a/src/dircmd.sh +++ b/src/dircmd.sh @@ -75,9 +75,13 @@ _dircmd_restore() { fi } -if [[ ! ${PROMPT_COMMAND[*]} =~ '_dircmd_hook' ]]; then +if ((BASH_VERSINFO[0] > 5 || BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 1)); then + PROMPT_COMMAND=${PROMPT_COMMAND-} PROMPT_COMMAND+=("_dircmd_hook") +else + PROMPT_COMMAND="_dircmd_hook; ${PROMPT_COMMAND}" fi + prmptcmd() { eval "${PROMPT_COMMAND}" }