HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulepretty-simple-4.1.3.0Haskell2010

Text.Pretty.Simple

This module contains the functions pPrint, pShow, and pString for pretty-printing any Haskell data type with a Show instance.

pPrint is the main go-to function when debugging Haskell code. pShow and pString are slight variations on pPrint.

pPrint, pShow, and pString will pretty-print in color using ANSI escape codes. They look good on a console with a dark (black) background. The variations pPrintLightBg, pShowLightBg, and pStringLightBg are for printing in color to a console with a light (white) background. The variations pPrintNoColor, pShowNoColor, and pStringNoColor are for pretty-printing without using color.

pPrint and pPrintLightBg will intelligently decide whether or not to use ANSI escape codes for coloring depending on whether or not the output is a TTY. This works in most cases. If you want to force color output, you can use the pPrintForceColor or pPrintForceColorLightBg functions.

The variations pPrintOpt, pShowOpt, and pStringOpt are used when specifying the OutputOptions. Most users can ignore these.

There are a few other functions available that are similar to pPrint.

See the Examples section at the end of this module for examples of acutally using pPrint. See the README.md for examples of printing in color.

  • 7 types
  • 48 values

Output with color on dark background

10 declarations
valuepPrint :: (MonadIO m, Show a) => a -> m ()
#

Pretty-print any data type that has a Show instance.

If you've never seen MonadIO before, you can think of this function as having the following type signature:

 pPrint :: Show a => a -> IO ()

This function will only use colors if it detects it's printing to a TTY.

This function is for printing to a dark background. Use pPrintLightBg for printing to a terminal with a light background. Different colors are used.

Prints to stdout. Use pHPrint to print to a different Handle.

Example1 expression
pPrint [Just (1, "hello")][ Just    ( 1    , "hello"    )]
valuepHPrint :: (MonadIO m, Show a) => Handle -> a -> m ()
#

Similar to pPrint, but take a Handle to print to.

Example1 expression
pHPrint stdout [Just (1, "hello")][ Just    ( 1    , "hello"    )]
valuepPrintString :: MonadIO m => String -> m ()
#

Similar to pPrint, but the first argument is a String representing a data type that has already been showed.

Example1 expression
pPrintString $ show [ Just (1, "hello"), Nothing ][ Just    ( 1    , "hello"    ), Nothing]
valuepPrintForceColor :: (MonadIO m, Show a) => a -> m ()
#

Similar to pPrint, but print in color regardless of whether the output goes to a TTY or not.

See pPrint for an example of how to use this function.

valuepShow :: Show a => a -> Text
#

Similar to pPrintForceColor, but just return the resulting pretty-printed data type as a Text instead of printing it to the screen.

This function is for printing to a dark background.

See pShowNoColor for an example of how to use this function.

valuepString :: String -> Text
#

Similar to pShow, but the first argument is a String representing a data type that has already been showed.

This will work on any String that is similar to a Haskell data type. The only requirement is that the strings are quoted, and braces, parentheses, and brackets are correctly used to represent indentation. For example, pString will correctly pretty-print JSON.

This function is for printing to a dark background.

See pStringNoColor for an example of how to use this function.

Aliases for output with color on dark background

10 declarations

Output with color on light background

10 declarations

Output with NO color

6 declarations
valuepPrintNoColor :: (MonadIO m, Show a) => a -> m ()
#

Similar to pPrint, but doesn't print in color. However, data types will still be indented nicely.

Example1 expression
pPrintNoColor $ Just ["hello", "bye"]Just    [ "hello"    , "bye"    ]
valuepPrintStringNoColor :: MonadIO m => String -> m ()
#

Similar to pPrintString, but doesn't print in color. However, data types will still be indented nicely.

Example1 expression
pPrintStringNoColor $ show $ Just ["hello", "bye"]Just    [ "hello"    , "bye"    ]
valuepShowNoColor :: Show a => a -> Text
#

Like pShow, but without color.

Example1 expression
pShowNoColor [ Nothing, Just (1, "hello") ]"[ Nothing\n, Just\n    ( 1\n    , \"hello\"\n    )\n]"

Output With OutputOptions

6 declarations
valuepPrintOpt
  1. :: (MonadIO m, Show a)
  2. => CheckColorTty
  3. -> OutputOptions
  4. -> a
  5. -> m ()
#

Similar to pPrint but takes OutputOptions to change how the pretty-printing is done.

For example, pPrintOpt can be used to make the indentation much smaller than normal.

This is what the normal indentation looks like:

Example1 expression
pPrintOpt NoCheckColorTty defaultOutputOptionsNoColor $ Just ("hello", "bye")Just    ( "hello"    , "bye"    )

This is what smaller indentation looks like:

Example2 expressions
let smallIndent = defaultOutputOptionsNoColor {outputOptionsIndentAmount = 1}pPrintOpt CheckColorTty smallIndent $ Just ("hello", "bye")Just ( "hello" , "bye" )

Lines in strings get indented

Example1 expression
pPrintOpt NoCheckColorTty defaultOutputOptionsNoColor (1, (2, "foo\nbar\nbaz", 3))( 1,    ( 2    , "foo      bar      baz"    , 3    ))

Lines get indented even in custom show instances

Example3 expressions
data Foo = Fooinstance Show Foo where show _ = "foo\nbar\nbaz"pPrintOpt CheckColorTty defaultOutputOptionsNoColor (1, (2, Foo, 3))( 1,    ( 2    , foo      bar      baz    , 3    ))

CheckColorTty determines whether to test stdout for whether or not it is connected to a TTY.

If set to NoCheckColorTty, then pPrintOpt won't check if stdout is a TTY. It will print in color depending on the value of outputOptionsColorOptions.

If set to CheckColorTty, then pPrintOpt will check if stdout is conneted to a TTY. If stdout is determined to be connected to a TTY, then it will print in color depending on the value of outputOptionsColorOptions. If stdout is determined to NOT be connected to a TTY, then it will NOT print in color, regardless of the value of outputOptionsColorOptions.

Similar to pPrintOpt, but the last argument is a string representing a data structure that has already been showed.

Example2 expressions
let foo = show (1, (2, "hello", 3))pPrintStringOpt CheckColorTty defaultOutputOptionsNoColor foo( 1,    ( 2    , "hello"    , 3    ))

OutputOptions

6 declarations
datadata OutputOptions
#

Data-type wrapping up all the options available when rendering the list of Outputs.

Constructors

  • OutputOptions
    • outputOptionsIndentAmount :: Int

      Number of spaces to use when indenting. It should probably be either 2 or 4.

    • outputOptionsPageWidth :: Int

      The maximum number of characters to fit on to one line.

    • outputOptionsCompact :: Bool

      Use less vertical (and more horizontal) space.

    • outputOptionsCompactParens :: Bool

      Group closing parentheses on to a single line.

    • outputOptionsInitialIndent :: Int

      Indent the whole output by this amount.

    • outputOptionsColorOptions :: Maybe ColorOptions

      If this is Nothing, then don't colorize the output. If this is Just colorOptions, then use colorOptions to colorize the output.

    • outputOptionsStringStyle :: StringOutputStyle

      Controls how string literals are output.

      By default, the pPrint functions escape non-printable characters, but print all printable characters:

      Example1 expression
      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.

      Example1 expression
      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.

      Example1 expression
      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.

Instances4Eq, Show, Generic, Rep
datadata StringOutputStyle
#

Control how escaped and non-printable are output for strings.

See outputOptionsStringStyle for what the output looks like with each of these options.

Constructors

  • 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.

Instances4Eq, Show, Generic, Rep
datadata CheckColorTty
#

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.

Constructors

Instances4Eq, Show, Generic, Rep

ColorOptions

7 declarations
datadata ColorOptions
#

These options are for colorizing the output of functions like pPrint.

If you don't want to use a color for one of the options, use colorNull.

Constructors

Instances4Eq, Show, Generic, Rep
datadata Style
#

Ways to style terminal output.

Instances4Eq, Show, Generic, Rep
datadata Color
#

The 8 ANSI terminal colors.

Instances3Eq, Ord, Show
  • Eq ColorDefined in prettyprinter-ansi-terminal-1.1.3 · Prettyprinter.Render.Terminal.Internal
  • Ord ColorDefined in prettyprinter-ansi-terminal-1.1.3 · Prettyprinter.Render.Terminal.Internal
  • Show ColorDefined in prettyprinter-ansi-terminal-1.1.3 · Prettyprinter.Render.Terminal.Internal
datadata Intensity
#

Dull or vivid coloring, as supported by ANSI terminals.

Instances3Eq, Ord, Show
  • Eq IntensityDefined in prettyprinter-ansi-terminal-1.1.3 · Prettyprinter.Render.Terminal.Internal
  • Ord IntensityDefined in prettyprinter-ansi-terminal-1.1.3 · Prettyprinter.Render.Terminal.Internal
  • Show IntensityDefined in prettyprinter-ansi-terminal-1.1.3 · Prettyprinter.Render.Terminal.Internal

Examples

0 declarations

Here are some examples of using pPrint on different data types. You can look at these examples to get an idea of what pPrint will output.

Simple Haskell data type

Example1 expression
data Foo a = Foo a String Char deriving Show
Example1 expression
pPrint $ Foo 3 "hello" 'a'Foo 3 "hello" 'a'

List

Example1 expression
pPrint $ [1,2,3][ 1, 2, 3]

Slightly more complicated list

Example1 expression
pPrint $ [ Foo [ (), () ] "hello" 'b' ][ Foo    [ ()    , ()    ] "hello" 'b']
Example1 expression
pPrint $ [ Foo [ "bar", "baz" ] "hello" 'a', Foo [] "bye" 'b' ][ Foo    [ "bar"    , "baz"    ] "hello" 'a', Foo [] "bye" 'b']

Record

Example1 expression
:{data Bar b = Bar  { barInt :: Int  , barA :: b  , barList :: [Foo Double]  } deriving Show:}
Example1 expression
pPrint $ Bar 1 [10, 11] [Foo 1.1 "" 'a', Foo 2.2 "hello" 'b']Bar    { barInt = 1    , barA =        [ 10        , 11        ]    , barList =        [ Foo 1.1 "" 'a'        , Foo 2.2 "hello" 'b'        ]    }

Newtype

Example1 expression
newtype Baz = Baz { unBaz :: [String] } deriving Show
Example1 expression
pPrint $ Baz ["hello", "bye"]Baz    { unBaz =        [ "hello"        , "bye"        ]    }

Newline Rules

Example1 expression
data Foo = A | B Foo | C [Foo] [Foo] deriving Show
Example1 expression
pPrint $ B ( B A )B ( B A )
Example1 expression
pPrint $ B ( B ( B A ) )B    ( B ( B A ) )
Example1 expression
pPrint $ B ( B ( B ( B A ) ) )B    ( B        ( B ( B A ) )    )
Example1 expression
pPrint $ B ( C [A, A] [B A, B (B (B A))] )B    ( C        [ A        , A        ]        [ B A        , B            ( B ( B A ) )        ]    )

Laziness

Example1 expression
take 100 . unpack . pShowNoColor $ [1..]"[ 1\n, 2\n, 3\n, 4\n, 5\n, 6\n, 7\n, 8\n, 9\n, 10\n, 11\n, 12\n, 13\n, 14\n, 15\n, 16\n, 17\n, 18\n, 19\n, 20\n, 21\n, 22"

Unicode

Example1 expression
pPrint $ Baz ["猫", "犬", "ヤギ"]Baz    { unBaz =        [ "猫"        , "犬"        , "ヤギ"        ]    }

Char

Example1 expression
pPrint 'λ''λ'

Compactness options

Example1 expression
pPrintStringOpt CheckColorTty defaultOutputOptionsDarkBg {outputOptionsCompact = True} "AST [] [Def ((3,1),(5,30)) (Id \"fact'\" \"fact'\") [] (Forall ((3,9),(3,26)) [((Id \"n\" \"n_0\"),KPromote (TyCon (Id \"Nat\" \"Nat\")))])]"AST []    [ Def        ( ( 3, 1 ), ( 5, 30 ) )        ( Id "fact'" "fact'" ) []        ( Forall            ( ( 3, 9 ), ( 3, 26 ) )            [ ( ( Id "n" "n_0" ), KPromote ( TyCon ( Id "Nat" "Nat" ) ) ) ]        )    ]
Example1 expression
pPrintOpt CheckColorTty defaultOutputOptionsDarkBg {outputOptionsCompactParens = True} $ B ( C [A, A] [B A, B (B (B A))] )B    ( C        [ A        , A ]        [ B A        , B            ( B ( B A ) ) ] )
Example1 expression
pPrintOpt CheckColorTty defaultOutputOptionsDarkBg {outputOptionsCompact = True} $ [("id", 123), ("state", 1), ("pass", 1), ("tested", 100), ("time", 12345)][    ( "id", 123 ),    ( "state", 1 ),    ( "pass", 1 ),    ( "tested", 100 ),    ( "time", 12345 )]

Initial indent

Example1 expression
pPrintOpt CheckColorTty defaultOutputOptionsDarkBg {outputOptionsInitialIndent = 3} $ B ( B ( B ( B A ) ) )   B       ( B           ( B ( B A ) )       )

Weird/illegal show instances

Example1 expression
pPrintString "2019-02-18 20:56:24.265489 UTC"2019-02-18 20:56:24.265489 UTC
Example1 expression
pPrintString "a7ed86f7-7f2c-4be5-a760-46a3950c2abf"a7ed86f7-7f2c-4be5-a760-46a3950c2abf
Example1 expression
pPrintString "192.168.0.1:8000"192.168.0.1:8000
Example1 expression
pPrintString "A @\"type\" 1"A @"type" 1
Example1 expression
pPrintString "2+2"2+2
Example1 expression
pPrintString "1.0e-2"1.0e-2
Example1 expression
pPrintString "0x1b"0x1b

Other

Making sure the spacing after a string is correct.

Example1 expression
data Foo = Foo String Int deriving Show
Example1 expression
pPrint $ Foo "bar" 0Foo "bar" 0

Non-printable characters will get escaped.

Example1 expression
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.