packages feed

pretty-simple 3.2.3.0 → 3.3.0.0

raw patch · 4 files changed

+84/−15 lines, 4 files

Files

CHANGELOG.md view
@@ -1,4 +1,12 @@ +## 3.3.0.0++*   Add an output option to print escaped and non-printable characters+    literally when outputting strings.+    [#68](https://github.com/cdepillabout/pretty-simple/pull/68) and+    [#69](https://github.com/cdepillabout/pretty-simple/pull/69)+    Thanks Joe Hermaszewski ([@expipiplus1](https://github.com/expipiplus1))!+ ## 3.2.3.0  *   Fix a bug that messes up printing identifiers with `'` in the name.
pretty-simple.cabal view
@@ -1,5 +1,5 @@ name:                pretty-simple-version:             3.2.3.0+version:             3.3.0.0 synopsis:            pretty printer for data types with a 'Show' instance. description:         Please see <https://github.com/cdepillabout/pretty-simple#readme README.md>. homepage:            https://github.com/cdepillabout/pretty-simple
src/Text/Pretty/Simple.hs view
@@ -93,6 +93,7 @@   , pStringOpt   -- * 'OutputOptions'   , OutputOptions(..)+  , StringOutputStyle(..)   , defaultOutputOptionsDarkBg   , defaultOutputOptionsLightBg   , defaultOutputOptionsNoColor@@ -118,10 +119,11 @@ import System.IO (Handle, stdout)  import Text.Pretty.Simple.Internal-       (CheckColorTty(..), OutputOptions(..), defaultColorOptionsDarkBg,-        defaultColorOptionsLightBg, defaultOutputOptionsDarkBg,-        defaultOutputOptionsLightBg, defaultOutputOptionsNoColor,-        hCheckTTY, expressionParse, expressionsToOutputs, render)+       (CheckColorTty(..), OutputOptions(..), StringOutputStyle(..),+        defaultColorOptionsDarkBg, defaultColorOptionsLightBg,+        defaultOutputOptionsDarkBg, defaultOutputOptionsLightBg,+        defaultOutputOptionsNoColor, hCheckTTY, expressionParse,+        expressionsToOutputs, render)  -- $setup -- >>> import Data.Text.Lazy (unpack)@@ -665,3 +667,6 @@ -- -- >>> pPrint "this string has non-printable characters: \x8 and \x9" -- "this string has non-printable characters: \x8 and \x9"+--+-- If you don't want non-printable characters to be escaped, take a look at+-- 'outputOptionsStringStyle' and 'StringOutputStyle'.
src/Text/Pretty/Simple/Internal/OutputPrinter.hs view
@@ -46,6 +46,9 @@ import Text.Pretty.Simple.Internal.Output        (NestLevel(..), Output(..), OutputType(..)) +-- $setup+-- >>> import Text.Pretty.Simple (pPrintString, pPrintStringOpt)+ -- | Determines whether pretty-simple should check if the output 'Handle' is a -- TTY device.  Normally, users only want to print in color if the output -- 'Handle' is a TTY device.@@ -61,6 +64,24 @@   -- 'outputOptionsColorOptions'.   deriving (Eq, Generic, Show, Typeable) +-- | Control how escaped and non-printable are output for strings.+--+-- See 'outputOptionsStringStyle' for what the output looks like with each of+-- these options.+data StringOutputStyle+  = Literal+  -- ^ Output string literals by printing the source characters exactly.+  --+  -- For examples: without this option the printer will insert a newline in+  -- place of @"\n"@, with this options the printer will output @'\'@ and+  -- @'n'@. Similarly the exact escape codes used in the input string will be+  -- replicated, so @"\65"@ will be printed as @"\65"@ and not @"A"@.+  | EscapeNonPrintable+  -- ^ Replace non-printable characters with hexadecimal escape sequences.+  | DoNotEscapeNonPrintable+  -- ^ Output non-printable characters without modification.+  deriving (Eq, Generic, Show, Typeable)+ -- | Data-type wrapping up all the options available when rendering the list -- of 'Output's. data OutputOptions = OutputOptions@@ -71,9 +92,43 @@   -- ^ If this is 'Nothing', then don't colorize the output.  If this is   -- @'Just' colorOptions@, then use @colorOptions@ to colorize the output.   ---  , outputOptionsEscapeNonPrintable :: Bool-  -- ^ Whether to replace non-printable characters with hexadecimal escape-  -- sequences.+  , outputOptionsStringStyle :: StringOutputStyle+  -- ^ Controls how string literals are output.+  --+  -- By default, the pPrint functions escape non-printable characters, but+  -- print all printable characters:+  --+  -- >>> pPrintString "\"A \\x42 Ä \\xC4 \\x1 \\n\""+  -- "A B Ä Ä \x1 "+  --+  -- Here, you can see that the character @A@ has been printed as-is.  @\x42@+  -- has been printed in the non-escaped version, @B@.  The non-printable+  -- character @\x1@ has been printed as @\x1@.  Newlines will be removed to+  -- make the output easier to read.+  --+  -- This corresponds to the 'StringOutputStyle' called 'EscapeNonPrintable'.+  --+  -- (Note that in the above and following examples, the characters have to be+  -- double-escaped, which makes it somewhat confusing...)+  --+  -- Another output style is 'DoNotEscapeNonPrintable'.  This is similar+  -- to 'EscapeNonPrintable', except that non-printable characters get printed+  -- out literally to the screen.+  --+  -- >>> pPrintStringOpt CheckColorTty defaultOutputOptionsDarkBg{ outputOptionsStringStyle = DoNotEscapeNonPrintable } "\"A \\x42 Ä \\xC4 \\n\""+  -- "A B Ä Ä "+  --+  -- If you change the above example to contain @\x1@, you can see that it is+  -- output as a literal, non-escaped character.  Newlines are still removed+  -- for readability.+  --+  -- Another output style is 'Literal'.  This just outputs all escape characters.+  --+  -- >>> pPrintStringOpt CheckColorTty defaultOutputOptionsDarkBg{ outputOptionsStringStyle = Literal } "\"A \\x42 Ä \\xC4 \\x1 \\n\""+  -- "A \x42 Ä \xC4 \x1 \n"+  --+  -- You can see that all the escape characters get output literally, including+  -- newline.   } deriving (Eq, Generic, Show, Typeable)  -- | Default values for 'OutputOptions' when printing to a console with a dark@@ -84,7 +139,7 @@   OutputOptions   { outputOptionsIndentAmount = 4   , outputOptionsColorOptions = Just defaultColorOptionsDarkBg-  , outputOptionsEscapeNonPrintable = True+  , outputOptionsStringStyle = EscapeNonPrintable   }  -- | Default values for 'OutputOptions' when printing to a console with a light@@ -95,7 +150,7 @@   OutputOptions   { outputOptionsIndentAmount = 4   , outputOptionsColorOptions = Just defaultColorOptionsLightBg-  , outputOptionsEscapeNonPrintable = True+  , outputOptionsStringStyle = EscapeNonPrintable   }  -- | Default values for 'OutputOptions' when printing using using ANSI escape@@ -106,7 +161,7 @@   OutputOptions   { outputOptionsIndentAmount = 4   , outputOptionsColorOptions = Nothing-  , outputOptionsEscapeNonPrintable = True+  , outputOptionsStringStyle = EscapeNonPrintable   }  -- | Given 'OutputOptions', disable colorful output if the given handle@@ -171,10 +226,11 @@     ]   where     process :: OutputOptions -> String -> String-    process opts =-      if outputOptionsEscapeNonPrintable opts-        then indentSubsequentLinesWith spaces . escapeNonPrintable . readStr-        else indentSubsequentLinesWith spaces . readStr+    process opts = case outputOptionsStringStyle opts of+      Literal -> id+      EscapeNonPrintable ->+        indentSubsequentLinesWith spaces . escapeNonPrintable . readStr+      DoNotEscapeNonPrintable -> indentSubsequentLinesWith spaces . readStr       where         spaces :: String         spaces = replicate (indentSpaces + 2) ' '