fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes Altmanningershare/functions: fix path to /bin/sh on android7ca78e717

master
1.4 KiB51 linesraw
1# localization: skip(private)
2function __fish_cached --description "Cache the command output for a given amount of time"
3
4    argparse --min-args 1 --max-args 1 --stop-nonopt 't/max-age=!_validate_int --min 0' 'k/cache-key=' -- $argv
5    or return
6
7    if set -q _flag_cache_key
8        set -f cache_key $_flag_cache_key
9    else
10        set -f cache_key (string trim "$argv" | string split ' ')[1]
11    end
12    if not string match -q --regex '^[\w+-]+$' -- "$cache_key"
13        return 1
14    end
15
16    if set -q _flag_max_age
17        set -f max_age $_flag_max_age
18    else
19        set -f max_age -1
20    end
21    set -l cache_dir (__fish_make_cache_dir)
22    or return
23    set -l cache_file (path normalize $cache_dir/$cache_key)
24    set -l cache_age (path mtime --relative $cache_file)
25
26    set -l populate_cache (__fish_posix_shell) -c "
27        {
28            $argv
29        } >$cache_file || rm $cache_file 2>/dev/null
30    "
31
32    if not test -f $cache_file
33        __fish_cache_put $cache_file
34        $populate_cache &
35
36        if test -n "$last_pid"
37            # wait for at most 1 second if supported
38            command --search waitpid &>/dev/null
39            and waitpid --exited --timeout 1 $last_pid
40            and test -f $cache_file
41            and cat $cache_file
42        end
43    else
44        cat $cache_file
45
46        if test $cache_age -gt $max_age
47            __fish_cache_put $cache_file
48            $populate_cache &
49        end
50    end
51end