cdup: improvements
This commit is contained in:
parent
b23ed68195
commit
127e2e5aa2
2 changed files with 54 additions and 6 deletions
|
|
@ -1,20 +1,64 @@
|
||||||
|
|
||||||
|
PROJECT_ROOT_INDICATORS=(.git .hg package.json build.sbt)
|
||||||
|
|
||||||
|
# These commands find the topmost (i.e. shortest full path) project directory in
|
||||||
|
# the hierarchy of the given directory.
|
||||||
|
# cdup: changes to the match
|
||||||
|
# _cdup: prints the match
|
||||||
|
# _cdup_r: sets ~r as named dir for the match (set up as a chpwd hook)
|
||||||
|
#
|
||||||
|
# args: (cdup|_cdup) [-c] DIR [PROJECT_ROOT_INDICATOR...]
|
||||||
|
# -c / --closest return the nearest project directory, i.e. least steps up,
|
||||||
|
# possibly including the current directory
|
||||||
|
#
|
||||||
|
# DIR is starting point for the search
|
||||||
|
# A PROJECT_ROOT_INDICATOR are files to look for to detect project roots. If
|
||||||
|
# not given, a default list is used. $PROJECT_ROOT_INDICATORS can be used to
|
||||||
|
# override the default globally
|
||||||
|
|
||||||
_cdup() {
|
_cdup() {
|
||||||
|
zparseopts -D -E c=closest -closest=closest
|
||||||
local dir=$1; shift
|
local dir=$1; shift
|
||||||
local targets=("$@")
|
local targets=("$@")
|
||||||
|
local match nextmatch
|
||||||
for target in $targets; do
|
for target in $targets; do
|
||||||
if [[ -e $dir/$target ]]; then
|
if [[ -e $dir/$target ]]; then
|
||||||
cd $dir
|
if [[ $closest ]]; then
|
||||||
return 0
|
echo $dir
|
||||||
|
return
|
||||||
|
else
|
||||||
|
match=$dir
|
||||||
|
break
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
if [[ $dir == "/" ]]; then return 1
|
if [[ $dir != "/" ]]; then
|
||||||
else _cdup ${dir:h} ${targets[@]}
|
nextmatch=$(_cdup ${dir:h} ${targets[@]})
|
||||||
|
match=${nextmatch:-$match}
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ $match ]]; then echo $match; return 0;
|
||||||
|
else return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
cdup() {
|
cdup() {
|
||||||
if [[ ${#@} -gt 0 ]]; then _cdup ${PWD:h} "$@"
|
zparseopts -D -E c=closest -closest=closest
|
||||||
else _cdup ${PWD:h} .git .hg package.json build.sbt
|
local target=$(_cdup $closest ${PWD:h} ${@:-$PROJECT_ROOT_INDICATORS})
|
||||||
|
if [[ $? != 0 ]]; then return 1;
|
||||||
|
else cd $target
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_cdup_r () {
|
||||||
|
_cdup_project_root=$(_cdup ${PWD} ${PROJECT_ROOT_INDICATORS})
|
||||||
|
hash -d r=$_cdup_project_root
|
||||||
|
}
|
||||||
|
|
||||||
|
_prompt_cdup() {
|
||||||
|
[[ -n $_cdup_project_root ]] && echo "project: ${_cdup_project_root:h:t}/${_cdup_project_root:t}"
|
||||||
|
}
|
||||||
|
|
||||||
|
autoload -U add-zsh-hook
|
||||||
|
add-zsh-hook chpwd _cdup_r
|
||||||
|
_cdup_r
|
||||||
|
|
|
||||||
4
zshrc
4
zshrc
|
|
@ -128,6 +128,10 @@ if exists mail; then
|
||||||
__chromaz_extra_left+=_prompt_cron_mails
|
__chromaz_extra_left+=_prompt_cron_mails
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if exists _prompt_cdup; then
|
||||||
|
__chromaz_extra_left+=_prompt_cdup
|
||||||
|
fi
|
||||||
|
|
||||||
tabs -4
|
tabs -4
|
||||||
#zprof
|
#zprof
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue