summaryrefslogtreecommitdiff
path: root/functions/__z_add.fish
blob: 20d5d7e5714af7e7370a6e60162872d4841aaa6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function __z_add -d "Add PATH to .z file"
    test -n "$fish_private_mode"; and return 0

    for i in $Z_EXCLUDE
        if string match -r $i $PWD >/dev/null
            return 0 #Path excluded
        end
    end

    set -l tmpfile (mktemp $Z_DATA.XXXXXX)

    if test -f $tmpfile
        set -l path (string replace --all \\ \\\\ $PWD)
        command awk -v path=$path -v now=(date +%s) -F "|" '
      BEGIN {
          rank[path] = 1
          time[path] = now
      }
      $2 >= 1 {
          if( $1 == path ) {
              rank[$1] = $2 + 1
              time[$1] = now
          }
          else {
              rank[$1] = $2
              time[$1] = $3
          }
          count += $2
      }
      END {
          if( count > 1000 ) {
              for( i in rank ) print i "|" 0.9*rank[i] "|" time[i] # aging
          }
          else for( i in rank ) print i "|" rank[i] "|" time[i]
      }
    ' $Z_DATA 2>/dev/null >$tmpfile

        if test ! -z "$Z_OWNER"
            chown $Z_OWNER:(id -ng $Z_OWNER) $tmpfile
        end
        #
        # Don't use redirection here as it can lead to a race condition where $Z_DATA is clobbered.
        # Note: There is a still a possible race condition where an old version of $Z_DATA is
        #       read by one instance of Fish before another instance of Fish writes its copy.
        #
        command mv $tmpfile $Z_DATA
        or command rm $tmpfile
    end
end