text-display: A typeclass for user-facing output

[ library, mit, text ] [ Propose Tags ]

The Display typeclass provides a solution for user-facing output that does not have to abide by the rules of the Show typeclass.


[Skip to Readme]

Modules

[Index] [Quick Jump]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.0.1.0, 0.0.2.0, 0.0.3.0, 0.0.4.0, 0.0.5.0, 0.0.5.1
Change log CHANGELOG.md
Dependencies base (>=4.12 && <=5), bytestring (>=0.10), text (>=1.2 && <1.3) [details]
License MIT
Author Hécate Moonlight
Maintainer Hécate Moonlight
Revised Revision 1 made by hecate at 2021-11-20T17:47:38Z
Category Text
Home page https://github.com/haskell-text/text-display#readme
Bug tracker https://github.com/haskell-text/text-display/issues
Source repo head: git clone https://github.com/haskell-text/text-display
Uploaded by hecate at 2021-11-02T19:41:35Z
Distributions NixOS:0.0.5.1
Reverse Dependencies 5 direct, 0 indirect [details]
Downloads 912 total (35 in the last 30 days)
Rating 2.25 (votes: 2) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for text-display-0.0.1.0

[back to package description]

CI badge made with Haskell

Buy Me a Coffee at ko-fi.com

A Typeclass for user-facing output

Description

The text-display library offers a way for developers to print a textual representation of datatypes that does not have to abide by the rules of the Show typeclass.

If you wish to learn more about how things are done and why, please read the DESIGN.md file.

Examples

There are two methods to implement Display for your type:

The first one is a manual implementation:

data ManualType = MT Int

-- >>> display (MT 32)
-- "MT 32"
instance Display ManualType where
  displayPrec prec (MT i) = displayParen (prec > 10) $ "MT " <> displayPrec 11 i

But this can be quite time-consuming, especially if your datatype already has an existing Show that you wish to reuse. In which case, you can piggy-back on this instance like this:

{-# LANGUAGE DerivingVia #-}
data AutomaticallyDerived = AD
  -- We derive 'Show'
  deriving Show 
  -- We take advantage of the 'Show' instance to derive 'Display' from it
  deriving Display
    via (ShowInstance AutomaticallyDerived) 

But let's say you want to redact an instance of Display? You can do it locally, through the OpaqueInstance helper. It is most useful to hide tokens or passwords:

data UserToken = UserToken UUID                           
 deriving Display                                         
   via (OpaqueInstance "[REDACTED]" UserToken)            
                                                          
display $ UserToken "7a01d2ce-31ff-11ec-8c10-5405db82c3cd"
-- => "[REDACTED]"