@hackage pretty-display0.1.5
Typeclass for human-readable display
Categories
License
BSD-3-Clause
Maintainer
Justin Sermeno
Links
Versions
Installation
Dependencies (3)
- base >=4.7 && <5
- pretty-show
- text Show all…
Dependents (1)
@hackage/amby
pretty-display: typeclass for human-readable display
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")
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.