__fish_apropos: refactor, limit to 10.15+, change default cache dir.

No longer uses the __fish_apropos hack on every version of macOS.
Juat Catalina+.

The whatis database generated and replaced daily is 2 megabytes on
my computer, and in ~/.cache on a home dir might wind up on a net
mount or something annoying. or, definitely it's backed up by default.

It's wiser to throw that junk in with other cache files on the system
aka DARWIN_USER_CACHE_DIR, and only use the XDG directory if
someone specifically configured that.

Mainly, this just means at least it won't automatically get backed
up by Time Machine and stuff every day, which is no big deal but
y'know...

Rearranged stuff a little to not shell out every time.
This commit is contained in:
Aaron Gyes 2021-04-01 16:48:32 -07:00
parent ed5e5a9d49
commit 85e3381480

View file

@ -4,39 +4,49 @@ if not type -q apropos
exit
end
function __fish_apropos
# macOS 10.15 "Catalina" has some major issues.
# The whatis database is non-existent, so apropos tries (and fails) to create it every time,
# which takes about half a second.
#
# Instead, we build a whatis database in the user cache directory
# and override the MANPATH using that directory before we run `apropos`
#
# the cache is rebuilt once a day
if test (uname) = Darwin
set -l cache $HOME/.cache/fish/
if test -n "$XDG_CACHE_HOME"
set cache $XDG_CACHE_HOME/fish
end
# Check for macOS Catalina or above. This is Darwin 19.x or above. See unames reported here:
# https://en.wikipedia.org/wiki/Darwin_(operating_system)
set -l sysver (uname -sr | string match -r "(Darwin) (\d\d)"\.)
set -l db $cache/whatis
test $status -eq 0 -a (count $sysver) -eq 3
and if test $sysver[2] = 'Darwin' -a $sysver[3] -ge 19
if test -n "$XDG_CACHE_HOME"
set dir $XDG_CACHE_HOME/fish
else
set dir (getconf DARWIN_USER_CACHE_DIR)"fish"
end
function __fish_apropos -V dir
# macOS 10.15 "Catalina" has a read only filesystem where the whatis database should be.
# The whatis database is non-existent, so apropos tries (and fails) to create it every time,
# which can take seconds.
#
# Instead, we build a whatis database in the user cache directory
# and override the MANPATH using that directory before we run `apropos`
#
# the cache is rebuilt once a week.
set -l whatis $cache/whatis
set -l max_age 86400 # one day
set -l age $max_age
if test -f "$db"
if test -f "$whatis"
# Some people use GNU tools on macOS, and GNU stat works differently.
# However it's currently guaranteed that the macOS stat is in /usr/bin,
# so we use that explicitly.
set age (math (date +%s) - (/usr/bin/stat -f %m $db))
set age (math (date +%s) - (/usr/bin/stat -f %m $whatis))
end
MANPATH="$cache" apropos -- $argv
MANPATH="$dir" apropos $argv
if test $age -ge $max_age
mkdir -m 700 -p $cache
/usr/libexec/makewhatis -o $db (man --path | string split :) >/dev/null 2>&1 </dev/null &
test -d "$dir" || mkdir -m 700 -p $dir
/usr/libexec/makewhatis -o "$whatis" (man --path | string split :) >/dev/null 2>&1 </dev/null &
end
else
apropos -- $argv
end
else
function __fish_apropos
apropos $argv
end
end