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

Commit

Permalink
smartcd: use a function to validate selected_entry
Browse files Browse the repository at this point in the history
  • Loading branch information
CodesOfRishi committed Jan 9, 2022
1 parent 9698c36 commit 95c23dc
Showing 1 changed file with 15 additions and 34 deletions.
49 changes: 15 additions & 34 deletions smartcd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ __smartcd::col2() {
awk '{print $2}'
}

# validate selected_entry
__smartcd::validate_selected_entry() {
if [[ -z ${selected_entry} ]]; then
printf '%s\n' "No directory found or selected!" 1>&2
return 1
else
builtin cd "${selected_entry}" && generate_recent_dir_log && \
if [[ -z ${piped_value} ]]; then printf '%s\n' "${PWD}"; fi
fi
}

__smartcd__() {
# location for smartcd to store log
export SMARTCD_CONFIG_DIR=${SMARTCD_CONFIG_DIR:-"$HOME/.config/.smartcd"}
Expand Down Expand Up @@ -102,14 +113,7 @@ __smartcd__() {
if [[ ${exit_status} -ne 0 ]]; then
if [[ $( printf '%s\n' "${err_msg}" | tr "[:upper:]" "[:lower:]" ) = *"no such file or directory"* ]]; then
local selected_entry && selected_entry=$( eval "${find_sub_dir_cmd_args}" | run_fzf_command "${path_argument}" )

if [[ -z ${selected_entry} ]]; then
printf '%s\n' "No directory found or selected!" 1>&2
return 1
else
builtin cd "${selected_entry}" && generate_recent_dir_log && \
if [[ -z ${piped_value} ]]; then printf '%s\n' "${PWD}"; fi
fi
__smartcd::validate_selected_entry
else
printf '%s\n' "${err_msg}" 1>&2
return 1
Expand All @@ -127,15 +131,7 @@ __smartcd__() {
else
local query=$*
local selected_entry && selected_entry=$( < "${recent_dir_log}" run_fzf_command "${query}" )

if [[ -z ${selected_entry} ]]; then
printf '%s\n' "No directory found or selected!" 1>&2
return 1
else
builtin cd "${selected_entry}" || return 1
generate_recent_dir_log && \
if [[ -z ${piped_value} ]]; then printf '%s\n' "${PWD}"; fi
fi
__smartcd::validate_selected_entry
fi
}

Expand All @@ -157,14 +153,7 @@ __smartcd__() {

local query=$*
local selected_entry && selected_entry=$( find_parent_dir_paths | run_fzf_command "${query}" )

if [[ -z ${selected_entry} ]]; then
printf '%s\n' "No directory found or selected!" 1>&2
return 1
else
builtin cd "${selected_entry}" && generate_recent_dir_log && \
if [[ -z ${piped_value} ]]; then printf '%s\n' "${PWD}"; fi
fi
__smartcd::validate_selected_entry
}

# feature
Expand All @@ -182,16 +171,8 @@ __smartcd__() {
# feature
base_parent_cd() {
local path_argument=$*

local selected_entry && selected_entry=$( eval "${find_base_dir_cmd_args}" | run_fzf_command "${path_argument}" )

if [[ -z ${selected_entry} ]]; then
printf '%s\n' "No directory found or selected!" 1>&2
return 1
else
builtin cd "${selected_entry}" && generate_recent_dir_log && \
if [[ -z ${piped_value} ]]; then printf '%s\n' "${PWD}"; fi
fi
__smartcd::validate_selected_entry
}

# feature
Expand Down

0 comments on commit 95c23dc

Please sign in to comment.