| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
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
- module Pipes.Transduce.Text
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"
Fail when an error message appears in the combined stdout and stderr stream. This uses the eachLine function. We switched to executeFallibly to enable either-like failures.
>>>:{executeFallibly (piped (shell "{ echo ooo ; sleep 1 ; echo _error_ 1>&2 ; }")) $ let check line = runExceptT $ when (Data.Text.Lazy.isInfixOf "error" line) $ throwE line in foldOutErr (PT.bothAsUtf8x (PT.combinedLines (PT.eachLine check))) :} Left "_error_"
module Pipes.Transduce.Text