Pretty-printingCommon needs
Pretty-printing is the process of formatting text files (ex. source code) following stylistic conventions which adjust the positioning, spacing, colour, and size of the text to make the content easier for people to view, read, and understand (wikipedia).
Examples
Formatting source code:
int main(){std::cout<<"Hello world!\n";}
int main() {
std::cout << "Hello world!\n";
}
Formatting Haskell type signatures:
prettySig "example" ["Int", "Bool", "Char", "IO ()"]
-- Output for wide enough formats:
example :: Int -> Bool -> Char -> IO ()
-- Output for narrow formats:
example :: Int
-> Bool
-> Char
-> IO ()
Recommendations
prettyprinter is the most modern pretty-printing library and has excellent documentation, uses text instead of String
s yielding great performance, and has good adoption. The pretty package is the most popular (by number of downloads and reverse deps) but uses String
, lacks examples, and has a smaller ecosystem.
boxes is a pretty-printer which allows you to lay out content in 2 dimensions using a simple box model.
Pretty printers for programming languages
Pretty printers (and lexers, parsers, etc.) for a number of popular programming languages can be found in the language-LANG
package (language-java for example).
Not included
wl-pprint is outdated and has name clashes with many things in base (<$>
for example).
Pretty printer loosely based on Wadler's "A Prettier Printer" paper.
Example
let prettyType = align . sep . zipWith (<+>) ("::" : repeat "->")
prettySig name ty = pretty name <+> prettyType ty
in prettySig "example" ["Int", "Bool", "Char", "IO ()"]
-- Output for wide enough formats:
example :: Int -> Bool -> Char -> IO ()
-- Output for narrow formats:
example :: Int
-> Bool
-> Char
-> IO ()
- prettyprinter - the core package
- prettyprinter-ansi-terminal - a renderer suitable for ANSI terminal output including colors
- prettyprinter-compat-wl-pprint a drop-in compatibility layer for previous users of
wl-pprint
- prettyprinter-compat-ansi-wl-pprint - the same, but for previous users of
ansi-wl-pprint
- prettyprinter-compat-annotated-wl-pprint - the same, but for previous users of
annotated-wl-pprint
- prettyprinter-convert-ansi-wl-pprint - a converter, not a drop-in replacement, for documents generated by
ansi-wl-pprint
See the README for more information.
<notes are empty>
<notes are empty>
Pretty printer based on John Hughes' "The Design of a Pretty-printing Library" paper and improved by Simon Peyton Jones.
-
Uses
String
instead of text which causes poor performance when formatting a large amount of output
<notes are empty>
<notes are empty>