packages feed

hwm-0.1.2 / src / HWM / Runtime / UI.hs

{-# LANGUAGE DerivingStrategies #-}{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE GeneralizedNewtypeDeriving #-}{-# LANGUAGE MultiParamTypeClasses #-}{-# LANGUAGE NamedFieldPuns #-}{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE TupleSections #-}{-# LANGUAGE UndecidableInstances #-}{-# LANGUAGE NoImplicitPrelude #-}module HWM.Runtime.UI  ( MonadUI (..),    UIT,    runUIT,    runUI,    putLine,    indent,    section,    sectionWorkspace,    sectionEnvironments,    sectionConfig,    sectionTableM,    forTable_,    printSummary,    statusIndicator,    runSpinner,    printGenTable,    forTable,  )whereimport Control.Concurrent (threadDelay)import Control.Monad.Error.Class (MonadError)import Control.Monad.Except (MonadError (..))import Data.List (groupBy, maximum, (!!))import qualified Data.Map.Strict as Mapimport qualified Data.Text as Timport HWM.Core.Common (Name)import HWM.Core.Formatting (Color (..), Format (..), Status (..), chalk, indentBlockNum, padDots, renderSummaryStatus, subPathSign)import HWM.Core.Result  ( Issue (..),    IssueDetails (..),    ResultT (..),    Severity (..),  )import Reludeimport System.Console.ANSI (clearLine, setCursorColumn)data UIContext m = UIContext  { uiWriter :: Text -> m (),    uiIndent :: Int  }newtype UIT m a = UIT {_unUIT :: ReaderT (UIContext m) m a}  deriving newtype (Functor, Applicative, Monad, MonadIO)instance MonadTrans UIT where  lift = UIT . liftinstance (MonadError err m) => MonadError err (UIT m) where  throwError = UIT . lift . throwError  catchError (UIT action) handler = UIT $ ReaderT $ \ctx ->    catchError (runReaderT action ctx) (\e -> runReaderT (_unUIT (handler e)) ctx)runUIT :: (Monad m) => (Text -> m ()) -> UIT m a -> m arunUIT writer (UIT action) =  let ctx = UIContext {uiWriter = writer, uiIndent = 0}   in runReaderT action ctxrunUI :: UIT IO a -> IO arunUI = runUIT (putStr . toString)class (Monad m) => MonadUI m where  uiWrite :: Text -> m ()  uiIndentLevel :: m Int  uiWithIndent :: (Int -> Int) -> m a -> m ainstance (Monad m) => MonadUI (UIT m) where  uiWrite txt = UIT $ do    UIContext {uiWriter} <- ask    lift (uiWriter txt)  uiIndentLevel = UIT $ asks uiIndent  uiWithIndent f (UIT action) = UIT $ local adjust action    where      adjust ctx = ctx {uiIndent = f (uiIndent ctx)}instance (MonadUI m) => MonadUI (ResultT m) where  uiWrite txt = lift (uiWrite txt)  uiIndentLevel = lift uiIndentLevel  uiWithIndent f (ResultT action) = ResultT $ uiWithIndent f actionputLine :: (MonadUI m) => Text -> m ()putLine txt = do  level <- uiIndentLevel  uiWrite (indentBlockNum level (txt <> "\n"))indent :: (MonadUI m) => Int -> m a -> m aindent amount = uiWithIndent (+ amount)sectionBase :: (MonadUI m) => Text -> Text -> m a -> m asectionBase emoji title action = do  putLine ""  putLine (emoji <> " " <> chalk Bold title)  indent 1 actionsection :: (MonadUI m) => Text -> m a -> m ()section t m = sectionBase "•" t m $> ()sectionWorkspace :: (MonadUI m) => m a -> m ()sectionWorkspace m = sectionBase "./" "workspace" m $> ()sectionEnvironments :: (MonadUI m) => Maybe Text -> m a -> m ()sectionEnvironments title = section ("environments" <> maybe "" (\name -> chalk Dim " (default: " <> chalk Magenta name <> chalk Dim ")") title)minRawSize :: IntminRawSize = 16forHLTable :: (MonadUI m) => [a] -> (a -> (Name, m (Name, b))) -> m [(Name, b)]forHLTable as f = traverse formatRow rows  where    rows = map f as    maxLabelLen = maximum (minRawSize : map (T.length . fst) rows) + 2    formatRow (label, valueM) = do      (value, mvalue) <- valueM      putLine $ padDots maxLabelLen label <> value      pure (label, mvalue)forTable :: (MonadUI m) => Name -> [a] -> (a -> (Name, m (Name, b))) -> m [(Name, b)]forTable title as f = sectionBase "•" title $ forHLTable as fforTable_ :: (MonadUI m) => [a] -> (a -> (Text, m Text)) -> m ()forTable_ rows f = tableM_ (map f rows)tableM_ :: (MonadUI m) => [(Text, m Text)] -> m ()tableM_ rows = forHLTable rows (second ((,()) <$>)) $> ()sectionTableM :: (MonadUI m) => Text -> [(Text, m Text)] -> m ()sectionTableM title = section title . tableM_sectionConfig :: (MonadUI m) => [(Text, m Text)] -> m ()sectionConfig = section "config" . tableM_printGenTable :: (MonadUI m) => [[Text]] -> m ()printGenTable rows =  let n = if null rows then 0 else maximum (map length rows)      padRow r = take n (r ++ repeat "")      paddedRows = map padRow rows      colWidths = [maximum (0 : map (T.length . (!! i)) paddedRows) | i <- [0 .. n - 1]]      formatRow row =        let padded = padRow row            cells = zipWith (`T.justifyLeft` ' ') colWidths padded         in putLine $ T.intercalate "  " cells   in traverse_ formatRow rowsisError :: Issue -> BoolisError i = issueSeverity i == SeverityErrorrenderSummaryLines :: [Issue] -> [Text]renderSummaryLines [] = ["", renderSummaryStatus Checked, ""]renderSummaryLines issues =  let headerLine =        if any isError issues          then renderSummaryStatus Invalid          else renderSummaryStatus Warning      grouped = groupBy ((==) `on` issueTopic) (sortOn issueTopic issues)      renderGroup [] = []      renderGroup pkgIssues@(Issue {issueTopic = header} : _) =        let l1 = "  "            step = l1 <> subPathSign <> chalk Dim "• "            l2 = l1 <> "    " <> subPathSign            headerText = l1 <> chalk Dim "• " <> chalk Bold header            renderIssue Issue {issueDetails = Nothing, issueSeverity, issueMessage} =              [step <> chalk (levelColor issueSeverity) issueMessage]            renderIssue Issue {issueDetails = Just (GenericIssue {issueFile}), issueSeverity, issueMessage} =              [ step <> chalk (levelColor issueSeverity) issueMessage,                l2 <> chalk Dim ("file: " <> format issueFile)              ]            renderIssue Issue {issueDetails = Just (CommandIssue {issueCommand, issueLogFile}), issueSeverity, issueMessage} =              let cmd =                    if T.length issueCommand > 60                      then T.take 60 issueCommand <> chalk Dim "..."                      else issueCommand               in [ step <> chalk (levelColor issueSeverity) (issueMessage <> ": ") <> cmd,                    l2 <> chalk Dim ("logs: " <> format issueLogFile)                  ]            renderIssue Issue {issueDetails = Just (DependencyIssue {issueDependencies, issueFile}), issueSeverity, issueMessage} =              let groupedDeps = Map.toList $ Map.fromListWith (++) [(scope, [(depName, actual, expected)]) | (scope, depName, actual, expected) <- issueDependencies]                  depLines = concatMap formatGroup groupedDeps                  formatGroup (scope, deps) =                    (l2 <> chalk Dim "• " <> chalk Dim scope) : map formatDepLine deps                  formatDepLine (depName, actual, expected) =                    l1 <> "        " <> subPathSign <> depName <> ": " <> chalk Dim (actual <> " → " <> expected)               in step <> chalk (levelColor issueSeverity) issueMessage                    : l2 <> chalk Dim ("file: " <> format issueFile)                    : depLines            detailLines = concatMap renderIssue pkgIssues         in headerText : detailLines   in ["", headerLine, ""] <> concatMap renderGroup grouped <> [""]levelColor :: Severity -> ColorlevelColor SeverityError = RedlevelColor SeverityWarning = YellowprintSummary :: (MonadUI m) => [Issue] -> m ()printSummary = traverse_ putLine . renderSummaryLinesstatusIndicator :: (MonadIO m) => Int -> Text -> Text -> m ()statusIndicator padding prefix msg = do  liftIO clearLine  liftIO $ setCursorColumn 0  liftIO $ putStr $ toString $ "  " <> padDots padding prefix <> msg  liftIO $ hFlush stdoutrunSpinner :: (MonadIO m) => Int -> Text -> m ()runSpinner padding prefix = loop ["◜", "◠", "◝", "◞", "◡", "◟"]  where    loop (f : fs) = do      statusIndicator padding prefix f      liftIO $ threadDelay 200000 -- 200ms      loop (fs ++ [f])    loop [] = pure ()