AJChapman / formatting

Format strings type-safely with combinators
BSD 3-Clause "New" or "Revised" License
164 stars 39 forks source link

Added ‘toText’ (tt) formatter for Showable instances #2

Closed Icelandjack closed 10 years ago

Icelandjack commented 10 years ago

Solves a very common issue of formatting values that can be shown:

ghci> import Text.Printf
ghci> newtype Issue = Issue Int deriving Show
ghci> printf "%s is valid: %s\n" (show (Issue 17)) (show True)
Issue 17 is valid: True

Using this you can write:

ghci> fprint (toText % " is valid: " % toText % "\n") (Issue 17) True
Issue 17 is valid: True
ghci> fprint (tt % " is valid: " % tt % "\n") (Issue 17) True
Issue 17 is valid: True

I chose the name toText since show is obviously taken, I thought about using repr for the long formatter as Python does but r is already taken as a short formatter. Alternatives welcome. It is included in text-format which chooses the name shown for the formatter which is also valid but a short form is not as obvious.

chrisdone commented 10 years ago

How about the name shown?

chrisdone commented 10 years ago

Oh, you already wrote that. Yes, I like shown. =)

Icelandjack commented 10 years ago

shown sounds good, what about a short formatter?

chrisdone commented 10 years ago

sh?

Icelandjack commented 10 years ago

sh it is, I've updated the patch.

chrisdone commented 10 years ago

Rebased, squashed and merged!