How can I handle multiple lines of output when running external processes with Shelly?

65 Views Asked by At

I'm looking for a function of type Text -> [Text], essentially that would split text based by newlines. However I can't just split on \n as this could be escaped for example as in \\n, I'm not sure what other complications exist.

Main.hs

{-# LANGUAGE OverloadedStrings #-}

import Data.Text (Text)
import Data.Text.IO
import Shelly

example :: Sh Text
example = run "./example.sh" []

main :: IO ()
main = do
  v <- shelly example
  Data.Text.IO.putStrLn v

example.sh

echo "test1\\n"
echo "test2"
echo "test3"

Output of ./example.sh:

test1\n
test2
test3

So this is output of 3 lines.

Does Shelly support some functionality to handle the above? Or maybe some other Haskell library to parse the output? I guess it would look something like putStrLn but instead of being output to IO () it would be output to [Text]?

1

There are 1 best solutions below

0
On BEST ANSWER

https://hackage.haskell.org/package/text-1.2.2.2/docs/Data-Text.html#v:lines

main :: IO ()
main = do
  v <- shelly example
  print $ Data.Text.lines v