packages feed

rio-prettyprint 0.1.2.0 → 0.1.3.0

raw patch · 3 files changed

+180/−1 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ RIO.PrettyPrint.PrettyException: PrettyException :: e -> PrettyException
+ RIO.PrettyPrint.PrettyException: data PrettyException
+ RIO.PrettyPrint.PrettyException: instance GHC.Exception.Type.Exception RIO.PrettyPrint.PrettyException.PrettyException
+ RIO.PrettyPrint.PrettyException: instance GHC.Show.Show RIO.PrettyPrint.PrettyException.PrettyException
+ RIO.PrettyPrint.PrettyException: instance Text.PrettyPrint.Leijen.Extended.Pretty RIO.PrettyPrint.PrettyException.PrettyException
+ RIO.PrettyPrint.Simple: data SimplePrettyApp
+ RIO.PrettyPrint.Simple: instance RIO.Prelude.Logger.HasLogFunc RIO.PrettyPrint.Simple.SimplePrettyApp
+ RIO.PrettyPrint.Simple: instance RIO.PrettyPrint.HasTerm RIO.PrettyPrint.Simple.SimplePrettyApp
+ RIO.PrettyPrint.Simple: instance RIO.PrettyPrint.StylesUpdate.HasStylesUpdate RIO.PrettyPrint.Simple.SimplePrettyApp
+ RIO.PrettyPrint.Simple: instance RIO.Process.HasProcessContext RIO.PrettyPrint.Simple.SimplePrettyApp
+ RIO.PrettyPrint.Simple: mkSimplePrettyApp :: MonadIO m => LogFunc -> Maybe ProcessContext -> Bool -> Int -> StylesUpdate -> m SimplePrettyApp
+ RIO.PrettyPrint.Simple: runSimplePrettyApp :: MonadIO m => Int -> StylesUpdate -> RIO SimplePrettyApp a -> m a

Files

rio-prettyprint.cabal view
@@ -5,7 +5,7 @@ -- see: https://github.com/sol/hpack  name:           rio-prettyprint-version:        0.1.2.0+version:        0.1.3.0 synopsis:       Pretty-printing for RIO description:    Combine RIO's log capabilities with pretty printing category:       Development@@ -26,6 +26,8 @@   exposed-modules:       RIO.PrettyPrint       RIO.PrettyPrint.DefaultStyles+      RIO.PrettyPrint.PrettyException+      RIO.PrettyPrint.Simple       RIO.PrettyPrint.StylesUpdate       RIO.PrettyPrint.Types       Text.PrettyPrint.Leijen.Extended
+ src/RIO/PrettyPrint/PrettyException.hs view
@@ -0,0 +1,79 @@+{-# LANGUAGE NoImplicitPrelude         #-}+{-# LANGUAGE ExistentialQuantification #-}++{-|+This module provides a type representing pretty exceptions. It can be used as in+the example below:++@+{-# LANGUAGE NoImplicitPrelude #-}+{-# LANGUAGE OverloadedStrings #-}++module Main (main) where++import RIO+         ( Exception, Handler (..), IO, RIO, Show, SomeException (..), Typeable+         , ($), catches, displayException, exitFailure, fromString, logError+         , mempty, throwIO+         )+import RIO.PrettyPrint+         ( Pretty (..), Style (..), (<+>), prettyError, prettyInfo, style )+import RIO.PrettyPrint.PrettyException ( PrettyException (..) )+import RIO.PrettyPrint.Simple ( SimplePrettyApp, runSimplePrettyApp )++main :: IO ()+main = runSimplePrettyApp 80 mempty (action `catches` handleExceptions)+ where+  action :: RIO SimplePrettyApp ()+  action = do+      prettyInfo "Running action!"+      throwIO (PrettyException MyPrettyException)++  handleExceptions :: [Handler (RIO SimplePrettyApp) ()]+  handleExceptions =+    [ Handler handlePrettyException+    , Handler handleSomeException+    ]++  handlePrettyException :: PrettyException -> RIO SimplePrettyApp ()+  handlePrettyException e = do+    prettyError $ pretty e+    exitFailure++  handleSomeException :: SomeException -> RIO SimplePrettyApp ()+  handleSomeException (SomeException e) = do+    logError $ fromString $ displayException e+    exitFailure++data MyPrettyException+  = MyPrettyException+  deriving (Show, Typeable)++instance Pretty MyPrettyException where+  pretty MyPrettyException =+    "My" <+> style Highlight "pretty" <+> "exception!"++instance Exception MyPrettyException+@+-}+module RIO.PrettyPrint.PrettyException+  ( PrettyException (..)+  ) where++import RIO (Exception, Show (..), Typeable)+import Text.PrettyPrint.Leijen.Extended (Pretty (..))++-- | Type representing pretty exceptions.+--+-- @since 0.1.3.0+data PrettyException+  = forall e. (Exception e, Pretty e) => PrettyException e+  deriving Typeable++instance Show PrettyException where+  show (PrettyException e) = show e++instance Pretty PrettyException where+  pretty (PrettyException e) = pretty e++instance Exception PrettyException
+ src/RIO/PrettyPrint/Simple.hs view
@@ -0,0 +1,98 @@+{-# LANGUAGE NoImplicitPrelude #-}++{-|+This module exports a 'SimplePrettyApp' type, for providing a basic environment+including pretty printing functionality.+-}+module RIO.PrettyPrint.Simple+  ( SimplePrettyApp+  , mkSimplePrettyApp+  , runSimplePrettyApp+  ) where++import System.Environment (lookupEnv)++import RIO+         ( Bool (..), HasLogFunc (..), Int, LogFunc, Maybe (..), MonadIO, RIO+         , ($), (<$>), isJust, lens, liftIO, logOptionsHandle, maybe, pure+         , runRIO, setLogUseColor, stderr, withLogFunc+         )+import RIO.Process+         ( HasProcessContext (..), ProcessContext, mkDefaultProcessContext )++import RIO.PrettyPrint (HasTerm (..))+import RIO.PrettyPrint.StylesUpdate (HasStylesUpdate (..), StylesUpdate (..))++-- | A simple, non-customizable environment type, which provides+-- pretty printing functionality.+--+-- @since 0.1.3.0+data SimplePrettyApp = SimplePrettyApp+  { spaLogFunc :: !LogFunc+  , spaProcessContext :: !ProcessContext+  , spaUseColor :: !Bool+  , spaTermWidth :: !Int+  , spaStylesUpdate :: !StylesUpdate+  }++instance HasLogFunc SimplePrettyApp where+  logFuncL = lens spaLogFunc (\x y -> x { spaLogFunc = y })++instance HasProcessContext SimplePrettyApp where+  processContextL = lens spaProcessContext (\x y -> x { spaProcessContext = y })++instance HasStylesUpdate SimplePrettyApp where+  stylesUpdateL = lens spaStylesUpdate (\x y -> x { spaStylesUpdate = y })++instance HasTerm SimplePrettyApp where+  useColorL = lens spaUseColor (\x y -> x { spaUseColor = y })+  termWidthL = lens spaTermWidth (\x y -> x { spaTermWidth = y })++-- | Constructor for 'SimplePrettyApp'. If 'ProcessContext' is not supplied+-- 'mkDefaultProcessContext' will be used to create it.+--+-- @since 0.1.3.0+mkSimplePrettyApp+  :: MonadIO m+  => LogFunc+  -> Maybe ProcessContext+  -> Bool+     -- ^ Use color?+  -> Int+     -- ^ Terminal width+  -> StylesUpdate+  -> m SimplePrettyApp+mkSimplePrettyApp logFunc mProcessContext useColor termWidth stylesUpdate = do+  processContext <- maybe mkDefaultProcessContext pure mProcessContext+  pure $ SimplePrettyApp+    { spaLogFunc = logFunc+    , spaProcessContext = processContext+    , spaUseColor = useColor+    , spaTermWidth = termWidth+    , spaStylesUpdate = stylesUpdate+    }++-- | Run with a default configured @SimplePrettyApp@, consisting of:+--+-- * Logging to 'stderr'+--+-- * If the @RIO_VERBOSE@ environment variable is set, turns on verbose logging+--+-- * Default process context+--+-- * Logging using color+--+-- @since 0.1.3.0+runSimplePrettyApp+  :: MonadIO m+  => Int+     -- ^ Terminal width+  -> StylesUpdate+  -> RIO SimplePrettyApp a+  -> m a+runSimplePrettyApp termWidth stylesUpdate m = liftIO $ do+  verbose <- isJust <$> lookupEnv "RIO_VERBOSE"+  lo <- setLogUseColor True <$> logOptionsHandle stderr verbose+  withLogFunc lo $ \lf -> do+    simplePrettyApp <- mkSimplePrettyApp lf Nothing True termWidth stylesUpdate+    runRIO simplePrettyApp m