Skip to content

Commit

Permalink
Merge pull request #24 from commercialhaskell/updates
Browse files Browse the repository at this point in the history
Add various functions from Stack project
  • Loading branch information
mpilgrem authored Sep 22, 2023
2 parents b182f41 + 092479b commit eda8a7e
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 18 deletions.
9 changes: 9 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog for rio-prettyprint

## 0.1.7.0

* Add `prettyThrowIO` and `prettyThrowM`, to throw an exception as a
`PrettyException`.
* Add `ppException`, to provide the prettiest available information about an
exception.
* Add `prettyGeneric` and `prettyWith` for greater flexibility with pretty
logging.

## 0.1.6.0

* Add `mkBulletedList` for greater flexibility in the format of such lists.
Expand Down
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
## rio-prettyprint

`rio-prettyprint` is a Haskell package that provides a library that combines the logging
capabilities of the [`rio` package](https://hackage.haskell.org/package/rio) with the pretty
printing capabilities of the
`rio-prettyprint` is a Haskell package that provides a library that combines the
logging capabilities of the
[`rio` package](https://hackage.haskell.org/package/rio) with the pretty
printing capabilities of the
[`annotated-wl-pprint` package](https://hackage.haskell.org/package/annotated-wl-pprint).
Documents can be annotated with an optional style from a set of named styles, and the
style associated with each named style can be specified as a list of ANSI Select Graphic
Rendition (SGR) commands. These commands are represented by the constructors of a type
provided by the

Documents can be annotated with an optional style from a set of named styles,
and the style associated with each named style can be specified as a list of
ANSI Select Graphic Rendition (SGR) commands. These commands are represented by
the constructors of a type provided by the
[`ansi-terminal-types` package](https://hackage.haskell.org/package/ansi-terminal-types)

The library also provides:
* a type that represents a simple, non-customisable environments that provide pretty logging
functionality; and

* a type that represents a simple, non-customisable environments that provide
pretty logging functionality; and
* a type that represents pretty exceptions.
10 changes: 7 additions & 3 deletions package.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
name: rio-prettyprint
version: 0.1.6.0
version: 0.1.7.0
synopsis: Pretty-printing for RIO
description: Combine RIO's log capabilities with pretty printing
category: Development
author: Michael Snoyman
maintainer: [email protected]
copyright: 2018-2019 FP Complete
copyright: 2018-2023 FP Complete
license: BSD3
github: commercialhaskell/rio-prettyprint
description: Combine RIO's log capabilities with pretty printing
extra-source-files:
- ChangeLog.md
- README.md
- stack.yaml

dependencies:
- base >=4.10 && < 5
Expand Down
8 changes: 6 additions & 2 deletions rio-prettyprint.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack

name: rio-prettyprint
version: 0.1.6.0
version: 0.1.7.0
synopsis: Pretty-printing for RIO
description: Combine RIO's log capabilities with pretty printing
category: Development
homepage: https://github.com/commercialhaskell/rio-prettyprint#readme
bug-reports: https://github.com/commercialhaskell/rio-prettyprint/issues
author: Michael Snoyman
maintainer: [email protected]
copyright: 2018-2019 FP Complete
copyright: 2018-2023 FP Complete
license: BSD3
license-file: LICENSE
build-type: Simple
extra-source-files:
ChangeLog.md
README.md
stack.yaml

source-repository head
type: git
Expand Down
14 changes: 12 additions & 2 deletions src/RIO/PrettyPrint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ module RIO.PrettyPrint
, prettyErrorNoIndent
, prettyErrorNoIndentL
, prettyErrorNoIndentS
-- | Pretty messages at the specified log level.
, prettyGeneric
, prettyWith

-- * Semantic styling functions
-- | These are used rather than applying colors or other styling directly,
Expand Down Expand Up @@ -130,6 +133,13 @@ displayWithColor x = do

-- TODO: switch to using implicit callstacks once 7.8 support is dropped

prettyGeneric ::
(HasTerm env, HasCallStack, Pretty b, MonadReader env m, MonadIO m)
=> LogLevel
-> b
-> m ()
prettyGeneric level = prettyWith level id

prettyWith ::
(HasTerm env, HasCallStack, Pretty b, MonadReader env m, MonadIO m)
=> LogLevel
Expand Down Expand Up @@ -273,9 +283,9 @@ mkBulletedList ::
-- ^ Spaced with a blank line between each item?
-> Char
-- ^ The character to act as the bullet point.
-> [StyleDoc]
-> [StyleDoc]
-> StyleDoc
mkBulletedList isSpaced bullet =
mkBulletedList isSpaced bullet =
mconcat . intersperse spacer . map ((fromString [bullet] <+>) . align)
where
spacer = if isSpaced then line <> line else line
Expand Down
25 changes: 23 additions & 2 deletions src/RIO/PrettyPrint/PrettyException.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@
--
module RIO.PrettyPrint.PrettyException
( PrettyException (..)
, ppException
, prettyThrowIO
, prettyThrowM
) where

import RIO ( Exception (..), Show, Typeable )
import Text.PrettyPrint.Leijen.Extended ( Pretty (..) )
import RIO
( Exception (..), Maybe (..), MonadIO, MonadThrow, Show, SomeException
, Typeable, (.), throwIO, throwM
)
import Text.PrettyPrint.Leijen.Extended ( Pretty (..), StyleDoc, string )

-- | Type representing pretty exceptions.
--
Expand All @@ -75,3 +81,18 @@ instance Pretty PrettyException where

instance Exception PrettyException where
displayException (PrettyException e) = displayException e

-- | Provide the prettiest available information about an exception.
ppException :: SomeException -> StyleDoc
ppException e = case fromException e of
Just (PrettyException e') -> pretty e'
Nothing -> (string . displayException) e

-- | Synchronously throw the given exception as a 'PrettyException'.
prettyThrowIO :: (Exception e, MonadIO m, Pretty e) => e -> m a
prettyThrowIO = throwIO . PrettyException

-- | Throw the given exception as a 'PrettyException', when the action is run in
-- the monad @m@.
prettyThrowM :: (Exception e, MonadThrow m, Pretty e) => e -> m a
prettyThrowM = throwM . PrettyException

0 comments on commit eda8a7e

Please sign in to comment.