{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module RIO.Prelude.Display
( Utf8Builder (..)
, Display (..)
, displayShow
, utf8BuilderToText
, utf8BuilderToLazyText
, displayBytesUtf8
, writeFileUtf8Builder
) where
import Data.String (IsString (..))
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Builder as BB
import Data.ByteString.Builder (Builder)
import Data.Semigroup (Semigroup(..))
import Data.Text (Text)
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL
import UnliftIO
import Data.Text.Encoding (decodeUtf8With, encodeUtf8Builder)
import Data.Text.Encoding.Error (lenientDecode)
import Data.Int
import Data.Word
import System.Process.Typed (ProcessConfig, setEnvInherit)
newtype Utf8Builder = Utf8Builder { getUtf8Builder :: Builder }
deriving (Semigroup)
instance Monoid Utf8Builder where
mempty = Utf8Builder mempty
{-# INLINE mempty #-}
mappend = (Data.Semigroup.<>)
{-# INLINE mappend #-}
mconcat = foldr mappend mempty
{-# INLINE mconcat #-}
instance IsString Utf8Builder where
fromString = Utf8Builder . BB.stringUtf8
class Display a where
{-# MINIMAL display | textDisplay #-}
display :: a -> Utf8Builder
display = display . textDisplay
textDisplay :: a -> Text
textDisplay = utf8BuilderToText . display
instance Display Utf8Builder where
display = id
instance Display Text where
display = Utf8Builder . encodeUtf8Builder
instance Display TL.Text where
display = foldMap display . TL.toChunks
instance Display Char where
display = Utf8Builder . BB.charUtf8
instance Display Integer where
display = Utf8Builder . BB.integerDec
instance Display Float where
display = Utf8Builder . BB.floatDec
instance Display Double where
display = Utf8Builder . BB.doubleDec
instance Display Int where
display = Utf8Builder . BB.intDec
instance Display Int8 where
display = Utf8Builder . BB.int8Dec
instance Display Int16 where
display = Utf8Builder . BB.int16Dec
instance Display Int32 where
display = Utf8Builder . BB.int32Dec
instance Display Int64 where
display = Utf8Builder . BB.int64Dec
instance Display Word where
display = Utf8Builder . BB.wordDec
instance Display Word8 where
display = Utf8Builder . BB.word8Dec
instance Display Word16 where
display = Utf8Builder . BB.word16Dec
instance Display Word32 where
display = Utf8Builder . BB.word32Dec
instance Display Word64 where
display = Utf8Builder . BB.word64Dec
instance Display SomeException where
display = fromString . displayException
instance Display IOException where
display = fromString . displayException
instance Display (ProcessConfig a b c) where
display = displayShow . setEnvInherit
displayShow :: Show a => a -> Utf8Builder
displayShow = fromString . show
displayBytesUtf8 :: ByteString -> Utf8Builder
displayBytesUtf8 = Utf8Builder . BB.byteString
utf8BuilderToText :: Utf8Builder -> Text
utf8BuilderToText =
decodeUtf8With lenientDecode . BL.toStrict . BB.toLazyByteString . getUtf8Builder
utf8BuilderToLazyText :: Utf8Builder -> TL.Text
utf8BuilderToLazyText =
TL.decodeUtf8With lenientDecode . BB.toLazyByteString . getUtf8Builder
writeFileUtf8Builder :: MonadIO m => FilePath -> Utf8Builder -> m ()
writeFileUtf8Builder fp (Utf8Builder builder) =
liftIO $ withBinaryFile fp WriteMode $ \h -> BB.hPutBuilder h builder