Skip to content

Commit 9e41a50

Browse files
committed
Merge branch 'master' of git://github.com/rupa/z into solaris-compatibility
2 parents 27e8ce3 + ea5ec78 commit 9e41a50

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

z.sh

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ _z() {
4242
[ -z "$_Z_OWNER" -a -f "$datafile" -a ! -O "$datafile" ] && return
4343

4444
_z_dirs () {
45+
local line
4546
while read line; do
4647
# only count directories
47-
[ -d "${line%%\|*}" ] && echo $line
48+
[ -d "${line%%\|*}" ] && echo "$line"
4849
done < "$datafile"
4950
return 0
5051
}
@@ -64,7 +65,7 @@ _z() {
6465

6566
# maintain the data file
6667
local tempfile="$datafile.$RANDOM"
67-
awk < <(_z_dirs 2>/dev/null) -v path="$*" -v now="$(date +%s)" -F"|" '
68+
_z_dirs | awk -v path="$*" -v now="$(date +%s)" -F"|" '
6869
BEGIN {
6970
rank[path] = 1
7071
time[path] = now
@@ -97,17 +98,15 @@ _z() {
9798

9899
# tab completion
99100
elif [ "$1" = "--complete" -a -s "$datafile" ]; then
100-
while read line; do
101-
[ -d "${line%%\|*}" ] && echo $line
102-
done < "$datafile" | awk -v q="$2" -F"|" '
101+
_z_dirs | awk -v q="$2" -F"|" '
103102
BEGIN {
104-
if( q == tolower(q) ) imatch = 1
105103
q = substr(q, 3)
106-
gsub(" ", ".*", q)
104+
if( q == tolower(q) ) imatch = 1
105+
gsub(/ /, ".*", q)
107106
}
108107
{
109108
if( imatch ) {
110-
if( tolower($1) ~ tolower(q) ) print $1
109+
if( tolower($1) ~ q ) print $1
111110
} else if( $1 ~ q ) print $1
112111
}
113112
' 2>/dev/null
@@ -148,19 +147,21 @@ _z() {
148147
if( dx < 604800 ) return rank / 2
149148
return rank / 4
150149
}
151-
function output(files, out, common) {
150+
function output(matches, best_match, common) {
152151
# list or return the desired directory
153152
if( list ) {
154153
cmd = "sort -n >&2"
155-
for( x in files ) {
156-
if( files[x] ) printf "%-10s %s\n", files[x], x | cmd
154+
for( x in matches ) {
155+
if( matches[x] ) {
156+
printf "%-10s %s\n", matches[x], x | cmd
157+
}
157158
}
158159
if( common ) {
159160
printf "%-10s %s\n", "common:", common > "/dev/stderr"
160161
}
161162
} else {
162-
if( common ) out = common
163-
print out
163+
if( common ) best_match = common
164+
print best_match
164165
}
165166
}
166167
function common(matches) {
@@ -171,11 +172,9 @@ _z() {
171172
}
172173
}
173174
if( short == "/" ) return
174-
# use a copy to escape special characters, as we want to return
175-
# the original. yeah, this escaping is awful.
176-
clean_short = short
177-
gsub(/\[\(\)\[\]\|\]/, "\\\\&", clean_short)
178-
for( x in matches ) if( matches[x] && x !~ clean_short ) return
175+
for( x in matches ) if( matches[x] && index(x, short) != 1 ) {
176+
return
177+
}
179178
return short
180179
}
181180
BEGIN {

0 commit comments

Comments
 (0)