mirror of
https://github.com/Tarrasch/zsh-autoenv.git
synced 2024-12-21 10:20:15 +02:00
Add helper functions for $PATH manipulation (#90)
This commit is contained in:
parent
2c8cfbcea8
commit
c60241f9d9
18
README.md
18
README.md
@ -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
|
||||
|
23
autoenv.zsh
23
autoenv.zsh
@ -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
29
tests/autoenv_utils.t
Normal 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:
|
Loading…
Reference in New Issue
Block a user