packages feed

pa-pretty (empty) → 0.1.0.0

raw patch · 5 files changed

+201/−0 lines, 5 filesdep +aesondep +aeson-prettydep +ansi-terminal

Dependencies added: aeson, aeson-pretty, ansi-terminal, base, hscolour, nicify-lib, pa-prelude, text

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for pa-pretty++## 0.1.0.0 -- 2023-05-24++- First version
+ LICENSE view
@@ -0,0 +1,11 @@+Copyright 2023 Possehl Analytics GmbH++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.++3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,5 @@+# `pa-pretty`++A small pretty-printing library wrapping other libs.++Currently not fast cause it’s going through `String` a lot.
+ pa-pretty.cabal view
@@ -0,0 +1,82 @@+cabal-version:      3.0+-- !!! ATTN: file autogenerated from pa-template.cabal.mustache on toplevel !!!+name:               pa-pretty+version:            0.1.0.0+synopsis:           Some pretty-printing helpers+description:        +license:            BSD-3-Clause+license-file:       LICENSE+-- author:+maintainer:         Philip Patsch <philip.patsch@possehl-analytics.com>+copyright:          2023 Possehl Analytics GmbH+homepage:           https://github.com/possehl-analytics/pa-hackage+category:           Data, Possehl-Analytics+build-type:         Simple+extra-doc-files:    CHANGELOG.md+                    README.md+-- extra-source-files:+++common common-options+  ghc-options:+      -Wall+      -Wno-type-defaults+      -Wunused-packages+      -Wredundant-constraints+      -fwarn-missing-deriving-strategies++  -- See https://downloads.haskell.org/ghc/latest/docs/users_guide/exts.html+  -- for a description of all these extensions+  default-extensions:+      -- Infer Applicative instead of Monad where possible+    ApplicativeDo++    -- Allow literal strings to be Text+    OverloadedStrings++    -- Syntactic sugar improvements+    LambdaCase+    MultiWayIf++    -- Makes the (deprecated) usage of * instead of Data.Kind.Type an error+    NoStarIsType++    -- Convenient and crucial to deal with ambiguous field names, commonly+    -- known as RecordDotSyntax+    OverloadedRecordDot++    -- does not export record fields as functions, use OverloadedRecordDot to access instead+    NoFieldSelectors++    -- Record punning+    RecordWildCards++    -- Improved Deriving+    DerivingStrategies+    DerivingVia++    -- Type-level strings+    DataKinds++    -- to enable the `type` keyword in import lists (ormolu uses this automatically)+    ExplicitNamespaces++  default-language: GHC2021++library+    import:           common-options+    exposed-modules:+      Pretty,+    -- other-modules:+    -- other-extensions:+    hs-source-dirs:   src+    build-depends:+      base <5,+      pa-prelude,+      aeson,+      aeson-pretty,+      text,+      hscolour,+      ansi-terminal,+      nicify-lib,+
+ src/Pretty.hs view
@@ -0,0 +1,98 @@+module Pretty+  ( -- * Pretty printing for error messages+    Err,+    showPretty,+    showPrettyJson,+    printPretty,+    -- constructors hidden+    prettyErrs,+    message,+    messageString,+    pretty,+    prettyString,+    hscolour',+  )+where++import Data.Aeson qualified as Json+import Data.Aeson.Encode.Pretty qualified as Aeson.Pretty+import Data.List qualified as List+import Data.Text.Lazy.Builder qualified as Text.Builder+import Language.Haskell.HsColour+  ( Output (TTYg),+    hscolour,+  )+import Language.Haskell.HsColour.ANSI (TerminalType (..))+import Language.Haskell.HsColour.Colourise+  ( defaultColourPrefs,+  )+import PossehlAnalyticsPrelude+import System.Console.ANSI (setSGRCode)+import System.Console.ANSI.Types+  ( Color (Red),+    ColorIntensity (Dull),+    ConsoleLayer (Foreground),+    SGR (Reset, SetColor),+  )+import Text.Nicify (nicify)++-- | Print any 'Show'able type to stderr, formatted nicely and in color. Very helpful for debugging.+printPretty :: Show a => a -> IO ()+printPretty a =+  a & showPretty & putStderrLn++showPretty :: Show a => a -> Text+showPretty a = a & pretty & (: []) & prettyErrs & stringToText++showPrettyJson :: Json.Value -> Text+showPrettyJson val =+  val+    & Aeson.Pretty.encodePrettyToTextBuilder+    & Text.Builder.toLazyText+    & toStrict++-- | Display a list of 'Err's as a colored error message+-- and abort the test.+prettyErrs :: [Err] -> String+prettyErrs errs = res+  where+    res = List.intercalate "\n" $ map one errs+    one = \case+      ErrMsg s -> color Red s+      ErrPrettyString s -> prettyShowString s+    -- Pretty print a String that was produced by 'show'+    prettyShowString :: String -> String+    prettyShowString = hscolour' . nicify++-- | Small DSL for pretty-printing errors+data Err+  = -- | Message to display in the error+    ErrMsg String+  | -- | Pretty print a String that was produced by 'show'+    ErrPrettyString String++-- | Plain message to display, as 'Text'+message :: Text -> Err+message = ErrMsg . textToString++-- | Plain message to display, as 'String'+messageString :: String -> Err+messageString = ErrMsg++-- | Any 'Show'able to pretty print+pretty :: Show a => a -> Err+pretty x = ErrPrettyString $ show x++-- | Pretty print a String that was produced by 'show'+prettyString :: String -> Err+prettyString s = ErrPrettyString s++-- Prettifying Helpers, mostly stolen from+-- https://hackage.haskell.org/package/hspec-expectations-pretty-diff-0.7.2.5/docs/src/Test.Hspec.Expectations.Pretty.html#prettyColor++hscolour' :: String -> String+hscolour' =+  hscolour (TTYg Ansi16Colour) defaultColourPrefs False False "" False++color :: Color -> String -> String+color c s = setSGRCode [SetColor Foreground Dull c] ++ s ++ setSGRCode [Reset]