Add helper functions for $PATH manipulation (#90)

This commit is contained in:
Daniel Hahler 2018-09-14 03:46:43 +02:00 committed by GitHub
parent 2c8cfbcea8
commit c60241f9d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 0 deletions

View File

@ -149,6 +149,24 @@ zsh-autoenv works automatically once installed.
You can use ``autoenv-edit`` to edit the nearest/current autoenv files.
It will use ``$AUTOENV_EDITOR``, ``$EDITOR``, or ``vim`` for editing.
## Helper functions
The following helper functions are available:
### autoenv_append_path
Appends path(s) to `$path` (`$PATH`), if they are not in there already.
### autoenv_prepend_path
Prepends path(s) to `$path` (`$PATH`), if they are not in there already.
### autoenv_remove_path
Removes path(s) from `$path` (`$PATH`).
Returns 0 in case `$path` has changed, 1 otherwise.
## Recipes
### Automatically activate Python virtualenvs

View File

@ -59,6 +59,29 @@ autoenv_source_parent() {
fi
}
autoenv_append_path() {
local i
for i; do
(( ${path[(i)$i]} <= ${#path} )) && continue
path+=($i)
done
}
autoenv_prepend_path() {
local i
for i; do
(( ${path[(i)$i]} <= ${#path} )) && continue
path=($i $path)
done
}
autoenv_remove_path() {
local i
local old_path=$path
for i; do
path=("${(@)path:#$i}")
done
[[ $old_path != $path ]]
}
# Internal functions. {{{
# Internal: stack of loaded env files (i.e. entered directories). {{{
typeset -g -a _autoenv_stack_entered

29
tests/autoenv_utils.t Normal file
View File

@ -0,0 +1,29 @@
Tests for provided utils/helpers.
$ source $TESTDIR/setup.zsh || return 1
$ PATH=
$ autoenv_prepend_path custom_path
$ echo $PATH
custom_path
$ autoenv_prepend_path custom_path
$ echo $PATH
custom_path
$ autoenv_prepend_path another_path a_third_one
$ echo $PATH
a_third_one:another_path:custom_path
$ autoenv_remove_path another_path a_third_one
$ echo $PATH
custom_path
$ autoenv_remove_path does_not_exist
[1]
$ echo $PATH
custom_path
$ autoenv_remove_path custom_path
$ echo PATH:$PATH
PATH: