]> Sergey Matveev's repositories - zsh-autoenv.git/commitdiff
Add helper functions for $PATH manipulation (#90)
authorDaniel Hahler <github@thequod.de>
Fri, 14 Sep 2018 01:46:43 +0000 (03:46 +0200)
committerGitHub <noreply@github.com>
Fri, 14 Sep 2018 01:46:43 +0000 (03:46 +0200)
README.md
autoenv.zsh
tests/autoenv_utils.t [new file with mode: 0644]

index c49e66fe78ec206ce330b65add38c57a9a9ec8d0..fe81fe643195e6e6f088ecb38a6ce9e56cf14d66 100644 (file)
--- a/README.md
+++ b/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
index e70eec165000b6a6fdfc99a0191caeaaa84ce73c..598443b65bb68ef2d8b016c3af045a82541ae4d5 100644 (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
diff --git a/tests/autoenv_utils.t b/tests/autoenv_utils.t
new file mode 100644 (file)
index 0000000..e3d2271
--- /dev/null
@@ -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: