{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
module Turtle.Format (
Format (..)
, (%)
, format
, printf
, eprintf
, makeFormat
, w
, d
, u
, o
, x
, f
, e
, g
, s
, l
, fp
, utc
, repr
) where
import Control.Category (Category(..))
import Control.Monad.IO.Class (MonadIO(..))
import Data.Monoid ((<>))
import Data.String (IsString(..))
import Data.Text (Text, pack)
import Data.Time (UTCTime)
import Data.Word
import Numeric (showEFloat, showFFloat, showGFloat, showHex, showOct)
import Prelude hiding ((.), id)
import qualified System.IO as IO
import Turtle.Line (Line)
import qualified Data.Text.IO as Text
import qualified Turtle.Line
newtype Format a b = Format { forall a b. Format a b -> (Text -> a) -> b
(>>-) :: (Text -> a) -> b }
instance Category Format where
id :: forall a. Format a a
id = forall a b. ((Text -> a) -> b) -> Format a b
Format (\Text -> a
return_ -> Text -> a
return_ Text
"")
Format b c
fmt1 . :: forall b c a. Format b c -> Format a b -> Format a c
. Format a b
fmt2 = forall a b. ((Text -> a) -> b) -> Format a b
Format (\Text -> a
return_ ->
Format b c
fmt1 forall a b. Format a b -> (Text -> a) -> b
>>- \Text
str1 ->
Format a b
fmt2 forall a b. Format a b -> (Text -> a) -> b
>>- \Text
str2 ->
Text -> a
return_ (Text
str1 forall a. Semigroup a => a -> a -> a
<> Text
str2) )
(%) :: Format b c -> Format a b -> Format a c
% :: forall b c a. Format b c -> Format a b -> Format a c
(%) = forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
(.)
instance (a ~ b) => IsString (Format a b) where
fromString :: String -> Format a b
fromString String
str = forall a b. ((Text -> a) -> b) -> Format a b
Format (\Text -> a
return_ -> Text -> a
return_ (String -> Text
pack String
str))
format :: Format Text r -> r
format :: forall r. Format Text r -> r
format Format Text r
fmt = Format Text r
fmt forall a b. Format a b -> (Text -> a) -> b
>>- forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
printf :: MonadIO io => Format (io ()) r -> r
printf :: forall (io :: * -> *) r. MonadIO io => Format (io ()) r -> r
printf Format (io ()) r
fmt = Format (io ()) r
fmt forall a b. Format a b -> (Text -> a) -> b
>>- (forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> IO ()
Text.putStr)
eprintf :: MonadIO io => Format (io ()) r -> r
eprintf :: forall (io :: * -> *) r. MonadIO io => Format (io ()) r -> r
eprintf Format (io ()) r
fmt = Format (io ()) r
fmt forall a b. Format a b -> (Text -> a) -> b
>>- (forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Handle -> Text -> IO ()
Text.hPutStr Handle
IO.stderr)
makeFormat :: (a -> Text) -> Format r (a -> r)
makeFormat :: forall a r. (a -> Text) -> Format r (a -> r)
makeFormat a -> Text
k = forall a b. ((Text -> a) -> b) -> Format a b
Format (\Text -> r
return_ -> \a
a -> Text -> r
return_ (a -> Text
k a
a))
w :: Show a => Format r (a -> r)
w :: forall a r. Show a => Format r (a -> r)
w = forall a r. (a -> Text) -> Format r (a -> r)
makeFormat (String -> Text
pack forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. Show a => a -> String
show)
d :: Integral n => Format r (n -> r)
d :: forall n r. Integral n => Format r (n -> r)
d = forall a r. (a -> Text) -> Format r (a -> r)
makeFormat (String -> Text
pack forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. Show a => a -> String
show forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. Integral a => a -> Integer
toInteger)
u :: Format r (Word -> r)
u :: forall r. Format r (Word -> r)
u = forall a r. Show a => Format r (a -> r)
w
o :: Format r (Word -> r)
o :: forall r. Format r (Word -> r)
o = forall a r. (a -> Text) -> Format r (a -> r)
makeFormat (\Word
n -> String -> Text
pack (forall a. (Integral a, Show a) => a -> ShowS
showOct Word
n String
""))
x :: Format r (Word -> r)
x :: forall r. Format r (Word -> r)
x = forall a r. (a -> Text) -> Format r (a -> r)
makeFormat (\Word
n -> String -> Text
pack (forall a. (Integral a, Show a) => a -> ShowS
showHex Word
n String
""))
f :: Format r (Double -> r)
f :: forall r. Format r (Double -> r)
f = forall a r. (a -> Text) -> Format r (a -> r)
makeFormat (\Double
n -> String -> Text
pack (forall a. RealFloat a => Maybe Int -> a -> ShowS
showFFloat (forall a. a -> Maybe a
Just Int
6) Double
n String
""))
e :: Format r (Double -> r)
e :: forall r. Format r (Double -> r)
e = forall a r. (a -> Text) -> Format r (a -> r)
makeFormat (\Double
n -> String -> Text
pack (forall a. RealFloat a => Maybe Int -> a -> ShowS
showEFloat (forall a. a -> Maybe a
Just Int
6) Double
n String
""))
g :: Format r (Double -> r)
g :: forall r. Format r (Double -> r)
g = forall a r. (a -> Text) -> Format r (a -> r)
makeFormat (\Double
n -> String -> Text
pack (forall a. RealFloat a => Maybe Int -> a -> ShowS
showGFloat (forall a. a -> Maybe a
Just Int
6) Double
n String
""))
s :: Format r (Text -> r)
s :: forall r. Format r (Text -> r)
s = forall a r. (a -> Text) -> Format r (a -> r)
makeFormat forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
l :: Format r (Line -> r)
l :: forall r. Format r (Line -> r)
l = forall a r. (a -> Text) -> Format r (a -> r)
makeFormat Line -> Text
Turtle.Line.lineToText
fp :: Format r (FilePath -> r)
fp :: forall r. Format r (String -> r)
fp = forall a r. (a -> Text) -> Format r (a -> r)
makeFormat String -> Text
pack
utc :: Format r (UTCTime -> r)
utc :: forall r. Format r (UTCTime -> r)
utc = forall a r. Show a => Format r (a -> r)
w
repr :: (Show a, IsString text) => a -> text
repr :: forall a text. (Show a, IsString text) => a -> text
repr = forall a. IsString a => String -> a
fromString forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. Show a => a -> String
show