process-streaming-0.9.2: Streaming interface to system processes.

Safe HaskellNone
LanguageHaskell2010

System.Process.Streaming.Text

Contents

Description

This module re-exports the entirety of Pipes.Transduce.Text from the pipes-transduce package. It provides functions useful for treating the stdout and stderr streams as text.

It is better to import it qualified:

>>> import qualified System.Process.Streaming.Text as PT

Synopsis

Examples

To process a standard stream as utf8 text:

>>> execute (piped (shell "echo foo")) (foldOut (PT.asUtf8x PT.intoLazyText))
"foo\n"   

asUtf8x throws exceptions on decodign errors. asUtf8 uses sum types instead. We must provide an error mapping function, here we simply use id:

>>> executeFallibly (piped (shell "echo foo")) (foldOut (PT.asUtf8 id PT.intoLazyText))
Right "foo\n"   

asFoldedLines lets you identify and consume the lines that appear in a stream as lazy Texts.

>>> :{
      execute (piped (shell "{ echo foo ; echo bar ; }")) $
          (foldOut (PT.asUtf8x (PT.asFoldedLines intoList)))
    :}
["foo","bar"]

Sometimes we want to consume the lines in stdout and stderr as a single text stream. We can do this with foldOutErr and combinedLines.

We also need bothAsUtf8x to decode both streams.

>>> :{
      execute (piped (shell "{ echo ooo ; sleep 1 ; echo eee 1>&2 ; }")) $ 
          (foldOutErr (PT.bothAsUtf8x (PT.combinedLines PT.intoLazyText)))
    :}
"ooo\neee\n"

We can also tag each line with its provenance, using combinedLinesPrefixing:

>>> :{
      execute (piped (shell "{ echo ooo ; sleep 1 ; echo eee 1>&2 ; }")) $
          (foldOutErr (PT.bothAsUtf8x (PT.combinedLinesPrefixing "+" "-" PT.intoLazyText)))
    :}
"+ooo\n-eee\n"