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 Strings 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).

edit description
or press Ctrl+Enter to savemarkdown supported
#
Wadler/Leijen
move item up move item down edit item info delete item
Summary edit summary

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 ()
Summary quit editing summary
Prosedit prosquit editing pros
  • Uses Text instead of String for better performance
    move trait up move trait down edit trait delete trait
  • Supports annotations (coloring, mouse-over hovers, adding URLs, etc.)
    move trait up move trait down edit trait delete trait
  • Excellent docs and tutorial
    move trait up move trait down edit trait delete trait
  • Has compat packages to help migrate from other pretty printing libraries
    move trait up move trait down edit trait delete trait

press Ctrl+Enter or Enter to addmarkdown supportededit off
Consedit consquit editing cons

    press Ctrl+Enter or Enter to addmarkdown supportededit off
    Ecosystemedit ecosystem

    See the README for more information.

    Ecosystemquit editing ecosystemor press Ctrl+Enter to savemarkdown supported
    Notes
    collapse notesedit notes

    <notes are empty>

    add something!

    #
    pretty-simple (Hackage)
    other
    move item up move item down edit item info delete item
    Summary edit summary

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

    Summary quit editing summary
    Prosedit prosquit editing pros
    • can turn deriving(Show) into something readable automatically
      move trait up move trait down edit trait delete trait
    • includes a drop-in replacement for Debug.Trace
      move trait up move trait down edit trait delete trait

    press Ctrl+Enter or Enter to addmarkdown supportededit off
    Consedit consquit editing cons
    • more limited than prettyprinter if you intend to write your own printers
      move trait up move trait down edit trait delete trait

    press Ctrl+Enter or Enter to addmarkdown supportededit off
    Ecosystemedit ecosystem
    Ecosystemquit editing ecosystemor press Ctrl+Enter to savemarkdown supported
    Notes
    collapse notesedit notes

    <notes are empty>

    add something!

    #
    pretty (Hackage)
    Wadler/Leijen
    move item up move item down edit item info delete item
    Summary edit summary

    Pretty printer based on John Hughes' "The Design of a Pretty-printing Library" paper and improved by Simon Peyton Jones.

    Summary quit editing summary
    Prosedit prosquit editing pros
    • Few dependencies (and all of them ship with GHC)
      move trait up move trait down edit trait delete trait

    press Ctrl+Enter or Enter to addmarkdown supportededit off
    Consedit consquit editing cons
    • Uses String instead of text which causes poor performance when formatting a large amount of output
      move trait up move trait down edit trait delete trait

    press Ctrl+Enter or Enter to addmarkdown supportededit off
    Ecosystemedit ecosystem
    Ecosystemquit editing ecosystemor press Ctrl+Enter to savemarkdown supported
    Notes
    collapse notesedit notes

    <notes are empty>

    add something!

    #
    boxes (Hackage)
    box model
    move item up move item down edit item info delete item
    Summary edit summary

    Lays out text in two dimensions, using a simple box model.

    Example

    print_table :: [[String]] -> IO ()
    print_table rows = printBox $ hsep 2 left (map (vcat left . map text) (transpose rows))
    Mercoxit     335.67
    Arkonor      276.15
    Bistot       214.30

    Source

    Summary quit editing summary
    Prosedit prosquit editing pros
    • Few dependencies (base and split)
      move trait up move trait down edit trait delete trait
    • Simple model
      move trait up move trait down edit trait delete trait

    press Ctrl+Enter or Enter to addmarkdown supportededit off
    Consedit consquit editing cons
    • Limited formatting configuration
      move trait up move trait down edit trait delete trait
    • No examples/tutorial
      move trait up move trait down edit trait delete trait

    press Ctrl+Enter or Enter to addmarkdown supportededit off
    Ecosystemedit ecosystem
    Ecosystemquit editing ecosystemor press Ctrl+Enter to savemarkdown supported
    Notes
    collapse notesedit notes

    <notes are empty>

    add something!