Cleanup
Rename variables, improve comments, etc, for clarity and to better fit my programmerly preferences.
This commit is contained in:
1 parent
66b9ab69c1
commit
0109444865
1 file changed
+47
-43
@@ -46,9 +46,8 @@ _z() {
|
||||
[ "$*" = "$exclude" ] && return
|
||||
done
|
||||
|
||||
# maintain the file
|
||||
local tempfile
|
||||
tempfile="$datafile.$RANDOM"
|
||||
# maintain the data file
|
||||
local tempfile="$datafile.$RANDOM"
|
||||
while read line; do
|
||||
# only count directories
|
||||
[ -d "${line%%\|*}" ] && echo $line
|
||||
@@ -58,6 +57,7 @@ _z() {
|
||||
time[path] = now
|
||||
}
|
||||
$2 >= 1 {
|
||||
# drop ranks below 1
|
||||
if( $1 == path ) {
|
||||
rank[$1] = $2 + 1
|
||||
time[$1] = now
|
||||
@@ -70,10 +70,11 @@ _z() {
|
||||
END {
|
||||
if( count > 6000 ) {
|
||||
# aging
|
||||
for( i in rank ) print i "|" 0.99*rank[i] "|" time[i]
|
||||
} else for( i in rank ) print i "|" rank[i] "|" time[i]
|
||||
for( x in rank ) print x "|" 0.99*rank[x] "|" time[x]
|
||||
} else for( x in rank ) print x "|" rank[x] "|" time[x]
|
||||
}
|
||||
' 2>/dev/null >| "$tempfile"
|
||||
# do our best to avoid clobbering the datafile in a race condition
|
||||
if [ $? -ne 0 -a -f "$datafile" ]; then
|
||||
env rm -f "$tempfile"
|
||||
else
|
||||
@@ -86,14 +87,14 @@ _z() {
|
||||
[ -d "${line%%\|*}" ] && echo $line
|
||||
done < "$datafile" | awk -v q="$2" -F"|" '
|
||||
BEGIN {
|
||||
if( q == tolower(q) ) nocase = 1
|
||||
split(substr(q,3),fnd," ")
|
||||
if( q == tolower(q) ) imatch = 1
|
||||
split(substr(q, 3), fnd, " ")
|
||||
}
|
||||
{
|
||||
if( nocase ) {
|
||||
for( i in fnd ) tolower($1) !~ tolower(fnd[i]) && $1 = ""
|
||||
if( imatch ) {
|
||||
for( x in fnd ) tolower($1) !~ tolower(fnd[x]) && $1 = ""
|
||||
} else {
|
||||
for( i in fnd ) $1 !~ fnd[i] && $1 = ""
|
||||
for( x in fnd ) $1 !~ fnd[x] && $1 = ""
|
||||
}
|
||||
if( $1 ) print $1
|
||||
}
|
||||
@@ -130,66 +131,69 @@ _z() {
|
||||
done < "$datafile" | awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" '
|
||||
function frecent(rank, time) {
|
||||
# relate frequency and time
|
||||
dx = t-time
|
||||
if( dx < 3600 ) return rank*4
|
||||
if( dx < 86400 ) return rank*2
|
||||
if( dx < 604800 ) return rank/2
|
||||
return rank/4
|
||||
dx = t - time
|
||||
if( dx < 3600 ) return rank * 4
|
||||
if( dx < 86400 ) return rank * 2
|
||||
if( dx < 604800 ) return rank / 2
|
||||
return rank / 4
|
||||
}
|
||||
function output(files, toopen, common) {
|
||||
function output(files, out, common) {
|
||||
# list or return the desired directory
|
||||
if( list ) {
|
||||
cmd = "sort -n >&2"
|
||||
for( i in files ) {
|
||||
if( files[i] ) printf "%-10s %s\n", files[i], i | cmd
|
||||
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 ) toopen = common
|
||||
print toopen
|
||||
if( common ) out = common
|
||||
print out
|
||||
}
|
||||
}
|
||||
function common(matches) {
|
||||
# find the common root of a list of dirs, if it exists
|
||||
for( i in matches ) {
|
||||
if( matches[i] && (!short || length(i) < length(short)) ) {
|
||||
short = i
|
||||
# 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.
|
||||
# the original. yeah, this escaping is awful.
|
||||
clean_short = short
|
||||
gsub(/[\(\)\[\]\|]/, "\\\\&", clean_short)
|
||||
for( i in matches ) if( matches[i] && i !~ clean_short ) return
|
||||
for( x in matches ) if( matches[x] && x !~ clean_short ) return
|
||||
return short
|
||||
}
|
||||
BEGIN { split(q, a, " "); oldf = noldf = -9999999999 }
|
||||
BEGIN { split(q, words, " "); hi_rank = ihi_rank = -9999999999 }
|
||||
{
|
||||
if( typ == "rank" ) {
|
||||
f = $2
|
||||
rank = $2
|
||||
} else if( typ == "recent" ) {
|
||||
f = $3-t
|
||||
} else f = frecent($2, $3)
|
||||
wcase[$1] = nocase[$1] = f
|
||||
for( i in a ) {
|
||||
if( $1 !~ a[i] ) delete wcase[$1]
|
||||
if( tolower($1) !~ tolower(a[i]) ) delete nocase[$1]
|
||||
rank = $3 - t
|
||||
} else rank = frecent($2, $3)
|
||||
matches[$1] = imatches[$1] = rank
|
||||
for( x in words ) {
|
||||
if( $1 !~ words[x] ) delete matches[$1]
|
||||
if( tolower($1) !~ tolower(words[x]) ) delete imatches[$1]
|
||||
}
|
||||
if( wcase[$1] && wcase[$1] > oldf ) {
|
||||
cx = $1
|
||||
oldf = wcase[$1]
|
||||
} else if( nocase[$1] && nocase[$1] > noldf ) {
|
||||
ncx = $1
|
||||
noldf = nocase[$1]
|
||||
if( matches[$1] && matches[$1] > hi_rank ) {
|
||||
best_match = $1
|
||||
hi_rank = matches[$1]
|
||||
} else if( imatches[$1] && imatches[$1] > ihi_rank ) {
|
||||
ibest_match = $1
|
||||
ihi_rank = imatches[$1]
|
||||
}
|
||||
}
|
||||
END {
|
||||
if( cx ) {
|
||||
output(wcase, cx, common(wcase))
|
||||
} else if( ncx ) output(nocase, ncx, common(nocase))
|
||||
# prefer case sensitive
|
||||
if( best_match ) {
|
||||
output(matches, best_match, common(matches))
|
||||
} else if( ibest_match ) {
|
||||
output(imatches, ibest_match, common(imatches))
|
||||
}
|
||||
}
|
||||
')"
|
||||
[ $? -gt 0 ] && return
|
||||
|
||||
Reference in new issue
Block a user