module Blip.Pretty
( Pretty (..)
, prettyString
, prettyList
, prettyTuple
, showBits
)where
import Data.Word (Word64, Word32, Word16, Word8)
import Text.PrettyPrint
import Data.ByteString.Lazy (ByteString)
import Data.ByteString.Lazy.Char8 (unpack)
import Data.Bits (testBit, Bits, bitSize)
class Pretty a where
pretty :: a -> Doc
prettyList :: [Doc] -> Doc
prettyList = brackets . hcat . punctuate comma
prettyTuple :: [Doc] -> Doc
prettyTuple = parens . hcat . punctuate comma
prettyString :: Pretty a => a -> String
prettyString = render . pretty
instance Pretty a => Pretty (Maybe a) where
pretty Nothing = empty
pretty (Just x) = pretty x
instance Pretty Word8 where
pretty = integer . fromIntegral
instance Pretty Word16 where
pretty = integer . fromIntegral
instance Pretty Word32 where
pretty = integer . fromIntegral
instance Pretty Word64 where
pretty = integer . fromIntegral
instance Pretty Int where
pretty = int
instance Pretty Integer where
pretty = integer
instance Pretty Double where
pretty = double
instance (Pretty a, Pretty b) => Pretty (a, b) where
pretty (x, y) = text "(" <> pretty x <> text "," <+> pretty y <> text ")"
instance Pretty ByteString where
pretty = text . unpack
bits :: Bits a => a -> [Bool]
bits x = map (testBit x) [0 .. bitSize x 1]
showBits :: Bits a => a -> String
showBits = map toBit . bits
where
toBit True = '1'
toBit False = '0'