Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 18 additions & 23 deletions z.sh
Original file line number Diff line number Diff line change
Expand Up @@ -137,49 +137,44 @@ _z() {
if( dx < 604800 ) return rank / 2
return rank / 4
}
function output(files, out, common) {
function output(files, out) {
# list or return the desired directory
if( list ) {
cmd = "sort -n >&2"
for( x in files ) {
if( files[x] ) printf "%-10s %s\n", files[x], x | cmd
}
if( common ) {
printf "%-10s %s\n", "common:", common > "/dev/stderr"
}
} else {
if( common ) out = common
print out
}
}
function common(matches) {
# find the common root of a list of matches, if it exists
for( x in matches ) {
if( matches[x] && (!short || length(x) < length(short)) ) {
short = x
}
}
if( short == "/" ) return
# use a copy to escape special characters, as we want to return
# the original. yeah, this escaping is awful.
clean_short = short
gsub(/\[\(\)\[\]\|\]/, "\\\\&", clean_short)
for( x in matches ) if( matches[x] && x !~ clean_short ) return
return short
function matchpast(str, ere, thresh) {
n = match(str, ere)
if (n + RLENGTH > thresh)
return 1
return 0
}
BEGIN {
gsub(" ", ".*", q)
hi_rank = ihi_rank = -9999999999
}
{
n = split($1, broken, "/")
threshhold = length($1) - length(broken[n])

if( typ == "rank" ) {
rank = $2
} else if( typ == "recent" ) {
rank = $3 - t
} else rank = frecent($2, $3)
if( $1 ~ q ) {

if (list) # always match if we are listing them
matches[$1] = rank
if (matchpast($1, q, threshhold))
matches[$1] = rank
} else if( tolower($1) ~ tolower(q) ) imatches[$1] = rank
else if (matchpast(tolower($1), tolower(q), threshhold))
imatches[$1] = rank

if( matches[$1] && matches[$1] > hi_rank ) {
best_match = $1
hi_rank = matches[$1]
Expand All @@ -191,9 +186,9 @@ _z() {
END {
# prefer case sensitive
if( best_match ) {
output(matches, best_match, common(matches))
output(matches, best_match)
} else if( ibest_match ) {
output(imatches, ibest_match, common(imatches))
output(imatches, ibest_match)
}
}
')"
Expand Down