PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/z.sh

http://github.com/dakrone/dakrone-dotfiles
Shell | 240 lines | 191 code | 12 blank | 37 comment | 27 complexity | e0f20e4ac6376cc71f94941ed2e50022 MD5 | raw file
Possible License(s): CC-BY-SA-4.0, GPL-3.0
  1. # Copyright (c) 2009 rupa deadwyler under the WTFPL license
  2. # maintains a jump-list of the directories you actually use
  3. #
  4. # INSTALL:
  5. # * put something like this in your .bashrc/.zshrc:
  6. # . /path/to/z.sh
  7. # * cd around for a while to build up the db
  8. # * PROFIT!!
  9. # * optionally:
  10. # set $_Z_CMD in .bashrc/.zshrc to change the command (default z).
  11. # set $_Z_DATA in .bashrc/.zshrc to change the datafile (default ~/.z).
  12. # set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
  13. # set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself.
  14. # set $_Z_EXCLUDE_DIRS to an array of directories to exclude.
  15. #
  16. # USE:
  17. # * z foo # cd to most frecent dir matching foo
  18. # * z foo bar # cd to most frecent dir matching foo and bar
  19. # * z -r foo # cd to highest ranked dir matching foo
  20. # * z -t foo # cd to most recently accessed dir matching foo
  21. # * z -l foo # list matches instead of cd
  22. # * z -c foo # restrict matches to subdirs of $PWD
  23. [ -d "${_Z_DATA:-$HOME/.z}" ] && {
  24. echo "ERROR: z.sh's datafile (${_Z_DATA:-$HOME/.z}) is a directory."
  25. }
  26. _z() {
  27. local datafile="${_Z_DATA:-$HOME/.z}"
  28. # bail if we don't own ~/.z (we're another user but our ENV is still set)
  29. [ -f "$datafile" -a ! -O "$datafile" ] && return
  30. # add entries
  31. if [ "$1" = "--add" ]; then
  32. shift
  33. # $HOME isn't worth matching
  34. [ "$*" = "$HOME" ] && return
  35. # don't track excluded dirs
  36. local exclude
  37. for exclude in "${_Z_EXCLUDE_DIRS[@]}"; do
  38. [ "$*" = "$exclude" ] && return
  39. done
  40. # maintain the data file
  41. local tempfile="$datafile.$RANDOM"
  42. while read line; do
  43. # only count directories
  44. [ -d "${line%%\|*}" ] && echo $line
  45. done < "$datafile" | awk -v path="$*" -v now="$(date +%s)" -F"|" '
  46. BEGIN {
  47. rank[path] = 1
  48. time[path] = now
  49. }
  50. $2 >= 1 {
  51. # drop ranks below 1
  52. if( $1 == path ) {
  53. rank[$1] = $2 + 1
  54. time[$1] = now
  55. } else {
  56. rank[$1] = $2
  57. time[$1] = $3
  58. }
  59. count += $2
  60. }
  61. END {
  62. if( count > 6000 ) {
  63. # aging
  64. for( x in rank ) print x "|" 0.99*rank[x] "|" time[x]
  65. } else for( x in rank ) print x "|" rank[x] "|" time[x]
  66. }
  67. ' 2>/dev/null >| "$tempfile"
  68. # do our best to avoid clobbering the datafile in a race condition
  69. if [ $? -ne 0 -a -f "$datafile" ]; then
  70. env rm -f "$tempfile"
  71. else
  72. env mv -f "$tempfile" "$datafile" || env rm -f "$tmpfile"
  73. fi
  74. # tab completion
  75. elif [ "$1" = "--complete" ]; then
  76. while read line; do
  77. [ -d "${line%%\|*}" ] && echo $line
  78. done < "$datafile" | awk -v q="$2" -F"|" '
  79. BEGIN {
  80. if( q == tolower(q) ) imatch = 1
  81. split(substr(q, 3), fnd, " ")
  82. }
  83. {
  84. if( imatch ) {
  85. for( x in fnd ) tolower($1) !~ tolower(fnd[x]) && $1 = ""
  86. } else {
  87. for( x in fnd ) $1 !~ fnd[x] && $1 = ""
  88. }
  89. if( $1 ) print $1
  90. }
  91. ' 2>/dev/null
  92. else
  93. # list/go
  94. while [ "$1" ]; do case "$1" in
  95. --) while [ "$1" ]; do shift; local fnd="$fnd $1";done;;
  96. -*) local opt=${1:1}; while [ "$opt" ]; do case ${opt:0:1} in
  97. c) local fnd="^$PWD $fnd";;
  98. h) echo "${_Z_CMD:-z} [-chlrtx] args" >&2; return;;
  99. x) sed -i "\:^${PWD}|.*:d" "$datafile";;
  100. l) local list=1;;
  101. r) local typ="rank";;
  102. t) local typ="recent";;
  103. esac; opt=${opt:1}; done;;
  104. *) local fnd="$fnd $1";;
  105. esac; local last=$1; shift; done
  106. [ "$fnd" -a "$fnd" != "^$PWD " ] || local list=1
  107. # if we hit enter on a completion just go there
  108. case "$last" in
  109. # completions will always start with /
  110. /*) [ -z "$list" -a -d "$last" ] && cd "$last" && return;;
  111. esac
  112. # no file yet
  113. [ -f "$datafile" ] || return
  114. local cd
  115. cd="$(while read line; do
  116. [ -d "${line%%\|*}" ] && echo $line
  117. done < "$datafile" | awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" '
  118. function frecent(rank, time) {
  119. # relate frequency and time
  120. dx = t - time
  121. if( dx < 3600 ) return rank * 4
  122. if( dx < 86400 ) return rank * 2
  123. if( dx < 604800 ) return rank / 2
  124. return rank / 4
  125. }
  126. function output(files, out, common) {
  127. # list or return the desired directory
  128. if( list ) {
  129. cmd = "sort -n >&2"
  130. for( x in files ) {
  131. if( files[x] ) printf "%-10s %s\n", files[x], x | cmd
  132. }
  133. if( common ) {
  134. printf "%-10s %s\n", "common:", common > "/dev/stderr"
  135. }
  136. } else {
  137. if( common ) out = common
  138. print out
  139. }
  140. }
  141. function common(matches) {
  142. # find the common root of a list of matches, if it exists
  143. for( x in matches ) {
  144. if( matches[x] && (!short || length(x) < length(short)) ) {
  145. short = x
  146. }
  147. }
  148. if( short == "/" ) return
  149. # use a copy to escape special characters, as we want to return
  150. # the original. yeah, this escaping is awful.
  151. clean_short = short
  152. gsub(/[\(\)\[\]\|]/, "\\\\&", clean_short)
  153. for( x in matches ) if( matches[x] && x !~ clean_short ) return
  154. return short
  155. }
  156. BEGIN { split(q, words, " "); hi_rank = ihi_rank = -9999999999 }
  157. {
  158. if( typ == "rank" ) {
  159. rank = $2
  160. } else if( typ == "recent" ) {
  161. rank = $3 - t
  162. } else rank = frecent($2, $3)
  163. matches[$1] = imatches[$1] = rank
  164. for( x in words ) {
  165. if( $1 !~ words[x] ) delete matches[$1]
  166. if( tolower($1) !~ tolower(words[x]) ) delete imatches[$1]
  167. }
  168. if( matches[$1] && matches[$1] > hi_rank ) {
  169. best_match = $1
  170. hi_rank = matches[$1]
  171. } else if( imatches[$1] && imatches[$1] > ihi_rank ) {
  172. ibest_match = $1
  173. ihi_rank = imatches[$1]
  174. }
  175. }
  176. END {
  177. # prefer case sensitive
  178. if( best_match ) {
  179. output(matches, best_match, common(matches))
  180. } else if( ibest_match ) {
  181. output(imatches, ibest_match, common(imatches))
  182. }
  183. }
  184. ')"
  185. [ $? -gt 0 ] && return
  186. [ "$cd" ] && cd "$cd"
  187. fi
  188. }
  189. alias ${_Z_CMD:-z}='_z 2>&1'
  190. [ "$_Z_NO_RESOLVE_SYMLINKS" ] || _Z_RESOLVE_SYMLINKS="-P"
  191. if compctl >/dev/null 2>&1; then
  192. # zsh
  193. [ "$_Z_NO_PROMPT_COMMAND" ] || {
  194. # populate directory list, avoid clobbering any other precmds.
  195. if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then
  196. _z_precmd() {
  197. _z --add "${PWD:a}"
  198. }
  199. else
  200. _z_precmd() {
  201. _z --add "${PWD:A}"
  202. }
  203. fi
  204. precmd_functions+=(_z_precmd)
  205. }
  206. _z_zsh_tab_completion() {
  207. # tab completion
  208. local compl
  209. read -l compl
  210. reply=(${(f)"$(_z --complete "$compl")"})
  211. }
  212. compctl -U -K _z_zsh_tab_completion _z
  213. elif complete >/dev/null 2>&1; then
  214. # bash
  215. # tab completion
  216. complete -o filenames -C '_z --complete "$COMP_LINE"' ${_Z_CMD:-z}
  217. [ "$_Z_NO_PROMPT_COMMAND" ] || {
  218. # populate directory list. avoid clobbering other PROMPT_COMMANDs.
  219. grep -q "_z --add" <<< "$PROMPT_COMMAND" || {
  220. PROMPT_COMMAND="$PROMPT_COMMAND"$'\n''_z --add "$(pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null;'
  221. }
  222. }
  223. fi