packages feed

txt-0.0.3.0: Data/Text/Lazy/IO.hs

module Data.Text.Lazy.IO where

import qualified Data.ByteString.Lazy as BS
import Data.Text.Lazy as T
import Data.Text.Lazy.Private
import System.IO (Handle, stdin, stdout)

put, putLn :: Text -> IO ()
put = hPut stdout
putLn = hPutLn stdout

getContents :: IO Text
getContents = hGetContents stdin

hGet :: Handle -> Int -> IO Text
hGet h n = Text <$> BS.hGet h n

hGetContents :: Handle -> IO Text
hGetContents h = Text <$> BS.hGetContents h

hPut, hPutLn :: Handle -> Text -> IO ()
hPut h = BS.hPut h . unText
hPutLn h t
  | T.length t < 0x400 = hPut h (t `snoc` '\n')
  | otherwise          = hPut h t *> hPut h "\n"

hPutNonBlocking :: Handle -> Text -> IO Text
hPutNonBlocking h = fmap Text . BS.hPutNonBlocking h . unText