z: upgrade plugin to the latest version (#9310)

Commit-id: 125f4dc47e15891739dd8262d5b23077fe8fb9ab
125f4dc47e
This commit is contained in:
Michele Bologna 2020-10-07 17:15:29 +02:00 committed by GitHub
parent 6ebf27b8d3
commit 01ce52e681
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 22 deletions

View file

@ -23,6 +23,8 @@ DESCRIPTION
OPTIONS OPTIONS
-c restrict matches to subdirectories of the current directory -c restrict matches to subdirectories of the current directory
-e echo the best match, don't cd
-h show a brief help message -h show a brief help message
-l list only -l list only
@ -57,6 +59,8 @@ NOTES
Optionally: Optionally:
Set $_Z_CMD to change the command name (default z). Set $_Z_CMD to change the command name (default z).
Set $_Z_DATA to change the datafile (default $HOME/.z). Set $_Z_DATA to change the datafile (default $HOME/.z).
Set $_Z_MAX_SCORE lower to age entries out faster (default
9000).
Set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution. Set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
Set $_Z_NO_PROMPT_COMMAND to handle PROMPT_COMMAND/precmd your- Set $_Z_NO_PROMPT_COMMAND to handle PROMPT_COMMAND/precmd your-
self. self.
@ -64,8 +68,8 @@ NOTES
Set $_Z_OWNER to allow usage when in 'sudo -s' mode. Set $_Z_OWNER to allow usage when in 'sudo -s' mode.
(These settings should go in .bashrc/.zshrc before the line (These settings should go in .bashrc/.zshrc before the line
added above.) added above.)
Install the provided man page z.1 somewhere like Install the provided man page z.1 somewhere in your MANPATH,
/usr/local/man/man1. like /usr/local/man/man1.
Aging: Aging:
The rank of directories maintained by z undergoes aging based on a sim- The rank of directories maintained by z undergoes aging based on a sim-

View file

@ -78,6 +78,9 @@ Set \fB$_Z_CMD\fR to change the command name (default \fBz\fR).
Set \fB$_Z_DATA\fR to change the datafile (default \fB$HOME/.z\fR). Set \fB$_Z_DATA\fR to change the datafile (default \fB$HOME/.z\fR).
.RE .RE
.RS .RS
Set \fB$_Z_MAX_SCORE\fR lower to age entries out faster (default \fB9000\fR).
.RE
.RS
Set \fB$_Z_NO_RESOLVE_SYMLINKS\fR to prevent symlink resolution. Set \fB$_Z_NO_RESOLVE_SYMLINKS\fR to prevent symlink resolution.
.RE .RE
.RS .RS

View file

@ -10,6 +10,7 @@
# * optionally: # * optionally:
# set $_Z_CMD in .bashrc/.zshrc to change the command (default z). # 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_DATA in .bashrc/.zshrc to change the datafile (default ~/.z).
# set $_Z_MAX_SCORE lower to age entries out faster (default 9000).
# set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution. # set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
# set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself. # set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself.
# set $_Z_EXCLUDE_DIRS to an array of directories to exclude. # set $_Z_EXCLUDE_DIRS to an array of directories to exclude.
@ -23,6 +24,8 @@
# * z -l foo # list matches instead of cd # * z -l foo # list matches instead of cd
# * z -e foo # echo the best match, don't cd # * z -e foo # echo the best match, don't cd
# * z -c foo # restrict matches to subdirs of $PWD # * z -c foo # restrict matches to subdirs of $PWD
# * z -x # remove the current directory from the datafile
# * z -h # show a brief help message
[ -d "${_Z_DATA:-$HOME/.z}" ] && { [ -d "${_Z_DATA:-$HOME/.z}" ] && {
echo "ERROR: z.sh's datafile (${_Z_DATA:-$HOME/.z}) is a directory." echo "ERROR: z.sh's datafile (${_Z_DATA:-$HOME/.z}) is a directory."
@ -62,7 +65,8 @@ _z() {
# maintain the data file # maintain the data file
local tempfile="$datafile.$RANDOM" local tempfile="$datafile.$RANDOM"
_z_dirs | awk -v path="$*" -v now="$(date +%s)" -F"|" ' local score=${_Z_MAX_SCORE:-9000}
_z_dirs | awk -v path="$*" -v now="$(date +%s)" -v score=$score -F"|" '
BEGIN { BEGIN {
rank[path] = 1 rank[path] = 1
time[path] = now time[path] = now
@ -79,7 +83,7 @@ _z() {
count += $2 count += $2
} }
END { END {
if( count > 9000 ) { if( count > score ) {
# aging # aging
for( x in rank ) print x "|" 0.99*rank[x] "|" time[x] for( x in rank ) print x "|" 0.99*rank[x] "|" time[x]
} else for( x in rank ) print x "|" rank[x] "|" time[x] } else for( x in rank ) print x "|" rank[x] "|" time[x]
@ -138,27 +142,24 @@ _z() {
local cd local cd
cd="$( < <( _z_dirs ) awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" ' cd="$( < <( _z_dirs ) awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" '
function frecent(rank, time) { function frecent(rank, time) {
# relate frequency and time # relate frequency and time
dx = t - time dx = t - time
if( dx < 3600 ) return rank * 4 return int(10000 * rank * (3.75/((0.0001 * dx + 1) + 0.25)))
if( dx < 86400 ) return rank * 2
if( dx < 604800 ) return rank / 2
return rank / 4
} }
function output(matches, best_match, common) { function output(matches, best_match, common) {
# list or return the desired directory # list or return the desired directory
if( list ) { if( list ) {
cmd = "sort -g >&2" if( common ) {
printf "%-10s %s\n", "common:", common > "/dev/stderr"
}
cmd = "sort -n >&2"
for( x in matches ) { for( x in matches ) {
if( matches[x] ) { if( matches[x] ) {
printf "%-10s %s\n", matches[x], x | cmd printf "%-10s %s\n", matches[x], x | cmd
} }
} }
if( common ) {
printf "%-10s %s\n", "common:", common > "/dev/stderr"
}
} else { } else {
if( common ) best_match = common if( common && !typ ) best_match = common
print best_match print best_match
} }
} }
@ -200,15 +201,22 @@ _z() {
# prefer case sensitive # prefer case sensitive
if( best_match ) { if( best_match ) {
output(matches, best_match, common(matches)) output(matches, best_match, common(matches))
exit
} else if( ibest_match ) { } else if( ibest_match ) {
output(imatches, ibest_match, common(imatches)) output(imatches, ibest_match, common(imatches))
exit
} }
exit(1)
} }
')" ')"
[ $? -eq 0 ] && [ "$cd" ] && { if [ "$?" -eq 0 ]; then
if [ "$echo" ]; then echo "$cd"; else builtin cd "$cd"; fi if [ "$cd" ]; then
} if [ "$echo" ]; then echo "$cd"; else builtin cd "$cd"; fi
fi
else
return $?
fi
fi fi
} }
@ -223,15 +231,11 @@ if type compctl >/dev/null 2>&1; then
if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then
_z_precmd() { _z_precmd() {
(_z --add "${PWD:a}" &) (_z --add "${PWD:a}" &)
# Reference $RANDOM to refresh its value inside the subshell
# Otherwise, multiple runs get the same value
: $RANDOM : $RANDOM
} }
else else
_z_precmd() { _z_precmd() {
(_z --add "${PWD:A}" &) (_z --add "${PWD:A}" &)
# Reference $RANDOM to refresh its value inside the subshell
# Otherwise, multiple runs get the same value
: $RANDOM : $RANDOM
} }
fi fi