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