pretty-display: Typeclass for human-readable display

[ bsd3, library, program, text ] [ Propose Tags ]

Typeclass for human-readable display. Provides tools for working interactively within ghci.


[Skip to Readme]

Modules

[Index]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0, 0.1.1, 0.1.2, 0.1.3, 0.1.4, 0.1.5, 0.1.6, 0.1.7, 0.1.8, 0.1.9, 0.1.10
Dependencies ansi-wl-pprint, base (>=4.7 && <5), pretty-display, pretty-show, text [details]
License BSD-3-Clause
Copyright Copyright (c) 2014-2016 Justin Sermeno
Author Justin Sermeno
Maintainer Justin Sermeno
Category Text
Home page https://github.com/githubuser/pretty-display
Source repo head: git clone https://github.com/githubuser/pretty-display
Uploaded by jsermeno at 2016-11-17T20:02:15Z
Distributions NixOS:0.1.10
Reverse Dependencies 1 direct, 1 indirect [details]
Executables pretty-display-example
Downloads 7039 total (29 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2016-11-17 [all 1 reports]

Readme for pretty-display-0.1.5

[back to package description]

pretty-display: typeclass for human-readable display

Build Status Haskell Programming Language BSD3 License

In Haskell the Show typeclass is used to display a syntactically correct Haskell expression. However, there are times when you want to provide a richer display for a value while still retaining the benefits of having derived Show instances. This can be especially useful when working interactively in ghci. pretty-display provides a tiny registered package with the Display typeclass for just this purpose.

GHCi usage

To use Display instances as the default in ghci create a .ghci file with the following:

import Text.Display

:set -interactive-print=Text.Display.dPrint
:def pp (\_ -> return ":set -interactive-print=Text.Display.dPrint")
:def npp (\_ -> return ":set -interactive-print=print")
ghci example

Typeclass usage

By default, all instances of Show are also instances of Display. To create a custom instance you will need to use the OVERLAPPING pragma, and define the display method of type a -> DisplayText. For example:

import Numeric

import Text.Display
import Text.PrettyPrint.ANSI.Leijen as PP

data MyRecord = MyRecord
  { numerators :: [Double]
  , denominator :: Double
  } deriving (Show)

record :: MyRecord
record = MyRecord
  { numerators = [0, 5..100]
  , denominator = 1326
  }

instance {-# OVERLAPPING #-} Display MyRecord where
  display a = mkDisplayTextFromStr
      $ show
      $ toCol "MyRecord (percentage): " <> toCol (displayPerc a)
    where
      displayPerc a = showFFloat (Just 2) (toPerc a) "%"
      toPerc a = sum (numerators a) / denominator a * 100
      toCol a = (PP.dullgreen . PP.fill 25 . PP.text) a

main :: IO ()
main = do
  dPrint record
  pPrint record

Using Show when defaulting to Display

If you've set ghci to use dPrint by default you can still print Show instances for debugging. For normal printing use print. For convenience pretty-display re-exports pPrint from the pretty-show package for pretty printing Show instances.