Skip to content

Commit 8bacc0e

Browse files
☈kingrking@sharpsaw.org
☈king
authored andcommitted
Improving zsh versions of path-manip.
1 parent c23854d commit 8bacc0e

File tree

3 files changed

+62
-27
lines changed

3 files changed

+62
-27
lines changed

lib/path-manip

+44-27
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ source ~/.../lib/tracefuncs
77

88
# List each directory in your PATH, one per line
99
...path-list() {
10-
local path_
11-
local d
12-
if [ "$#" -eq 1 ]; then eval path_=\$$1; else path_="$PATH"; fi
13-
for d in `echo $path_ | sed -e 's/:/ /g'`; do
14-
echo "$d"
15-
done
10+
if [ -n "$ZSH_VERSION" ]; then
11+
printf "%s\n" $path
12+
else
13+
local path_
14+
local d
15+
if [ "$#" -eq 1 ]; then eval path_=\$$1; else path_="$PATH"; fi
16+
for d in `echo $path_ | sed -e 's/:/ /g'`; do
17+
echo "$d"
18+
done
19+
fi
1620
}
1721

1822
# Remove a directory from your PATH
@@ -21,43 +25,56 @@ source ~/.../lib/tracefuncs
2125
...debug "Removed $* from \$PATH"
2226
}
2327
...quiet-path-remove() {
24-
local path_
25-
local d
26-
local p=""
27-
if [ "$#" -eq 2 ]; then eval path_=\$$2; else path_="$PATH"; fi
28-
for d in `echo $path_ | sed -e 's/:/ /g'`; do
29-
if [ "$d" != "$1" ]; then
30-
if [ "$p" = "" ]; then
31-
p="$d"
32-
else
33-
p="$p:$d"
28+
if [ -n "$ZSH_VERSION" ]; then
29+
path=(${path:#$1})
30+
else
31+
local path_
32+
local d
33+
local p=""
34+
if [ "$#" -eq 2 ]; then eval path_=\$$2; else path_="$PATH"; fi
35+
for d in `echo $path_ | sed -e 's/:/ /g'`; do
36+
if [ "$d" != "$1" ]; then
37+
if [ "$p" = "" ]; then
38+
p="$d"
39+
else
40+
p="$p:$d"
41+
fi
3442
fi
35-
fi
36-
done
37-
if [ "$#" -eq 2 ]; then eval $2=\$p; else PATH="$p"; fi
43+
done
44+
if [ "$#" -eq 2 ]; then eval $2=\$p; else PATH="$p"; fi
45+
fi
3846
}
3947

4048
# Add a directory to the start of your PATH while removing old references.
4149
...path-prepend() {
42-
local path_
4350
...quiet-path-remove $*
44-
if [ "$#" -eq 2 ]; then eval path_=\$$2; else path_="$PATH"; fi
45-
path_="$1:$path_"
46-
if [ "$#" -eq 2 ]; then eval "$2=$path_"; else PATH="$path_"; fi
51+
if [ -n "$ZSH_VERSION" ]; then
52+
path=($1 $path)
53+
else
54+
local path_
55+
if [ "$#" -eq 2 ]; then eval path_=\$$2; else path_="$PATH"; fi
56+
path_="$1:$path_"
57+
if [ "$#" -eq 2 ]; then eval "$2=$path_"; else PATH="$path_"; fi
58+
fi
4759
...debug "Prepended $* to \$PATH"
4860
}
4961

5062
# Add a directory to the end of your PATH while removing old references.
5163
...path-append() {
52-
local path_
5364
...quiet-path-remove $*
54-
if [ "$#" -eq 2 ]; then eval path_=\$$2; else path_="$PATH"; fi
55-
path_="$path_:$1"
56-
if [ "$#" -eq 2 ]; then eval $2=\$path_; else PATH="$path_"; fi
65+
if [ -n "$ZSH_VERSION" ]; then
66+
path=($path $1)
67+
else
68+
local path_
69+
if [ "$#" -eq 2 ]; then eval path_=\$$2; else path_="$PATH"; fi
70+
path_="$path_:$1"
71+
if [ "$#" -eq 2 ]; then eval $2=\$path_; else PATH="$path_"; fi
72+
fi
5773
...debug "Appended $* to \$PATH"
5874
}
5975

6076
# Copyright © 2011 Ingy dot Net <[email protected]>
6177
#
6278
# This library is free software, distributed under the ISC License.
6379
# See the LICENSE file distributed with this library.
80+
# vim:ft=zsh

t/_path-manip

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
source ../lib/path-manip
2+
ok() { echo -n "$@ "; read; eval "$@"; ...path-list; }
3+
4+
...path-list
5+
echo "# (☝ starting \$PATH)"
6+
ok ...path-prepend early
7+
ok ...path-remove early
8+
ok ...path-append late-then-early
9+
ok ...path-prepend late-then-early
10+
11+
# vim:ft=zsh

t/path-manip

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
echo "# This is a lazy test. Just look at the output to figure out if it's OK"
3+
echo "# bash"
4+
bash -c 'source _path-manip'
5+
echo
6+
echo "# now zsh"
7+
zsh -c 'source _path-manip'

0 commit comments

Comments
 (0)