• Stars
    star
    242
  • Rank 167,048 (Top 4 %)
  • Language
    Haskell
  • License
    BSD 3-Clause "New...
  • Created almost 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

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"
    )

If for whatever reason you're not able to incur a dependency on the pretty-simple library, you can simulate its behaviour by using process to call out to the command line executable (see below for installation):

pPrint :: Show a => a -> IO ()
pPrint = putStrLn <=< readProcess "pretty-simple" [] . show

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

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

More Repositories

1

termonad

Terminal emulator configurable in Haskell.
Haskell
400
star
2

servant-checked-exceptions

type-level errors for Servant APIs.
Haskell
73
star
3

nix-query-tree-viewer

GTK viewer for the output of `nix-store --query --tree`
Rust
65
star
4

password

datatypes and functions for easily working with passwords in Haskell
Haskell
49
star
5

stacklock2nix

Easily build a Haskell project from a stack.yaml.lock file with Nix
Nix
34
star
6

world-peace

open union and open product types in Haskell
Haskell
32
star
7

testing-code-that-accesses-db-in-haskell

5 ways to test code that accesses a DB in Haskell
Haskell
30
star
8

post-about-nix-and-haskell

29
star
9

cabal2nixWithoutIFD

PureScript
29
star
10

purescript2nix

Tool for easily building PureScript projects with Nix
Nix
17
star
11

purescript-email-validate

Validating an email address string against RFC 5322
PureScript
17
star
12

servant-rawm

Effectful Raw handler for Servant servers.
Haskell
16
star
13

haskell-type-families-presentation

This is a presentation about haskell's type families.
JavaScript
15
star
14

nix-reverse-deps-of-haskell-package

Find or build all reverse dependencies of a Haskell package using Nix
Nix
14
star
15

servant-static-th

Embed a directory of static files in your application and serve them from your Servant server
Haskell
14
star
16

break-time

break timer that forces you to take a break
Rust
13
star
17

nix-haskell-updates-status

Automatically-updated Hydra status of the `haskell-updates` branch in Nixpkgs
Shell
13
star
18

servant-on-heroku

Example app showing how to release a Haskell web app on Heroku
Haskell
9
star
19

print-console-colors

Print all the ANSI console colors for your terminal
Haskell
8
star
20

purescript-boxes

PureScript
7
star
21

yahoo-finance-api

Haskell wrapper for Yahoo Finance API
Haskell
7
star
22

purescript-road-to-react

PureScript translation of example JavaScript React app from https://www.roadtoreact.com/
PureScript
7
star
23

cps-vs-codensity-vs-reflection-without-remorse

Example code showing the difference between CPS, codensity, and reflection without remorse styles in Haskell
Haskell
6
star
24

nix-cabal-example-project

Nix
6
star
25

docs

My dot files.
Shell
6
star
26

xml-html-qq

QuasiQuoter for XML and HTML Documents
Haskell
5
star
27

emailaddress

Wrapper around email-validate adding new non-orphaned instances to EmailAddress
Haskell
5
star
28

highlight

command line tool for highlighting parts of files that match a regex
Haskell
5
star
29

heterocephalus-example

simple explanation on how to use -ddump-splices with stack
Haskell
5
star
30

geneticmidi

a java project to generate a chosen midi file using a genetic algorithm
Java
4
star
31

purenix-examples

Examples of common Nix things written in PureNix
Nix
4
star
32

open-blog-posts

Blog posts
4
star
33

from-sum

Canonical fromMaybeM and fromEitherM functions.
Haskell
4
star
34

envelope

Envelope type used to return responses from a JSON API for Haskell
Haskell
4
star
35

servant-example

Example servant project used to explain advanced Haskell features like type-level strings, type families, etc.
Haskell
3
star
36

advent-of-code

My solutions to various Advent of Code problems
Haskell
3
star
37

purescript-typesafe-localstorage

PureScript
3
star
38

read-env-var

Haskell library for safely reading environment variables
Haskell
3
star
39

icfp-tutorial-extensible-effects

Haskell
3
star
40

coq-playground

Coq
2
star
41

software-foundations

https://softwarefoundations.cis.upenn.edu/
HTML
2
star
42

advent-of-code2018

Advent of Code 2018 problems in Haskell. Archived and moved to https://github.com/cdepillabout/advent-of-code
Haskell
2
star
43

focuslist

A Haskell data structure for lists with a focus
Haskell
2
star
44

icfp-tutorial-generics

Haskell
2
star
45

dict-scrape

A web scraper that gets information from yahoo.co.jp's japanese dictionaries
Python
2
star
46

large-file-parser-ng

Parsing a large file with constant heap usage
Haskell
2
star
47

ssh

Ssh server/client in Haskell. Fork of http://hackage.haskell.org/package/ssh
Haskell
2
star
48

ctf

Notes about problems I could not solve from CTFs.
Haskell
1
star
49

large-file-parser

Haskell
1
star
50

singletons-doggy-test

Test code using Haskell's singletons library.
Haskell
1
star
51

haskell-meetup-2016-10-23

Haskell
1
star
52

accelerate-singleton-test

Haskell
1
star
53

purescript-community-presentation

lightning talk about the PureScript Community (in Japanese)
1
star
54

hn-markov-headlines

Create new headlines for hacker news using markov chains
Haskell
1
star
55

tempreader

Throw-away code demonstrating a little about the reader monad
Haskell
1
star
56

tohmato

online pomodoro timer in elm
JavaScript
1
star
57

oi-oi-oi

Haskell
1
star
58

coursera

solutions to coursera courses
MATLAB
1
star
59

haskell-uuid-test

Test creating uuids in Haskell.
Haskell
1
star
60

lenstestwhat

Haskell
1
star
61

udacity-scalable-microservices-with-kubernetes

Nix
1
star
62

mystreaming

Haskell
1
star
63

bash

Unofficial bash repository (since no official repository exists). This repo will not be kept up-to-date.
C
1
star
64

continuation-monad-test

This is just a test project for playing around with the continuation monad.
Haskell
1
star
65

haskell-backprop

Haskell
1
star
66

purescript-in-purescript

purescript compiler implemented in purescript
PureScript
1
star
67

free-monad-test

Some code to play around with testing free monads.
Haskell
1
star
68

coq-equivalence-not-congruence

Coq proof of an equivalence relation that is not congruent on the Imp language from Software Foundations
Coq
1
star
69

scary

Haskell
1
star
70

purescript-twitter-types

PureScript
1
star
71

hailgun-simple

Simple wrapper around the hailgun package.
Haskell
1
star
72

ddump-splices-test

Haskell
1
star
73

wwtw-haskell-solver

A solution to wibbly-wobbly-timey-wimey from the Defcon 23 (2015) prequalifiers. Not yet complete.
Haskell
1
star
74

ebstuff

Various projects using the eb library for accessing epwing dictionaries
C
1
star
75

recursion-schemes-test

This is just a small test of the haskell recursion-schemes library.
Haskell
1
star
76

ropasaurusrex

Solution for ropasaurusrex
Shell
1
star
77

twitter-app-only

Haskell
1
star
78

example-haskell-nix-flake

Nix
1
star
79

the-monad-reader-24-sample-code

Sample code from the Monad Reader Issue 24: https://themonadreader.files.wordpress.com/2015/08/issue24.pdf
Haskell
1
star
80

haskell-github-updater

This is a haskell cgi script that will automatically update a repo when hit with one of github's callbacks.
Haskell
1
star
81

anki-high-priority

Automatically mark facts as HighPriority in Anki decks
Python
1
star
82

port-purescript-package

Script to easily port a PureScript package to an alternative backend
Shell
1
star
83

frp

Haskell
1
star
84

free-monad-logger

Haskell
1
star
85

dependent-walrus

Haskell
1
star
86

reverse-engineering-lt

A lightening-talk about reverse engineering on Linux
CSS
1
star
87

purescript-words-lines

words, unwords, lines, and unlines functions for PureScript
PureScript
1
star
88

saabanto

Rust
1
star
89

haskell-git-too-many-cherry-picks

Checks which commits have been cherry-picked between branches.
Haskell
1
star
90

screenshot-to-clipboard

Utility for taking a screenshot and copying it to the clipboard
Haskell
1
star
91

codejam

Haskell
1
star
92

google-code-jam-twenty-seventeen

Haskell
1
star
93

xml-indexed-cursor

Indexed XML cursors similar to Text.XML.Cursor from xml-conduit
Haskell
1
star
94

math-stuff

Haskell
1
star
95

recursion-schemes-two

A small repository for playing around with recursion-schemes.
Haskell
1
star
96

codeiq-cyberdefence-reversing-challenge

Files for a reversing challenge from codeiq
Haskell
1
star
97

haskell-random-dice-test

Creates a test for the solution to the problem from here (Simulate a 7-sided die using only a 5-sided die): http://www.careercup.com/question?id=3043
Haskell
1
star
98

haskell-sqlitetest-error

Small repo to reproduce sqlite error
Haskell
1
star
99

brazile-web-scraper

web scraper for brazilian website
Haskell
1
star
100

example-static-haskell-nix

Example showing how to build a statically-linked Haskell executable with Nix and attach it to a GitHub Release
Nix
1
star