Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
function _stackdoc_search
set pkg "$argv[1]"
set com "$argv[2]"
set docs "$argv[3]"
echo "Search $pkg in $docs"
set results (find "$docs" -maxdepth 1 -type d -name "*$pkg*")
switch (count $results)
case '0'
return 1
case '1'
eval "$com" "$results/index.html"
return 0
case '*'
printf '%s\n' $results | grep "$pkg"
return 0
end
end
function stackdoc
set default_com 'echo Found '
set pkg "$argv[1]"
set -q argv[2]; and set com "$argv[2]"; or set com "$default_com"
if [ -n "$pkg" ]
_stackdoc_search "$pkg" "$com" (stack path --local-doc-root ^/dev/null)
or _stackdoc_search "$pkg" "$com" (stack path --snapshot-doc-root ^/dev/null)
or _stackdoc_search "$pkg" "$com" (dirname (dirname (dirname (stack path --global-pkg-db ^/dev/null))))/share/doc/ghc/html/libraries
else
echo "Usage: stackdoc PACKAGE [COMMAND]"
echo
echo "Search haddock docs for the named haskell package and execute the given command on the first result."
echo
echo "These directories are searched in-order:"
echo " - stack's local doc root for the current project"
echo " - stack's user-level snapshot doc root"
echo " - ghc library docs for stack's global package database"
echo
echo "If not given, the default command is '$default_com'."
echo
echo "Can't find the docs you want? Maybe the haddocks have yet to be generated. Try 'stack haddock \$PACKAGE'."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment