Skip to content

Commit

Permalink
Merge branch 'improvements' of git://github.com/blueyed/smartcd into …
Browse files Browse the repository at this point in the history
…blueyed-improvements
  • Loading branch information
daotoad committed Dec 11, 2016
2 parents 8566c30 + 5325dc1 commit 81e6d6e
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 37 deletions.
6 changes: 3 additions & 3 deletions download-and-install
Expand Up @@ -144,13 +144,13 @@ if [[ -z $smartcd_previously_installed ]]; then
echo
echo "To get started, create a few scripts. Its easy! Try this:"
echo
echo " echo 'echo hello there from \$(pwd)' | smartcd edit enter"
echo " echo 'echo goodbye from \$(pwd)' | smartcd edit leave"
echo " echo 'echo hello there from \$PWD' | smartcd edit enter"
echo " echo 'echo goodbye from \$PWD' | smartcd edit leave"
echo
echo "Then simply leave the directory and come back. For a more practical"
echo "example, how about tweaking your PATH?"
echo
echo " echo \"autostash PATH=\$(pwd)/bin:\\\$PATH\" | smartcd edit enter"
echo " echo \"autostash PATH=\$PWD/bin:\\\$PATH\" | smartcd edit enter"
echo
echo "(side note: the quoting rules when editing in this fashion can be a bit"
echo "awkward, so feel free to run \`smartcd edit\` interactively too!"
Expand Down
2 changes: 1 addition & 1 deletion lib/core/arrays
Expand Up @@ -139,7 +139,7 @@ function ashift() {
fi
}

# Bash requires the array be quoted, zsh requires it not
# Bash requires the array to be quoted, zsh does not.
function aunshift() {
local var=$1; shift

Expand Down
12 changes: 9 additions & 3 deletions lib/core/completion
Expand Up @@ -6,9 +6,15 @@
#

if [[ -n $ZSH_VERSION ]]; then
autoload -U compinit
compinit
autoload -U +X bashcompinit && bashcompinit
# Only load/call compinit once.
if [[ -z $_comp_dumpfile ]]; then
autoload -U compinit
compinit
fi
# Only load/call bashcompinit once.
if [[ -z $functions[complete] ]]; then
autoload -U +X bashcompinit && bashcompinit
fi
fi

function _smartcd_completion() {
Expand Down
38 changes: 24 additions & 14 deletions lib/core/smartcd
@@ -1,4 +1,5 @@
################################################################################

# smartcd - cd with some oomph
#
# Copyright (c) 2009,2012 Dave Olszewski <cxreg@pobox.com>
Expand Down Expand Up @@ -184,7 +185,7 @@ function smartcd() {
case $command in
-h|help|'') echo $usage;;
cd|pushd|popd) shift; _smartcd $command "$@"; return $?;;
reenter) local dir="$(pwd)"
reenter) local dir="$PWD"
_smartcd cd ..
_smartcd cd "$dir"
;;
Expand All @@ -203,32 +204,34 @@ function smartcd() {
--global) whence="global";;
--system) system="true";;
--all) whence="all"; system="both";;
*) echo "$opt unknown"
*) echo "$opt unknown. Available: [--host|--global|--all] [--system]."
return
;;
esac
shift
done

local type=$1
shift
case $type in
enter|leave) ;;
bash_enter|bash_leave) type="${type#bash_}";;
*) echo "Usage: smartcd $command [--host|--global|--all] [--system] (enter|leave)"
return
;;
esac
shift
[[ -n $type ]] && _smartcd_file_check "$type" "$whence" "$system" "$command" "$@"
;;
template) shift; smartcd_template "$@";;
setup) shift; local setup=$1; shift;
setup) shift; local setup=$1
case $setup in
cd|pushd|popd) setup_smartcd_function $setup;;
prompt-hook|prompt) setup_smartcd_prompt_hook;;
chpwd-hook|chpwd) setup_smartcd_chpwd_hook;;
exit-hook|exit) setup_smartcd_exit_hook;;
completion) _smartcd_load "completion";;
*) echo "Usage: smartcd $command (cd|pushd|popd|prompt[-hook]|chpwd[-hook]|exit[-hook]|completion)"
return ;;
esac
;;
on-leave) local code=${2//\\/\\\\};
Expand Down Expand Up @@ -449,7 +452,7 @@ function _smartcd() {
IFS="$_old_ifs"

if (( $rv != 0 )); then return $rv; fi
local whereto="$(pwd)"
local whereto="$PWD"
_smartcd_last_run_for="x$whereto"

# If zsh, turn autopushd off temporarily to avoid extra pushes
Expand Down Expand Up @@ -497,18 +500,24 @@ function _smartcd() {
apop leave_stack >/dev/null
done

local host=${SMARTCD_HOSTNAME:-$(hostname)}
local host=${SMARTCD_HOSTNAME:-${HOST:-$(hostname)}}

# Run leave_stack in lowest to highest
local stack_size=$(alen leave_stack)
: ${stack_size:=0}
local smartcd_runmode=leave

# Set vars used in while loops.
if [[ $stack_size -gt 0 ]]; then
local sysdir=$(_smartcd_sysdir)
fi

while (( $stack_size >= 1 )); do
# ashift in a sub-shell doesn't work
local smartcd_current_dir="$(afirst leave_stack)"
ashift leave_stack >/dev/null

# Set this just in case we are not able to actualy chdir to $smartcd_current_dir
# Set this in case we are not able to actually chdir to $smartcd_current_dir.
local varstash_dir="$smartcd_current_dir"

if [[ -d "$smartcd_current_dir" ]]; then
Expand All @@ -528,7 +537,6 @@ function _smartcd() {
# can check things out

# 1) Run system-wide global leave script
local sysdir=$(_smartcd_sysdir)
local confdir="$sysdir/scripts$smartcd_current_dir"
if [[ -f "$confdir/bash_leave" ]]; then
if (( ${SMARTCD_QUIET:-0} == 0 )); then
Expand Down Expand Up @@ -663,7 +671,6 @@ function _smartcd() {
done

# 1) Run system-wide global enter script
local sysdir=$(_smartcd_sysdir)
local confdir="$sysdir/scripts$smartcd_current_dir"
if [[ -f "$confdir/bash_enter" ]]; then
if (( ${SMARTCD_QUIET:-0} == 0 )); then
Expand Down Expand Up @@ -757,7 +764,7 @@ function _smartcd() {
}

function _smartcd_hook() {
local cwd="$(pwd)"
local cwd="$PWD"

# Skip this if we already ran smartcd for this directory
if [[ "x$cwd" != "$_smartcd_last_run_for" ]]; then
Expand Down Expand Up @@ -815,6 +822,9 @@ function setup_smartcd_chpwd_hook() {
return
fi
apush chpwd_functions _smartcd_hook
else
echo "chpwd hook is not supported for your shell."
return
fi
}

Expand Down Expand Up @@ -866,13 +876,13 @@ function _smartcd_file() {
if [[ -n $ZSH_VERSION ]]; then
setopt localoptions && setopt ksharrays
local this_file=${funcsourcetrace[0]}
this_file="${this_file%:*}"
# Remove line number and use absolute path.
this_file="${${this_file%:*}:a}"
else
local this_file="$BASH_SOURCE"
fi
if [[ "${this_file%/.smartcd/lib/core/smartcd}" == $HOME ]]; then
this_file=${this_file/#$HOME/\~}
fi
# Replace $HOME with ~ at the beginning.
this_file=${this_file/#$HOME/\~}
echo "$this_file"
}

Expand Down
6 changes: 3 additions & 3 deletions lib/core/smartcd_edit
@@ -1,7 +1,7 @@
# wrapper function to preserve legacy usage
function smartcd_edit() {
local type="$1"
local dir="${2:-$(pwd)}"
local dir="${2:-$PWD}"

case $type in
bash_enter|bash_leave) local file=$type; type="${type#bash_}";;
Expand Down Expand Up @@ -169,7 +169,7 @@ function _smartcd_file_check() {
return
fi
else
dir="$(pwd)"
dir="$PWD"
fi

if [[ -n $system ]]; then
Expand All @@ -178,7 +178,7 @@ function _smartcd_file_check() {
local base=$(_smartcd_base)
fi
if [[ $whence = "host" ]]; then
local host=${SMARTCD_HOSTNAME:-$(hostname)}
local host=${SMARTCD_HOSTNAME:-${HOST:-$(hostname)}}
base="$base/hosts/$host"
fi
local file="bash_$type"
Expand Down
6 changes: 3 additions & 3 deletions lib/core/smartcd_template
Expand Up @@ -68,7 +68,7 @@ EOF
if [[ -f "$base/templates/$name" ]]; then
echo "Template $name already exists, run \"smartcd template edit $name\" to modify it"
else
smartcd_template _init "$name" "$(pwd)"
smartcd_template _init "$name" "$PWD"
smartcd_template edit "$name"
fi
else
Expand Down Expand Up @@ -137,7 +137,7 @@ EOF
local name="$1"
if [[ -n $name ]]; then
if [[ -f "$base/templates/$name" ]]; then
local current_dir=$(pwd)
local current_dir=$PWD

local mode= line=
while builtin read -r line; do
Expand Down Expand Up @@ -201,7 +201,7 @@ EOF
esac
done < "$base/templates/$name"
IFS="$_old_ifs"
_smartcd_exec "$contents" "$(pwd)"
_smartcd_exec "$contents" "$PWD"
else
echo "Template $name not found"
fi
Expand Down
6 changes: 3 additions & 3 deletions lib/core/varstash
Expand Up @@ -71,7 +71,7 @@ function stash() {
fi

if [[ -n $1 ]] && [[ -z $run_from_smartcd ]] && [[ -z $run_from_autostash ]]; then
local working_dir="${varstash_dir:-$(pwd)}"
local working_dir="${varstash_dir:-$PWD}"
local smartcd_dir="$(_smartcd_base)/scripts$working_dir"
local help_action="stashing a variable"
local help_dir=$smartcd_dir
Expand Down Expand Up @@ -211,7 +211,7 @@ function stash() {

function autostash() {
if [[ -n $1 ]] && [[ -z $run_from_smartcd ]]; then
local working_dir="${varstash_dir:-$(pwd)}"
local working_dir="${varstash_dir:-$PWD}"
local smartcd_dir="$(_smartcd_base)/scripts$working_dir"
local help_action="autostashing a variable"
local help_dir=$smartcd_dir
Expand Down Expand Up @@ -241,7 +241,7 @@ function autostash() {

function unstash() {
if [[ -n $1 ]] && [[ -z $run_from_smartcd ]] && [[ -z $run_from_autounstash ]]; then
local working_dir=${varstash_dir:-$(pwd)}
local working_dir=${varstash_dir:-$PWD}
local smartcd_dir="$(_smartcd_base)/scripts$working_dir"
local help_action="unstashing a variable"
local help_dir=$smartcd_dir
Expand Down
10 changes: 5 additions & 5 deletions t/smartcd.t
@@ -1,7 +1,7 @@
# Set up smartcd
mkdir -p tmphome
oldhome=$HOME
export HOME="$(pwd)/tmphome"
export HOME="$PWD/tmphome"


# Load testing library
Expand Down Expand Up @@ -48,17 +48,17 @@ like "${output-_}" "this is a leaving test" "bash_leave executed successfully us
echo | smartcd edit enter "$dir"
echo | smartcd edit leave "$dir"

linkdest="$(pwd)/$dir/destination"
linkdest="$PWD/$dir/destination"
link="$dir/symlink"
mkdir -p "$linkdest"
ln -s destination "$link"
smartcd cd -P $link
is "_$(pwd)" "_$linkdest" "cd -P still works"
is "_$PWD" "_$linkdest" "cd -P still works"
smartcd cd ../..

spacedir="dir with a space"
mkdir -p "$spacedir"
smartcd_spacedir="$HOME/.smartcd/scripts$(pwd)/$spacedir"
smartcd_spacedir="$HOME/.smartcd/scripts$PWD/$spacedir"
mkdir -p "$smartcd_spacedir"
echo 'echo -n "1 "' > "$smartcd_spacedir/bash_enter"
echo 'echo 2' > "$smartcd_spacedir/bash_leave"
Expand All @@ -68,7 +68,7 @@ is "${output-_}" "1 2" "could enter and leave a directory with a space"
echo 'echo 4' > "$smartcd_spacedir/bash_leave"
spacedir2="dir with a space/subdir"
mkdir -p "$spacedir2"
smartcd_spacedir2="$HOME/.smartcd/scripts$(pwd)/$spacedir2"
smartcd_spacedir2="$HOME/.smartcd/scripts$PWD/$spacedir2"
mkdir -p "$smartcd_spacedir2"
echo 'echo -n "2 "' > "$smartcd_spacedir2/bash_enter"
echo 'echo -n "3 "' > "$smartcd_spacedir2/bash_leave"
Expand Down
4 changes: 2 additions & 2 deletions t/varstash.t
Expand Up @@ -109,12 +109,12 @@ is "_$(alast thing)" "_three four" "unstashed array"
oldhome=$HOME
stash HOME
mkdir -p tmphome
export HOME=$(pwd)/tmphome
export HOME=$PWD/tmphome

VARSTASH_AUTOCONFIG=1
oldshell=$SHELL
autostash SHELL
config_file="$HOME/.smartcd/scripts$(pwd)/bash_enter"
config_file="$HOME/.smartcd/scripts$PWD/bash_enter"
config_file_exists=$([[ -f $config_file ]] && echo "yes")

like "${config_file_exists-_}" "yes" "created smartcd file"
Expand Down

0 comments on commit 81e6d6e

Please sign in to comment.