{-# LANGUAGE CPP, MultiParamTypeClasses #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module System.Process.Chars where

#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>))
#endif
import Control.DeepSeq (force)
import qualified Control.Exception as C (evaluate)
import Data.ListLike.IO (hGetContents)
import Data.Text.Lazy (fromStrict, toChunks)
import Data.ListLike.Chars (Chars(..))
import Prelude hiding (null)
import System.Process
import System.Process.Common
import System.Exit (ExitCode)

-- | Like 'System.Process.readProcessWithExitCode', but specialized for 'Text'
instance ListLikeProcessIO Chars Char where
    forceOutput :: Chars -> IO Chars
forceOutput = Chars -> IO Chars
forall a. a -> IO a
C.evaluate (Chars -> IO Chars) -> (Chars -> Chars) -> Chars -> IO Chars
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Chars -> Chars
forall a. NFData a => a -> a
force
    readChunks :: Handle -> IO [Chars]
readChunks Handle
h = ((Text -> Chars) -> [Text] -> [Chars]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Chars
T (Text -> Chars) -> (Text -> Text) -> Text -> Chars
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
fromStrict) ([Text] -> [Chars]) -> (Text -> [Text]) -> Text -> [Chars]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
toChunks) (Text -> [Chars]) -> IO Text -> IO [Chars]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Handle -> IO Text
forall full item. ListLikeIO full item => Handle -> IO full
hGetContents Handle
h

-- | Specialized version for backwards compatibility.
readProcessWithExitCode
    :: FilePath                        -- ^ command to run
    -> [String]                        -- ^ any arguments
    -> Chars                         -- ^ standard input
    -> IO (ExitCode, Chars, Chars) -- ^ exitcode, stdout, stderr
readProcessWithExitCode :: FilePath -> [FilePath] -> Chars -> IO (ExitCode, Chars, Chars)
readProcessWithExitCode = FilePath -> [FilePath] -> Chars -> IO (ExitCode, Chars, Chars)
forall text char.
ListLikeProcessIO text char =>
FilePath -> [FilePath] -> text -> IO (ExitCode, text, text)
System.Process.Common.readProcessWithExitCode

readCreateProcessWithExitCode
    :: CreateProcess                   -- ^ command and arguments to run
    -> Chars                         -- ^ standard input
    -> IO (ExitCode, Chars, Chars) -- ^ exitcode, stdout, stderr
readCreateProcessWithExitCode :: CreateProcess -> Chars -> IO (ExitCode, Chars, Chars)
readCreateProcessWithExitCode = CreateProcess -> Chars -> IO (ExitCode, Chars, Chars)
forall maker text char.
(ProcessMaker maker, ListLikeProcessIO text char) =>
maker -> text -> IO (ExitCode, text, text)
System.Process.Common.readCreateProcessWithExitCode