__z_add.fish 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. function __z_add -d "Add PATH to .z file"
  2. for i in $Z_EXCLUDE
  3. if contains -- $PWD $i
  4. return 0 #Path excluded
  5. end
  6. end
  7. set -l tmpfile (mktemp $Z_DATA.XXXXXX)
  8. if test -f $tmpfile
  9. command awk -v path="$PWD" -v now=(date +%s) -F "|" '
  10. BEGIN {
  11. rank[path] = 1
  12. time[path] = now
  13. }
  14. $2 >= 1 {
  15. if( $1 == path ) {
  16. rank[$1] = $2 + 1
  17. time[$1] = now
  18. }
  19. else {
  20. rank[$1] = $2
  21. time[$1] = $3
  22. }
  23. count += $2
  24. }
  25. END {
  26. if( count > 1000 ) {
  27. for( i in rank ) print i "|" 0.9*rank[i] "|" time[i] # aging
  28. }
  29. else for( i in rank ) print i "|" rank[i] "|" time[i]
  30. }
  31. ' $Z_DATA 2>/dev/null >$tmpfile
  32. if test ! -z "$Z_OWNER"
  33. chown $Z_OWNER:(id -ng $Z_OWNER) $tmpfile
  34. end
  35. #
  36. # Don't use redirection here as it can lead to a race condition where $Z_DATA is clobbered.
  37. # Note: There is a still a possible race condition where an old version of $Z_DATA is
  38. # read by one instance of Fish before another instance of Fish writes its copy.
  39. #
  40. command mv $tmpfile $Z_DATA
  41. or command rm $tmpfile
  42. end
  43. __z_complete
  44. end