01ce52e6814c27a61945ed4406059c14f0c5d548
- Author
- Michele Bologna <michele.bologna@gmail.com>
- Committer
- GitHub <noreply@github.com>
- Date
Message
Diff
This diff is truncated to protect this page.
1diff --git a/plugins/z/README b/plugins/z/README
2index 56261cff4f613d49f0e3407c34e8e222b3f682d0..47e54c57a43e74218f3e19ff72cbb2f59f5b63a8 100644
3--- a/plugins/z/README
4+++ b/plugins/z/README
5@@ -23,6 +23,8 @@ DESCRIPTION
6 OPTIONS
7 -c restrict matches to subdirectories of the current directory
8
9+ -e echo the best match, don't cd
10+
11 -h show a brief help message
12
13 -l list only
14@@ -57,6 +59,8 @@ NOTES
15 Optionally:
16 Set $_Z_CMD to change the command name (default z).
17 Set $_Z_DATA to change the datafile (default $HOME/.z).
18+ Set $_Z_MAX_SCORE lower to age entries out faster (default
19+ 9000).
20 Set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
21 Set $_Z_NO_PROMPT_COMMAND to handle PROMPT_COMMAND/precmd your-
22 self.
23@@ -64,8 +68,8 @@ NOTES
24 Set $_Z_OWNER to allow usage when in 'sudo -s' mode.
25 (These settings should go in .bashrc/.zshrc before the line
26 added above.)
27- Install the provided man page z.1 somewhere like
28- /usr/local/man/man1.
29+ Install the provided man page z.1 somewhere in your MANPATH,
30+ like /usr/local/man/man1.
31
32 Aging:
33 The rank of directories maintained by z undergoes aging based on a sim-
34diff --git a/plugins/z/z.1 b/plugins/z/z.1
35index d4cac1ac27c31732b5ac669a7544222f2a754f80..182f981764647c19c80360d8356b4203097def67 100644
36--- a/plugins/z/z.1
37+++ b/plugins/z/z.1
38@@ -78,6 +78,9 @@ Set \fB$_Z_CMD\fR to change the command name (default \fBz\fR).
39 Set \fB$_Z_DATA\fR to change the datafile (default \fB$HOME/.z\fR).
40 .RE
41 .RS
42+Set \fB$_Z_MAX_SCORE\fR lower to age entries out faster (default \fB9000\fR).
43+.RE
44+.RS
45 Set \fB$_Z_NO_RESOLVE_SYMLINKS\fR to prevent symlink resolution.
46 .RE
47 .RS
48diff --git a/plugins/z/z.sh b/plugins/z/z.sh
49index 5c5771d629d18af00487c53411592139494b70cf..13008a60ebe8c00d0e7f92ec6f153244ffc94905 100644
50--- a/plugins/z/z.sh
51+++ b/plugins/z/z.sh
52@@ -10,6 +10,7 @@
53 # * optionally:
54 # set $_Z_CMD in .bashrc/.zshrc to change the command (default z).
55 # set $_Z_DATA in .bashrc/.zshrc to change the datafile (default ~/.z).
56+# set $_Z_MAX_SCORE lower to age entries out faster (default 9000).
57 # set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
58 # set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself.
59 # set $_Z_EXCLUDE_DIRS to an array of directories to exclude.
60@@ -23,6 +24,8 @@
61 # * z -l foo # list matches instead of cd
62 # * z -e foo # echo the best match, don't cd
63 # * z -c foo # restrict matches to subdirs of $PWD
64+# * z -x # remove the current directory from the datafile
65+# * z -h # show a brief help message
66
67 [ -d "${_Z_DATA:-$HOME/.z}" ] && {
68 echo "ERROR: z.sh's datafile (${_Z_DATA:-$HOME/.z}) is a directory."
69@@ -62,7 +65,8 @@ _z() {
70
71 # maintain the data file
72 local tempfile="$datafile.$RANDOM"
73- _z_dirs | awk -v path="$*" -v now="$(date +%s)" -F"|" '
74+ local score=${_Z_MAX_SCORE:-9000}
75+ _z_dirs | awk -v path="$*" -v now="$(date +%s)" -v score=$score -F"|" '
76 BEGIN {
77 rank[path] = 1
78 time[path] = now
79@@ -79,7 +83,7 @@ _z() {
80 count += $2
81 }
82 END {
83- if( count > 9000 ) {
84+ if( count > score ) {
85 # aging
86 for( x in rank ) print x "|" 0.99*rank[x] "|" time[x]
87 } else for( x in rank ) print x "|" rank[x] "|" time[x]
88@@ -138,27 +142,24 @@ _z() {
89 local cd
90 cd="$( < <( _z_dirs ) awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" '
91 function frecent(rank, time) {
92- # relate frequency and time
93- dx = t - time
94- if( dx < 3600 ) return rank * 4
95- if( dx < 86400 ) return rank * 2
96- if( dx < 604800 ) return rank / 2
97- return rank / 4
98+ # relate frequency and time
99+ dx = t - time
100+ return int(10000 * rank * (3.75/((0.0001 * dx + 1) + 0.25)))
101 }
102 function output(matches, best_match, common) {
103 # list or return the desired directory
104 if( list ) {
105- cmd = "sort -g >&2"
106+ if( common ) {
107+ printf "%-10s %s\n", "common:", common > "/dev/stderr"
108+ }
109+ cmd = "sort -n >&2"
110 for( x in matches ) {
111 if( matches[x] ) {
112 printf "%-10s %s\n", matches[x], x | cmd
113 }
114 }
115- if( common ) {
116- printf "%-10s %s\n", "common:", common > "/dev/stderr"
117- }
118 } else {
119- if( common ) best_match = common
120+ if( common && !typ ) best_match = common
121 print best_match
122 }
123 }
124@@ -200,15 +201,22 @@ _z() {
125 # prefer case sensitive
126 if( best_match ) {
127 output(matches, best_match, common(matches))
128+ exit
129 } else if( ibest_match ) {
130 output(imatches, ibest_match, common(imatches))
131+ exit
132 }
133+ exit(1)
134 }
135 ')"
136
137- [ $? -eq 0 ] && [ "$cd" ] && {
138- if [ "$echo" ]; then echo "$cd"; else builtin cd "$cd"; fi
139- }
140+ if [ "$?" -eq 0 ]; then
141+ if [ "$cd" ]; then
142+ if [ "$echo" ]; then echo "$cd"; else builtin cd "$cd"; fi
143+ fi
144+ else
145+ return $?
146+ fi
147 fi
148 }
149
150@@ -223,15 +231,11 @@ if type compctl >/dev/null 2>&1; then
151 if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then