2019-08-15
A few weeks ago I released pretty-simple-3.0.0.0.
I haven't done a release announcement in a while, so I wanted to list some of the new features that have been implemented in the last few releases. This post gives a short introduction to pretty-simple
, explains the new features, and lists some outstanding issues I'd like help with.
Introduction to pretty-simple
pretty-simple
gives you an easy way to pretty-print any Haskell data type, as long as it has a Show
instance. You are not required to make your data type an instance of a custom typeclass, or even have the Show
instance produce valid Haskell.
Let's look at an example. Imagine you have the following datatypes:
data Foo = Foo { foo1 :: Integer , foo2 :: [String] } deriving Show
data Bar = Bar { bar1 :: Double , bar2 :: [Foo] } deriving Show
You then create some values using these datatypes like the following:
If you run this in GHCi and print bar
, you get something like the following:
> print bar
Bar {bar1 = 10.55, bar2 = [Foo {foo1 = 3, foo2 = ["hello","goodbye"]},Foo {foo1 = 3, foo2 = ["hello","goodbye"]}]}
This is hard to read.
pPrint
from pretty-simple
makes this easier to read:
See the README.md for more info. See the Examples section in the Haddocks for more examples of how data types are pretty-printed.
New Features
Over the last few releases, pretty-simple
has gotten some interesting new features:
The module
Debug.Pretty.Simple
has been added. It exports functions similar toDebug.Trace
. These functions can be used the same way you would use functions liketraceShow
andtraceShowM
, but the output is pretty-printed. If you have a large data structure you would like to print when doing printf-style debugging, I suggest taking a look atDebug.Pretty.Simple
.A CLI program called
pretty-simple
has been added. This is a very simple CLI application that pretty-prints anything on stdin. This is useful if you accidentallyprint
a data type in GHCi and you forgot to usepPrint
. Just copy and paste the output to the stdin of the CLI app.The pretty-printing functions provided by
pretty-simple
now handle infinite data structures. You can attempt to pretty-print structures like infinite lists.Pretty-printing functions like
pPrint
now detect whether they are printing to a TTY device (like a terminal), or just a plain file. By default, when printing to a plain file, coloring is disabled. Alternative functions likepPrintForceColor
have been added with the previous behavior of always printing in color.
Check out the CHANGELOG.md to learn more.
Many people contributed to the above features and other bug fixes. Big thanks to:
And everyone else who reported issues!
Future Work (and Call to Action!)
There are a couple outstanding issues that would make pretty-simple
significantly better if solved. I'd appreciate help on any of these!
Highlight numbers in green.
Currently Haskell strings are highlighted in blue, but numbers aren't highlighted. It would be much easier to see numbers if they were highlighted.
This would require small changes to both the input parser and the output printer.
Gracefully handle non-balanced parentheses.
pretty-simple
has a bug where it ignores everything after a closing non-balanced parenthesis. It would be great to fix this.This would require small changes to the input parser.
Create a live, interactive web-view of
pretty-simple
.This would be a website where new users could play around with
pretty-simple
.1This is quite different to the other tasks, but might be cool for someone interested in GHCJS, Reflex, miso, etc.
Please feel free to send a PR with something above either fully implemented, or partially implemented if you get stuck! If you're a beginning Haskeller, I will try to help out with any small problems, coding conventions, etc!
Conclusion
pretty-simple
makes it easy to pretty-print complicated data structures. If you've ever found the normal print
output hard to read, try pretty-simple
!
Footnotes
There is actually a similar package to
pretty-simple
that has a website like this.The author wrote up a short explanation on how this works, although for
pretty-simple
I'd be fine with just generating the entire site with something likereflex-dom
.↩︎
tags: haskell