Skip to content

Commit

Permalink
replace notdir hack
Browse files Browse the repository at this point in the history
* Test entries in datafile via shell and pipe them into awk, rather than
  doing the test in awk.
  • Loading branch information
rupa committed Jun 22, 2012
1 parent 771c584 commit 5276d4d
Showing 1 changed file with 14 additions and 42 deletions.
56 changes: 14 additions & 42 deletions z.sh
Expand Up @@ -40,24 +40,14 @@ _z() {
# maintain the file
local tempfile
tempfile="$(mktemp $datafile.XXXXXX)" || return
awk -v path="$*" -v now="$(date +%s)" -v datafile="$datafile" -F"|" '
function notdir(path, tmp) {
# faster than system()
n = gsub("/+", "/", path)
for( i = 0; i < n; i++ ) path = path "/.."
path = path datafile
if( ( getline tmp < path ) >= 0 ) {
close(path)
return 0
}
return 1
}
cat "$datafile" | while read line; do
[ -d "${line%%\|*}" ] && echo $line
done | awk -v path="$*" -v now="$(date +%s)" -F"|" '
BEGIN {
rank[path] = 1
time[path] = now
}
$2 >= 1 {
if( notdir($1) ) next
if( $1 == path ) {
rank[$1] = $2 + 1
time[$1] = now
Expand All @@ -72,7 +62,7 @@ _z() {
for( i in rank ) print i "|" 0.9*rank[i] "|" time[i] # aging
} else for( i in rank ) print i "|" rank[i] "|" time[i]
}
' "$datafile" 2>/dev/null >| "$tempfile"
' 2>/dev/null >| "$tempfile"
if [ $? -ne 0 -a -f "$datafile" ]; then
env rm -f "$tempfile"
else
Expand All @@ -81,32 +71,22 @@ _z() {

# tab completion
elif [ "$1" = "--complete" ]; then
awk -v q="$2" -v datafile="$datafile" -F"|" '
function notdir(path, tmp) {
# faster than system()
n = gsub("/+", "/", path)
for( i = 0; i < n; i++ ) path = path "/.."
path = path datafile
if( ( getline tmp < path ) >= 0 ) {
close(path)
return 0
}
return 1
}
cat "$datafile" | while read line; do
[ -d "${line%%\|*}" ] && echo $line
done | awk -v q="$2" -F"|" '
BEGIN {
if( q == tolower(q) ) nocase = 1
split(substr(q,3),fnd," ")
}
{
if( notdir($1) ) next
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
}
' "$datafile" 2>/dev/null
' 2>/dev/null

else
# list/go
Expand All @@ -130,17 +110,9 @@ _z() {
[ -f "$datafile" ] || return

local cd
cd="$(awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -v datafile="$datafile" -F"|" '
function notdir(path, tmp) {
n = gsub("/+", "/", path)
for( i = 0; i < n; i++ ) path = path "/.."
path = path datafile
if( ( getline tmp < path ) >= 0 ) {
close(path)
return 0
}
return 1
}
cd="$(cat "$datafile" | while read line; do
[ -d "${line%%\|*}" ] && echo $line
done | awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" '
function frecent(rank, time) {
dx = t-time
if( dx < 3600 ) return rank*4
Expand Down Expand Up @@ -176,7 +148,6 @@ _z() {
}
BEGIN { split(q, a, " ") }
{
if( notdir($1) ) next
if( typ == "rank" ) {
f = $2
} else if( typ == "recent" ) {
Expand All @@ -200,7 +171,7 @@ _z() {
output(wcase, cx, common(wcase))
} else if( ncx ) output(nocase, ncx, common(nocase))
}
' "$datafile")"
')"
[ $? -gt 0 ] && return
[ "$cd" ] && cd "$cd"
fi
Expand All @@ -213,7 +184,8 @@ if complete &> /dev/null; then
complete -C '_z --complete "$COMP_LINE"' ${_Z_CMD:-z}
# populate directory list. avoid clobbering other PROMPT_COMMANDs.
echo $PROMPT_COMMAND | grep -q "_z --add"
[ $? -gt 0 ] && PROMPT_COMMAND='_z --add "$(pwd -P 2>/dev/null)" 2>/dev/null;'"$PROMPT_COMMAND"
#[ $? -gt 0 ] && PROMPT_COMMAND='_z --add "$(pwd -P 2>/dev/null)" 2>/dev/null;'"$PROMPT_COMMAND"
[ $? -gt 0 ] && PROMPT_COMMAND='_z --add "$(pwd 2>/dev/null)" 2>/dev/null;'"$PROMPT_COMMAND"
elif compctl &> /dev/null; then
# zsh tab completion
_z_zsh_tab_completion() {
Expand Down

0 comments on commit 5276d4d

Please sign in to comment.