pretty-simple: pretty printer for data types with a 'Show' instance.

[ bsd3, library, text ] [ Propose Tags ]

Please see README.md.


[Skip to Readme]

Flags

Automatic Flags
NameDescriptionDefault
buildexe

Build an small command line program that pretty-print anything from stdin.

Enabled
buildexample

Build a small example program showing how to use the pPrint function

Disabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.2.0.0, 0.3.0.0, 1.0.0.0, 1.0.0.1, 1.0.0.2, 1.0.0.3, 1.0.0.4, 1.0.0.5, 1.0.0.6, 1.1.0.0, 1.1.0.1, 1.1.0.2, 1.1.0.3, 2.0.0.0, 2.0.1.0, 2.0.2.0, 2.0.2.1, 2.1.0.0, 2.1.0.1, 2.2.0.0, 2.2.0.1, 3.0.0.0, 3.1.0.0, 3.1.1.0, 3.2.0.0, 3.2.1.0, 3.2.2.0, 3.2.3.0, 3.3.0.0, 4.0.0.0, 4.1.0.0, 4.1.1.0, 4.1.2.0
Change log CHANGELOG.md
Dependencies aeson, base (>=4.8 && <5), bytestring, containers, mtl (>=2.2), optparse-applicative, pretty-simple, prettyprinter (>=1.7.0), prettyprinter-ansi-terminal (>=1.1.2), text (>=1.2), transformers (>=0.4) [details]
License BSD-3-Clause
Copyright 2017-2019 Dennis Gosnell
Author Dennis Gosnell
Maintainer cdep.illabout@gmail.com
Category Text
Home page https://github.com/cdepillabout/pretty-simple
Source repo head: git clone git@github.com:cdepillabout/pretty-simple.git
Uploaded by cdepillabout at 2022-10-15T22:17:49Z
Distributions Arch:4.1.2.0, Debian:3.2.3.0, Fedora:4.1.2.0, LTSHaskell:4.1.2.0, NixOS:4.1.2.0, Stackage:4.1.2.0, openSUSE:4.1.2.0
Reverse Dependencies 36 direct, 79 indirect [details]
Executables pretty-simple-json-example, pretty-simple-example, pretty-simple
Downloads 37024 total (261 in the last 30 days)
Rating 2.5 (votes: 5) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for pretty-simple-4.1.2.0

[back to package description]

Text.Pretty.Simple

Build Status Hackage Stackage LTS Stackage Nightly BSD3 license

pretty-simple is a pretty printer for Haskell data types that have a Show instance.

For example, imagine the following Haskell data types and values:

data Foo = Foo { foo1 :: Integer , foo2 :: [String] } deriving Show

foo :: Foo
foo = Foo 3 ["hello", "goodbye"]

data Bar = Bar { bar1 :: Double , bar2 :: [Foo] } deriving Show

bar :: Bar
bar = Bar 10.55 [foo, foo]

If you run this in ghci and type print bar, you'll get output like this:

> print bar
Bar {bar1 = 10.55, bar2 = [Foo {foo1 = 3, foo2 = ["hello","goodbye"]},Foo {foo1 = 3, foo2 = ["hello","goodbye"]}]}

This is pretty hard to read. Imagine if there were more fields or it were even more deeply nested. It would be even more difficult to read.

pretty-simple can be used to print bar in an easy-to-read format:

example screenshot

There's a web app compiled with GHCJS where you can play around with pretty-simple running in your browser.

Usage

pretty-simple can be easily used from ghci when debugging.

When using stack to run ghci, just append the --package flag to the command line to load pretty-simple:

$ stack ghci --package pretty-simple

Or, with cabal:

$ cabal repl --build-depends pretty-simple

Once you get a prompt in ghci, you can use import to get pretty-simple's pPrint function in scope.

> import Text.Pretty.Simple (pPrint)

You can test out pPrint with simple data types like Maybe or tuples.

> pPrint $ Just ("hello", "goodbye")
Just
    ( "hello"
    , "goodbye"
    )

Features

  • Easy-to-read
    • Complex data types are simple to understand.
  • Color
    • Prints in color using ANSI escape codes.
    • It is possible to print without color by using the pPrintNoColor function.
  • Rainbow Parentheses
    • Easy to understand deeply nested data types.
  • Configurable
    • Indentation, compactness, colors and more are configurable with the pPrintOpt function.
  • Fast
    • No problem pretty-printing data types thousands of lines long.
  • Works with any data type with a Show instance
    • Some common Haskell data types have a Show instance that produces non-valid Haskell code. pretty-simple will pretty-print even these data types.

Why not (some other package)?

Other pretty-printing packages have some combination of these defects:

  • No options for printing in color.
  • No options for changing the amount of indentation
  • Requires every data type to be an instance of some special typeclass (instead of just Show).
  • Requires all Show instances to output valid Haskell code.

Other Uses

Pretty-print all GHCi output

The pPrint function can be used as the default output function in GHCi.

All you need to do is run GHCi with a command like one of these:

$ stack ghci --ghci-options "-interactive-print=Text.Pretty.Simple.pPrint" --package pretty-simple
$ cabal repl --repl-options "-interactive-print=Text.Pretty.Simple.pPrint" --build-depends pretty-simple

Now, whenever you make GHCi evaluate an expression, GHCi will pretty-print the result using pPrint! See here for more info on this neat feature in GHCi.

Pretty-printing JSON

pretty-simple can be used to pretty-print any String that is similar to Haskell data types. The only requirement is that the String must correctly use brackets, parenthese, and braces to indicate nesting.

For example, the pString function can be used to pretty-print JSON.

Recall our example from before.

data Foo = Foo { foo1 :: Integer , foo2 :: [String] } deriving Show

foo :: Foo
foo = Foo 3 ["hello", "goodbye"]

data Bar = Bar { bar1 :: Double , bar2 :: [Foo] } deriving Show

bar :: Bar
bar = Bar 10.55 [foo, foo]

You can use aeson to turn these data types into JSON. First, you must derive ToJSON instances for the data types. It is easiest to do this with Template Haskell:

{-# LANGUAGE TemplateHaskell #-}

$(deriveJSON defaultOptions ''Foo)
$(deriveJSON defaultOptions ''Bar)

If you run this in ghci and type encode bar, you'll get output like this:

> import Data.Aeson (encode)
> putLazyByteStringLn $ encode bar
{"bar1":10.55,"bar2":[{"foo1":3,"foo2":["hello","goodbye"]},{"foo1":3,"foo2":["hello","goodbye"]}]}

Just like Haskell's normal print output, this is pretty hard to read.

pretty-simple can be used to pretty-print the JSON-encoded bar in an easy-to-read format:

json example screenshot

(You can find the lazyByteStringToString, putLazyByteStringLn, and putLazyTextLn in the ExampleJSON.hs file.)

Pretty-printing from the command line

pretty-simple includes a command line executable that can be used to pretty-print anything passed in on stdin.

It can be installed to ~/.local/bin/ with the following command.

$ stack install pretty-simple

When run on the command line, you can paste in the Haskell datatype you want to be formatted, then hit Ctrl-D:

cli example screenshot

This is very useful if you accidentally print out a Haskell data type with print instead of pPrint.

Contributions

Feel free to open an issue or PR for any bugs/problems/suggestions/improvements.

Maintainers