Files
z/z.sh
T

135 lines
3.8 KiB
Bash
Raw Normal View History

2009-06-18 22:15:33 -07:00
#!/bin/bash
2009-06-26 11:22:25 -04:00
# maintains a jump-list of the directories you actually use
#
# INSTALL:
# * put something like this in your .bashrc:
# . /path/to/z.sh
# * cd around for a while to build up the db
# * PROFIT!!
#
# USE:
# * z foo # goes to most frecent dir matching foo
# * z foo bar # goes to most frecent dir matching foo and bar
# * z -r rank # goes to highest ranked dir matching foo
# * z -t rank # goes to most recently accessed dir matching foo
# * z -l foo # list all dirs matching foo (by frecency)
2009-06-18 22:15:33 -07:00
2009-06-19 13:58:10 -04:00
z() {
2009-06-18 22:15:33 -07:00
local datafile=$HOME/.z
if [ "$1" = "--add" ]; then
# add
shift
# $HOME isn't worth matching
[ "$*" = "$HOME" ] && return
2009-06-22 21:48:30 -04:00
awk -v p="$*" -v t="$(date +%s)" -F"|" '
BEGIN { rank[p] = 1; time[p] = t }
2009-06-18 22:15:33 -07:00
$2 >= 1 {
2009-06-22 21:48:30 -04:00
if( $1 == p ) {
rank[$1] = $2 + 1
time[$1] = t
2009-06-18 22:15:33 -07:00
} else {
2009-06-22 21:48:30 -04:00
rank[$1] = $2
time[$1] = $3
2009-06-18 22:15:33 -07:00
}
count += $2
}
END {
if( count > 1000 ) {
2009-06-24 20:05:03 -04:00
for( i in rank ) print i "|" 0.9*rank[i] "|" time[i] # aging
2009-06-22 21:48:30 -04:00
} else for( i in rank ) print i "|" rank[i] "|" time[i]
2009-06-18 22:15:33 -07:00
}
' $datafile 2>/dev/null > $datafile.tmp
mv -f $datafile.tmp $datafile
elif [ "$1" = "--complete" ]; then
# tab completion
awk -v q="$2" -F"|" '
2009-06-22 21:48:30 -04:00
BEGIN { split(substr(q,3),fnd," ") }
2009-06-18 22:15:33 -07:00
{
if( system("test -d \"" $1 "\"") ) next
2009-06-22 21:48:30 -04:00
for( i in fnd ) $1 !~ fnd[i] && $1 = ""; if( $1 ) print $1
2009-06-18 22:15:33 -07:00
}
' $datafile 2>/dev/null
else
# list/go
2009-06-22 21:48:30 -04:00
while [ "$1" ]; do case "$1" in
-h) echo "z [-h][-l][-r][-t] args" >&2; return;;
2009-06-18 22:15:33 -07:00
-l) local list=1;;
2009-06-19 01:38:11 -04:00
-r) local typ="rank";;
-t) local typ="recent";;
2009-06-22 21:48:30 -04:00
--) while [ "$1" ]; do shift; local fnd="$fnd $1";done;;
*) local fnd="$fnd $1";;
esac; local last=$1; shift; done
[ "$fnd" ] || local list=1
2009-06-18 22:15:33 -07:00
# if we hit enter on a completion just go there
2009-06-22 21:48:30 -04:00
[ -d "$last" ] && cd "$last" && return
2009-06-26 11:26:07 -04:00
[ -f "$datafile" ] || return
cd="$(awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -v tmpfl="$datafile.tmp" -F"|" '
2009-06-18 22:15:33 -07:00
function frecent(rank, 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
}
2009-06-22 21:48:30 -04:00
function output(files, toopen, override) {
2009-06-18 22:15:33 -07:00
if( list ) {
2009-06-19 01:59:29 -04:00
if( typ == "recent" ) {
cmd = "sort -nr >&2"
} else cmd = "sort -n >&2"
2009-06-22 21:48:30 -04:00
for( i in files ) if( files[i] ) printf "%-15s %s\n", files[i], i | cmd
if( override ) printf "%-15s %s\n", "common:", override > "/dev/stderr"
2009-06-18 22:15:33 -07:00
} else {
2009-06-22 21:48:30 -04:00
if( override ) toopen = override
print toopen
2009-06-18 22:15:33 -07:00
}
}
2009-06-22 21:48:30 -04:00
function common(matches, fnd, nc) {
for( i in matches ) {
2009-06-23 12:28:25 -04:00
if( matches[i] && (!short || length(i) < length(short)) ) short = i
2009-06-18 22:15:33 -07:00
}
2009-06-23 12:28:25 -04:00
if( short == "/" ) return
for( i in matches ) if( matches[i] && i !~ short ) x = 1
2009-06-18 22:15:33 -07:00
if( x ) return
if( nc ) {
2009-06-23 12:28:25 -04:00
for( i in fnd ) if( tolower(short) !~ tolower(fnd[i]) ) x = 1
} else for( i in fnd ) if( short !~ fnd[i] ) x = 1
if( !x ) return short
2009-06-18 22:15:33 -07:00
}
2009-06-19 09:48:42 -04:00
BEGIN { split(q, a, " ") }
2009-06-18 22:15:33 -07:00
{
if( system("test -d \"" $1 "\"") ) next
print $0 >> tmpfl
2009-06-19 01:33:17 -04:00
if( typ == "rank" ) {
2009-06-18 22:15:33 -07:00
f = $2
2009-06-19 01:33:17 -04:00
} else if( typ == "recent" ) {
2009-06-19 01:59:29 -04:00
f = t-$3
2009-06-18 22:15:33 -07:00
} else f = frecent($2, $3)
case[$1] = nocase[$1] = f
2009-06-20 12:23:56 -04:00
for( i in a ) {
if( $1 !~ a[i] ) delete case[$1]
if( tolower($1) !~ tolower(a[i]) ) delete nocase[$1]
}
2009-06-18 22:15:33 -07:00
if( case[$1] > oldf ) {
cx = $1
oldf = case[$1]
} else if( nocase[$1] > noldf ) {
ncx = $1
noldf = nocase[$1]
}
}
END {
if( cx ) {
output(case, cx, common(case, a, 0))
2009-06-20 12:23:56 -04:00
} else if( ncx ) output(nocase, ncx, common(nocase, a, 1))
2009-06-18 22:15:33 -07:00
}
' $datafile)"
mv -f $datafile.tmp $datafile
[ "$cd" ] && cd "$cd"
fi
}
# tab completion
2009-06-19 13:58:10 -04:00
complete -C 'z --complete "$COMP_LINE"' z
2009-06-18 22:15:33 -07:00
# populate directory list. avoid clobbering other PROMPT_COMMANDs.
2009-06-22 22:43:14 -04:00
echo $PROMPT_COMMAND | grep -q "z"
[ $? -gt 0 ] && PROMPT_COMMAND='z --add "$(pwd -P)";'"$PROMPT_COMMAND"