Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Improving zsh versions of path-manip.

  • Loading branch information...
commit 8bacc0e1c2442c9ae4bd6da5cb510e6ee7e520fc 1 parent c23854d
@rking rking authored rking@sharpsaw.org committed
Showing with 62 additions and 27 deletions.
  1. +44 −27 lib/path-manip
  2. +11 −0 t/_path-manip
  3. +7 −0 t/path-manip
View
71 lib/path-manip
@@ -7,12 +7,16 @@ source ~/.../lib/tracefuncs
# List each directory in your PATH, one per line
...path-list() {
- local path_
- local d
- if [ "$#" -eq 1 ]; then eval path_=\$$1; else path_="$PATH"; fi
- for d in `echo $path_ | sed -e 's/:/ /g'`; do
- echo "$d"
- done
+ if [ -n "$ZSH_VERSION" ]; then
+ printf "%s\n" $path
+ else
+ local path_
+ local d
+ if [ "$#" -eq 1 ]; then eval path_=\$$1; else path_="$PATH"; fi
+ for d in `echo $path_ | sed -e 's/:/ /g'`; do
+ echo "$d"
+ done
+ fi
}
# Remove a directory from your PATH
@@ -21,39 +25,51 @@ source ~/.../lib/tracefuncs
...debug "Removed $* from \$PATH"
}
...quiet-path-remove() {
- local path_
- local d
- local p=""
- if [ "$#" -eq 2 ]; then eval path_=\$$2; else path_="$PATH"; fi
- for d in `echo $path_ | sed -e 's/:/ /g'`; do
- if [ "$d" != "$1" ]; then
- if [ "$p" = "" ]; then
- p="$d"
- else
- p="$p:$d"
+ if [ -n "$ZSH_VERSION" ]; then
+ path=(${path:#$1})
+ else
+ local path_
+ local d
+ local p=""
+ if [ "$#" -eq 2 ]; then eval path_=\$$2; else path_="$PATH"; fi
+ for d in `echo $path_ | sed -e 's/:/ /g'`; do
+ if [ "$d" != "$1" ]; then
+ if [ "$p" = "" ]; then
+ p="$d"
+ else
+ p="$p:$d"
+ fi
fi
- fi
- done
- if [ "$#" -eq 2 ]; then eval $2=\$p; else PATH="$p"; fi
+ done
+ if [ "$#" -eq 2 ]; then eval $2=\$p; else PATH="$p"; fi
+ fi
}
# Add a directory to the start of your PATH while removing old references.
...path-prepend() {
- local path_
...quiet-path-remove $*
- if [ "$#" -eq 2 ]; then eval path_=\$$2; else path_="$PATH"; fi
- path_="$1:$path_"
- if [ "$#" -eq 2 ]; then eval "$2=$path_"; else PATH="$path_"; fi
+ if [ -n "$ZSH_VERSION" ]; then
+ path=($1 $path)
+ else
+ local path_
+ if [ "$#" -eq 2 ]; then eval path_=\$$2; else path_="$PATH"; fi
+ path_="$1:$path_"
+ if [ "$#" -eq 2 ]; then eval "$2=$path_"; else PATH="$path_"; fi
+ fi
...debug "Prepended $* to \$PATH"
}
# Add a directory to the end of your PATH while removing old references.
...path-append() {
- local path_
...quiet-path-remove $*
- if [ "$#" -eq 2 ]; then eval path_=\$$2; else path_="$PATH"; fi
- path_="$path_:$1"
- if [ "$#" -eq 2 ]; then eval $2=\$path_; else PATH="$path_"; fi
+ if [ -n "$ZSH_VERSION" ]; then
+ path=($path $1)
+ else
+ local path_
+ if [ "$#" -eq 2 ]; then eval path_=\$$2; else path_="$PATH"; fi
+ path_="$path_:$1"
+ if [ "$#" -eq 2 ]; then eval $2=\$path_; else PATH="$path_"; fi
+ fi
...debug "Appended $* to \$PATH"
}
@@ -61,3 +77,4 @@ source ~/.../lib/tracefuncs
#
# This library is free software, distributed under the ISC License.
# See the LICENSE file distributed with this library.
+# vim:ft=zsh
View
11 t/_path-manip
@@ -0,0 +1,11 @@
+source ../lib/path-manip
+ok() { echo -n "$@ "; read; eval "$@"; ...path-list; }
+
+...path-list
+echo "# (☝ starting \$PATH)"
+ok ...path-prepend early
+ok ...path-remove early
+ok ...path-append late-then-early
+ok ...path-prepend late-then-early
+
+# vim:ft=zsh
View
7 t/path-manip
@@ -0,0 +1,7 @@
+#!/bin/sh
+echo "# This is a lazy test. Just look at the output to figure out if it's OK"
+echo "# bash"
+bash -c 'source _path-manip'
+echo
+echo "# now zsh"
+zsh -c 'source _path-manip'
Please sign in to comment.
Something went wrong with that request. Please try again.