{-# LANGUAGE OverloadedStrings, RelaxedPolyRec #-}
module Data.Text.Format
(
left
, right
, hex
, fixed
, shortest
) where
import Control.Monad.IO.Class (MonadIO(liftIO))
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Builder as L
import Data.Text (Text)
import qualified Data.Text as ST
import qualified Data.Text as T
import qualified Formatting.Buildable as B
import qualified Data.Text.Encoding as T
import Data.Text.Format.Functions ((<>))
import Data.Text.Format.Types (Shown(..), Hex(..))
import qualified Data.Text.Lazy as LT
import Data.Text.Lazy.Builder
import qualified Data.Text.Lazy.IO as LT
import Prelude hiding (exp, print)
import System.IO (Handle)
import Text.Printf
left :: B.Buildable a => Int -> Char -> a -> Builder
left k c =
fromLazyText . LT.justifyRight (fromIntegral k) c . toLazyText . B.build
right :: B.Buildable a => Int -> Char -> a -> Builder
right k c =
fromLazyText . LT.justifyLeft (fromIntegral k) c . toLazyText . B.build
fixed :: (Real a) =>
Int
-> a -> Builder
fixed decs = B.build . T.pack . (printf ("%." ++ show decs ++ "f") :: Double->String) . realToFrac
{-# NOINLINE[0] fixed #-}
shortest :: (Real a) => a -> Builder
shortest = B.build . T.decodeUtf8 . L.toStrict . L.toLazyByteString . L.doubleDec . realToFrac
{-# NOINLINE[0] shortest #-}
hex :: Integral a => a -> Builder
hex = B.build . Hex
{-# INLINE hex #-}