Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
smartcd: optimize parent_dir_hop()
Browse files Browse the repository at this point in the history
  • Loading branch information
CodesOfRishi committed Dec 15, 2021
1 parent d0213a0 commit 1802eea
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions smartcd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,33 +91,28 @@ __smartcd__() {
return
fi

local parent_dir_log=$( mktemp ) # temporary file to store parent directories's absolute paths

_path=${PWD%/*}
while [[ ${_path} != "" ]]; do
fd --exclude .git/ --search-path ${_path} -t d --max-depth=1 -i -H -F >> ${parent_dir_log}
_path=${_path%/*}
done

if [[ ! -s ${parent_dir_log} ]]; then
>&2 echo "No matching parent-directory found!"
find_parent_dir_paths() {
_path=${PWD%/*}
while [[ ${_path} != "" ]]; do
fd --exclude .git/ --search-path ${_path} -t d --max-depth=1 -i -H -F
_path=${_path%/*}
done
}

local query=$@
local selected_entry=""
validate_rec_listing_cmd
if [[ ${SMARTCD_REC_LISTING_CMD} = "" ]]; then
selected_entry=($(find_parent_dir_paths | fzf --exit-0 --query="${query}"))
else
local query=$@
local selected_entry=""
validate_rec_listing_cmd
if [[ ${SMARTCD_REC_LISTING_CMD} = "" ]]; then
selected_entry=($(cat ${parent_dir_log} | fzf --exit-0 --query="${query}"))
else
selected_entry=($(cat ${parent_dir_log} | fzf --exit-0 --query="${query}" --preview "${SMARTCD_REC_LISTING_CMD} {}"))
fi
selected_entry=($(find_parent_dir_paths | fzf --exit-0 --query="${query}" --preview "${SMARTCD_REC_LISTING_CMD} {}"))
fi

if [[ ${selected_entry} = "" ]]; then
>&2 echo "No directory found or selected!"
else
builtin cd ${selected_entry} && generate_recent_dir_log && echo ${PWD}
fi
if [[ ${selected_entry} = "" ]]; then
>&2 echo "No directory found or selected!"
else
builtin cd ${selected_entry} && generate_recent_dir_log && echo ${PWD}
fi
rm -f ${parent_dir_log}
}

# feature
Expand Down

0 comments on commit 1802eea

Please sign in to comment.