Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
function _stackdoc_search () {
pkg="$1"
com="$2"
docs="$3"
echo "Search $pkg in $docs"
IFS=$'\n'
results=( $(find "$docs" -maxdepth 1 -type d -name "*$pkg*") )
unset IFS
case "${#results[@]}" in
'0')
return 1
;;
'1')
eval "$com" "$results/index.html"
return 0
;;
*)
printf '%s\n' "${results[@]}" | grep "$pkg"
return 0
;;
esac
}
function stackdoc () {
default_com='echo Found '
pkg="$1"
com="${2-$default_com}"
if [ -n "$pkg" ]; then
_stackdoc_search "$pkg" "$com" $(stack path --local-doc-root 2>/dev/null) \
|| _stackdoc_search "$pkg" "$com" $(stack path --snapshot-doc-root 2>/dev/null) \
|| _stackdoc_search "$pkg" "$com" $(dirname $(dirname $(dirname $(stack path --global-pkg-db 2>/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'."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment