Skip to content

Commit

Permalink
Merge pull request #37 from key-amb/feature/configure-subcommands
Browse files Browse the repository at this point in the history
Make "__enhancd::cd()" arguments "-" and ".." configurable by shell vars
  • Loading branch information
b4b4r07 committed Aug 29, 2016
2 parents a3b59a1 + 7525685 commit bf068c3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
2 changes: 2 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export ENHANCD_FILTER
export ENHANCD_DIR="${ENHANCD_DIR:-$HOME/.enhancd}"
export ENHANCD_DISABLE_DOT="${ENHANCD_DISABLE_DOT:-0}"
export ENHANCD_DISABLE_HYPHEN="${ENHANCD_DISABLE_HYPHEN:-0}"
export ENHANCD_DOT_ARG="${ENHANCD_DOT_ARG:-..}"
export ENHANCD_HYPHEN_ARG="${ENHANCD_HYPHEN_ARG:--}"

export _ENHANCD_VERSION="2.2.0"

Expand Down
16 changes: 13 additions & 3 deletions src/enhancd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,17 @@ __enhancd::cd()
case "$arg" in
":stdin:")
;;
"-")
"$ENHANCD_HYPHEN_ARG")
# If a hyphen is passed as the argument,
# searchs from the last 10 directory items in the log
if [[ "$ENHANCD_DISABLE_HYPHEN" -eq 0 ]]; then
t="$(__enhancd::list --narrow "$2" | head)"
t="$(__enhancd::filter "${t:-$2}")"
else
t="-"
t="$OLDPWD"
fi
;;
"..")
"$ENHANCD_DOT_ARG")
# If a double-dot is passed as the argument,
# it behaves like a zsh-bd plugin
# In short, you can jump back to a specific directory,
Expand All @@ -300,6 +300,16 @@ __enhancd::cd()
t=".."
fi
;;
"-")
# When $ENHANCD_HYPHEN_ARG is configured,
# this behaves like `cd -`
t="$OLDPWD"
;;
"..")
# When $ENHANCD_DOT_ARG is configured,
# ".." is passed to builtin cd
t=".."
;;
-*|--*)
shift
__enhancd::options "$arg" "$@"
Expand Down
51 changes: 50 additions & 1 deletion test/enhancd-enhancd_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,54 @@ T_SUB "__enhancd::filter()" ((
))

T_SUB "__enhancd::cd()" ((
# skip
# TODO: should set valid filter when writing filter tests
_ENHANCD_FILTER=dummy
T_SUB "With \$ENHANCD_DISABLE_HYPHEN set" ((
ENHANCD_DISABLE_HYPHEN=1
__enhancd::cd $HOME
__enhancd::cd $ENHANCD_ROOT
t_is "$PWD" "$ENHANCD_ROOT"
t_is "$OLDPWD" "$HOME"
__enhancd::cd -
t_is "$PWD" "$HOME"
t_is "$OLDPWD" "$ENHANCD_ROOT"
))

T_SUB "With \$ENHANCD_DISABLE_DOT set" ((
ENHANCD_DISABLE_DOT=1
__enhancd::cd $ENHANCD_ROOT/test
__enhancd::cd ..
t_is "$PWD" "$ENHANCD_ROOT"
))

T_SUB "With \$ENHANCD_HYPHEN_ARG set" ((
ENHANCD_HYPHEN_ARG=--dummy
ENHANCD_DISABLE_HYPHEN=1
__enhancd::cd $HOME
__enhancd::cd $ENHANCD_ROOT
__enhancd::cd --dummy
t_is "$PWD" "$HOME"
t_is "$OLDPWD" "$ENHANCD_ROOT"
ENHANCD_DISABLE_HYPHEN=0
__enhancd::cd $HOME
__enhancd::cd $ENHANCD_ROOT
__enhancd::cd -
t_is "$PWD" "$HOME"
t_is "$OLDPWD" "$ENHANCD_ROOT"
))

T_SUB "With \$ENHANCD_DOT_ARG set" ((
ENHANCD_DOT_ARG=--dummy
ENHANCD_DISABLE_DOT=1
__enhancd::cd $ENHANCD_ROOT/test
__enhancd::cd --dummy
t_is "$PWD" "$ENHANCD_ROOT"
ENHANCD_DISABLE_DOT=0
__enhancd::cd $ENHANCD_ROOT/test
__enhancd::cd ..
t_is "$PWD" "$ENHANCD_ROOT"
))
))

0 comments on commit bf068c3

Please sign in to comment.