module Formatting.Formatters
(
text,
stext,
string,
builder,
int,
float,
expt,
fixed,
prec,
shortest,
commas,
left,
right,
hex,
build,
Buildable
) where
import Formatting.Holey
import Data.Monoid
import qualified Data.Text as S
import qualified Data.Text as T
import Data.Text.Buildable (Buildable)
import qualified Data.Text.Buildable as B (build)
import qualified Data.Text.Format as T
import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy as LT
import Data.Text.Lazy.Builder (Builder)
import qualified Data.Text.Lazy.Builder as T
text :: Format Text
text = later T.fromLazyText
hex :: Integral a => Format a
hex = later T.hex
stext :: Format S.Text
stext = later T.fromText
string :: Format String
string = later (T.fromText . T.pack)
builder :: Format Builder
builder = later id
build :: Buildable a => Format a
build = later B.build
int :: Integral a => Format a
int = later T.shortest
float :: Real a => Format a
float = later (T.shortest)
expt :: Real a => Int -> Format a
expt i = later (T.expt i)
fixed :: Real a => Int -> Format a
fixed i = later (T.fixed i)
prec :: Real a => Int -> Format a
prec i = later (T.prec i)
shortest :: Real a => Format a
shortest = later T.shortest
left :: Buildable a => Int -> Char -> Format a
left i c = later (T.left i c)
right :: Buildable a => Int -> Char -> Format a
right i c = later (T.right i c)
commas :: (Buildable n,Integral n) => Format n
commas = later (commaize) where
commaize = T.fromLazyText .
LT.reverse .
foldr merge "" .
LT.zip ("000" <> cycle' ",00") .
LT.reverse .
T.toLazyText .
B.build
merge (f,c) rest | f == ',' = "," <> LT.singleton c <> rest
| otherwise = LT.singleton c <> rest
cycle' xs = xs <> cycle' xs