Files
z/z.sh
T

225 lines
6.1 KiB
Bash
Raw Normal View History

# Copyright (c) 2009 rupa deadwyler under the WTFPL license
2009-06-18 22:15:33 -07:00
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/.zshrc:
2009-06-26 11:22:25 -04:00
# . /path/to/z.sh
# * cd around for a while to build up the db
# * PROFIT!!
2012-07-11 17:07:54 -04:00
# * optionally:
# set $_Z_CMD in .bashrc/.zshrc to change the command (default z).
# set $_Z_DATA in .bashrc/.zshrc to change the datafile (default ~/.z).
# set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
# set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself.
2013-01-11 12:28:23 -05:00
# set $_Z_EXCLUDE_DIRS to an array of directories to exclude.
2009-06-26 11:22:25 -04:00
#
# USE:
2011-02-06 19:12:39 -05:00
# * z foo # cd to most frecent dir matching foo
# * z foo bar # cd to most frecent dir matching foo and bar
# * z -r foo # cd to highest ranked dir matching foo
# * z -t foo # cd to most recently accessed dir matching foo
# * z -l foo # list matches instead of cd
# * z -c foo # restrict matches to subdirs of $PWD
2009-06-18 22:15:33 -07:00
case $- in
*i*) ;;
*) echo 'ERROR: z.sh is meant to be sourced, not directly executed.'
esac
_z() {
2011-08-06 22:46:15 -04:00
local datafile="${_Z_DATA:-$HOME/.z}"
# bail out if we don't own ~/.z (we're another user but our ENV is still set)
[ -f "$datafile" -a ! -O "$datafile" ] && return
# add entries
2009-06-18 22:15:33 -07:00
if [ "$1" = "--add" ]; then
shift
2009-06-18 22:15:33 -07:00
# $HOME isn't worth matching
[ "$*" = "$HOME" ] && return
2013-01-11 12:28:23 -05:00
# don't track excluded dirs
local exclude
for exclude in "${_Z_EXCLUDE_DIRS[@]}"; do
[ "$*" = "$exclude" ] && return
2013-01-11 12:28:23 -05:00
done
# maintain the file
local tempfile
tempfile="$(mktemp $datafile.XXXXXX)" || return
2012-06-22 20:24:43 -04:00
while read line; do
2012-06-22 10:22:22 -04:00
[ -d "${line%%\|*}" ] && echo $line
2012-06-22 20:24:43 -04:00
done < "$datafile" | awk -v path="$*" -v now="$(date +%s)" -F"|" '
BEGIN {
rank[path] = 1
time[path] = now
}
2009-06-18 22:15:33 -07:00
$2 >= 1 {
if( $1 == path ) {
2009-06-22 21:48:30 -04:00
rank[$1] = $2 + 1
time[$1] = now
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 {
2013-01-18 19:44:28 -05:00
if( count > 6000 ) {
for( i in rank ) print i "|" 0.99*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
}
2012-06-22 10:22:22 -04:00
' 2>/dev/null >| "$tempfile"
2011-07-10 15:09:15 -04:00
if [ $? -ne 0 -a -f "$datafile" ]; then
env rm -f "$tempfile"
else
env mv -f "$tempfile" "$datafile"
fi
# tab completion
2009-06-18 22:15:33 -07:00
elif [ "$1" = "--complete" ]; then
2012-06-22 20:24:43 -04:00
while read line; do
2012-06-22 10:22:22 -04:00
[ -d "${line%%\|*}" ] && echo $line
2012-06-22 20:24:43 -04:00
done < "$datafile" | awk -v q="$2" -F"|" '
BEGIN {
if( q == tolower(q) ) nocase = 1
split(substr(q,3),fnd," ")
}
2009-06-18 22:15:33 -07:00
{
if( nocase ) {
for( i in fnd ) tolower($1) !~ tolower(fnd[i]) && $1 = ""
} else {
for( i in fnd ) $1 !~ fnd[i] && $1 = ""
}
if( $1 ) print $1
2009-06-18 22:15:33 -07:00
}
2012-06-22 10:22:22 -04:00
' 2>/dev/null
2009-06-18 22:15:33 -07:00
else
# list/go
2009-06-22 21:48:30 -04:00
while [ "$1" ]; do case "$1" in
-c) local fnd="$PWD $fnd";;
-h) echo "${_Z_CMD:-z} [-c][-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" -a "$fnd" != "$PWD " ] || local list=1
2009-06-18 22:15:33 -07:00
# if we hit enter on a completion just go there
case "$last" in
# completions will always start with /
/*) [ -z "$list" -a -d "$last" ] && cd "$last" && return;;
esac
# no file yet
2009-06-26 11:26:07 -04:00
[ -f "$datafile" ] || return
local cd
2012-06-22 20:24:43 -04:00
cd="$(while read line; do
2012-06-22 10:22:22 -04:00
[ -d "${line%%\|*}" ] && echo $line
2012-06-22 20:24:43 -04:00
done < "$datafile" | awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -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 ) {
cmd = "sort -n >&2"
2009-10-13 19:46:37 -04:00
for( i in files ) if( files[i] ) printf "%-10s %s\n", files[i], i | cmd
if( override ) printf "%-10s %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
}
}
function common(matches) {
# shortest match
2009-06-22 21:48:30 -04:00
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
2012-08-10 02:29:55 -04:00
# shortest match must be common to each match. escape special characters in
# a copy when testing, so we can return the original.
clean_short = short
gsub(/[\(\)\[\]\|]/, "\\\\&", clean_short)
for( i in matches ) if( matches[i] && i !~ clean_short ) return
return short
2009-06-18 22:15:33 -07:00
}
BEGIN { split(q, a, " "); oldf = noldf = -9999999999 }
2009-06-18 22:15:33 -07:00
{
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" ) {
f = $3-t
2009-06-18 22:15:33 -07:00
} else f = frecent($2, $3)
wcase[$1] = nocase[$1] = f
2009-06-20 12:23:56 -04:00
for( i in a ) {
if( $1 !~ a[i] ) delete wcase[$1]
2009-06-20 12:23:56 -04:00
if( tolower($1) !~ tolower(a[i]) ) delete nocase[$1]
}
if( wcase[$1] && wcase[$1] > oldf ) {
2009-06-18 22:15:33 -07:00
cx = $1
oldf = wcase[$1]
} else if( nocase[$1] && nocase[$1] > noldf ) {
2009-06-18 22:15:33 -07:00
ncx = $1
noldf = nocase[$1]
}
}
END {
if( cx ) {
output(wcase, cx, common(wcase))
} else if( ncx ) output(nocase, ncx, common(nocase))
2009-06-18 22:15:33 -07:00
}
2012-06-22 10:22:22 -04:00
')"
[ $? -gt 0 ] && return
[ "$cd" ] && cd "$cd"
2009-06-18 22:15:33 -07:00
fi
}
alias ${_Z_CMD:-z}='_z 2>&1'
[ "$_Z_NO_RESOLVE_SYMLINKS" ] || _Z_RESOLVE_SYMLINKS="-P"
if compctl &> /dev/null; then
[ "$_Z_NO_PROMPT_COMMAND" ] || {
# zsh populate directory list, avoid clobbering any other precmds
if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then
_z_precmd() {
_z --add "${PWD:a}"
}
else
_z_precmd() {
_z --add "${PWD:A}"
}
fi
precmd_functions+=(_z_precmd)
}
2011-08-03 19:26:06 -04:00
# zsh tab completion
_z_zsh_tab_completion() {
local compl
read -l compl
reply=(${(f)"$(_z --complete "$compl")"})
}
compctl -U -K _z_zsh_tab_completion _z
elif complete &> /dev/null; then
# bash tab completion
complete -o filenames -C '_z --complete "$COMP_LINE"' ${_Z_CMD:-z}
[ "$_Z_NO_PROMPT_COMMAND" ] || {
# bash populate directory list. avoid clobbering other PROMPT_COMMANDs.
echo $PROMPT_COMMAND | grep -q "_z --add" || {
PROMPT_COMMAND='_z --add "$(pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null;'"$PROMPT_COMMAND"
}
}
fi
2013-02-24 16:20:17 -05:00
# we can unset these as they are only used during setup
unset -v _Z_NO_PROMPT_COMMAND _Z_NO_RESOLVE_SYMLINKS _Z_RESOLVE_SYMLINKS