packageOpenDoc :: PackageAction
packageOpenDoc = do
    package <- ask
    let dir = ipdPackageDir package
    useStack <- liftIO . doesFileExist $ dir </> "stack.yaml"
    distDir <- if useStack
                        then do
                            --ask stack where its dist directory is
                            mvar <- liftIO newEmptyMVar
                            runExternalTool' "" "stack" ["path"] dir $ do
                                output <- CL.consume
                                liftIO . putMVar mvar $ head $ catMaybes $ map getDistOutput output
                            liftIO $ takeMVar mvar
                        else return "dist"
    liftIDE $ do
        prefs   <- readIDE prefs
        let path = dir </> distDir
                        </> "doc/html"
                        </> display (pkgName (ipdPackageId package))
                        </> "index.html"
        loadDoc . T.pack $ "file://" ++ path
        getDocumentation Nothing  >>= \ p -> displayPane p False
      `catchIDE`
        (\(e :: SomeException) -> print e)
  where
    -- get dist directory from stack path output
    getDistOutput (ToolOutput o) | Just t<-T.stripPrefix "dist-dir:" o = Just $ dropWhile isSpace $ T.unpack t
    getDistOutput _ = Nothing
packageOpenDoc :: PackageAction
packageOpenDoc = do
    package <- ask
    let dir = ipdPackageDir package
    useStack <- liftIO . doesFileExist $ dir </> "stack.yaml"
    distDir <- if useStack
                        then do
                            --ask stack where its dist directory is
                            mvar <- liftIO newEmptyMVar
                            runExternalTool' "" "stack" ["path"] dir $ do
                                output <- CL.consume
                                liftIO . putMVar mvar $ head $ catMaybes $ map getDistOutput output
                            liftIO $ takeMVar mvar
                        else return "dist"
    liftIDE $ do
        prefs   <- readIDE prefs
        let path = dir </> distDir
                        </> "doc/html"
                        </> display (pkgName (ipdPackageId package))
                        </> "index.html"
        loadDoc . T.pack $ "file://" ++ path
        getDocumentation Nothing  >>= \ p -> displayPane p False
      `catchIDE`
        (\(e :: SomeException) -> print e)
  where
    -- get dist directory from stack path output
    getDistOutput (ToolOutput o) | Just t<-T.stripPrefix "dist-dir:" o = Just $ dropWhile isSpace $ T.unpack t
    getDistOutput _ = Nothing
packageOpenDoc = do
    package <- ask
    let dir = ipdPackageDir package
    useStack <- liftIO . doesFileExist $ dir </> "stack.yaml"
    distDir <- if useStack
                        then do
                            --ask stack where its dist directory is
                            mvar <- liftIO newEmptyMVar
                            runExternalTool' "" "stack" ["path"] dir $ do
                                output <- CL.consume
                                liftIO . putMVar mvar $ head $ catMaybes $ map getDistOutput output
                            liftIO $ takeMVar mvar
                        else return "dist"
    liftIDE $ do
        prefs   <- readIDE prefs
        let path = dir </> distDir
                        </> "doc/html"
                        </> display (pkgName (ipdPackageId package))
                        </> "index.html"
        loadDoc . T.pack $ "file://" ++ path
        getDocumentation Nothing  >>= \ p -> displayPane p False
      `catchIDE`
        (\(e :: SomeException) -> print e)
  where
    -- get dist directory from stack path output
    getDistOutput (ToolOutput o) | Just t<-T.stripPrefix "dist-dir:" o = Just $ dropWhile isSpace $ T.unpack t
    getDistOutput _ = Nothing