Skip to content

Commit

Permalink
Allow print and hprint to operate in any MonadIO
Browse files Browse the repository at this point in the history
  • Loading branch information
bos committed Jun 9, 2011
1 parent 923c2de commit fbc3b7f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
15 changes: 9 additions & 6 deletions Data/Text/Format.hs
Expand Up @@ -36,15 +36,16 @@ module Data.Text.Format
, fixed_
) where

import qualified Data.Text.Buildable as B
import Data.Text.Format.Params (Params(..))
import Control.Monad.IO.Class (MonadIO(liftIO))
import Data.Text.Format.Functions ((<>))
import Data.Text.Format.Params (Params(..))
import Data.Text.Format.Types.Internal (FPControl(..), FPFormat(..), Fast(..))
import Data.Text.Format.Types.Internal (Format(..), Hex(..), Only(..), Shown(..))
import Data.Text.Lazy.Builder
import Prelude hiding (exp, print)
import System.IO (Handle)
import qualified Data.Text as ST
import qualified Data.Text.Buildable as B
import qualified Data.Text.Lazy as LT
import qualified Data.Text.Lazy.IO as LT

Expand All @@ -63,13 +64,15 @@ format :: Params ps => Format -> ps -> LT.Text
format fmt ps = toLazyText $ build fmt ps

-- | Render a format string and arguments, then print the result.
print :: Params ps => Format -> ps -> IO ()
print fmt ps = LT.putStr . toLazyText $ build fmt ps
print :: (MonadIO m, Params ps) => Format -> ps -> m ()
{-# SPECIALIZE print :: (Params ps) => Format -> ps -> IO () #-}
print fmt ps = liftIO . LT.putStr . toLazyText $ build fmt ps

-- | Render a format string and arguments, then print the result to
-- the given file handle.
hprint :: Params ps => Handle -> Format -> ps -> IO ()
hprint h fmt ps = LT.hPutStr h . toLazyText $ build fmt ps
hprint :: (MonadIO m, Params ps) => Handle -> Format -> ps -> m ()
{-# SPECIALIZE hprint :: (Params ps) => Handle -> Format -> ps -> IO () #-}
hprint h fmt ps = liftIO . LT.hPutStr h . toLazyText $ build fmt ps

-- | Pad the left hand side of a string until it reaches @k@
-- characters wide, if necessary filling with character @c@.
Expand Down
3 changes: 2 additions & 1 deletion text-format.cabal
Expand Up @@ -47,7 +47,8 @@ library
integer-gmp,
old-locale,
text >= 0.11.0.8,
time
time,
transformers

if flag(developer)
ghc-options: -Werror
Expand Down

0 comments on commit fbc3b7f

Please sign in to comment.