{-# 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 :: Int -> Char -> a -> Builder
left Int
k Char
c =
Text -> Builder
fromLazyText (Text -> Builder) -> (a -> Text) -> a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Char -> Text -> Text
LT.justifyRight (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
k) Char
c (Text -> Text) -> (a -> Text) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
toLazyText (Builder -> Text) -> (a -> Builder) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Builder
forall p. Buildable p => p -> Builder
B.build
right :: B.Buildable a => Int -> Char -> a -> Builder
right :: Int -> Char -> a -> Builder
right Int
k Char
c =
Text -> Builder
fromLazyText (Text -> Builder) -> (a -> Text) -> a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Char -> Text -> Text
LT.justifyLeft (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
k) Char
c (Text -> Text) -> (a -> Text) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
toLazyText (Builder -> Text) -> (a -> Builder) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Builder
forall p. Buildable p => p -> Builder
B.build
fixed :: (Real a) =>
Int
-> a -> Builder
fixed :: Int -> a -> Builder
fixed Int
decs = Text -> Builder
forall p. Buildable p => p -> Builder
B.build (Text -> Builder) -> (a -> Text) -> a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Text) -> (a -> String) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Double -> String
forall r. PrintfType r => String -> r
printf (String
"%." String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
decs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"f") :: Double->String) (Double -> String) -> (a -> Double) -> a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac
{-# NOINLINE[0] fixed #-}
shortest :: (Real a) => a -> Builder
shortest :: a -> Builder
shortest = Text -> Builder
forall p. Buildable p => p -> Builder
B.build (Text -> Builder) -> (a -> Text) -> a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
T.decodeUtf8 (ByteString -> Text) -> (a -> ByteString) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
L.toStrict (ByteString -> ByteString) -> (a -> ByteString) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
L.toLazyByteString (Builder -> ByteString) -> (a -> Builder) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Builder
L.doubleDec (Double -> Builder) -> (a -> Double) -> a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac
{-# NOINLINE[0] shortest #-}
hex :: Integral a => a -> Builder
hex :: a -> Builder
hex = Hex a -> Builder
forall p. Buildable p => p -> Builder
B.build (Hex a -> Builder) -> (a -> Hex a) -> a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Hex a
forall a. a -> Hex a
Hex
{-# INLINE hex #-}