Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added toLines. #355

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/Turtle/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE BangPatterns #-}

-- | This module provides a large suite of utilities that resemble Unix
-- utilities.
Expand Down Expand Up @@ -214,6 +215,7 @@ module Turtle.Prelude (

-- * Text
, cut
, toLines

-- * Subprocess management
, proc
Expand Down Expand Up @@ -300,7 +302,7 @@ import Control.Exception (Exception, bracket, bracket_, finally, mask, throwIO)
import Control.Foldl (Fold(..), genericLength, handles, list, premap)
import qualified Control.Foldl
import qualified Control.Foldl.Text
import Control.Monad (guard, liftM, msum, when, unless, (>=>), mfilter)
import Control.Monad (guard, liftM, msum, when, unless, (>=>), mfilter, join)
import Control.Monad.IO.Class (MonadIO(..))
import Control.Monad.Managed (MonadManaged(..), managed, managed_, runManaged)
#ifdef mingw32_HOST_OS
Expand Down Expand Up @@ -1894,6 +1896,29 @@ cut :: Pattern a -> Text -> [Text]
cut pattern txt = head (match (selfless chars `sepBy` pattern) txt)
-- This `head` should be safe ... in theory

-- | Safe Shell Text to Shell Line.
toLines :: Shell Text.Text -> Shell Line
toLines = fmap unsafeTextToLine . join . reduce (Fold step initial extract)
where
extract (!lineAcc, stream) = stream <|> return lineAcc
initial = (mempty, empty)
step acc "" = acc
step (!lineAcc, stream) x
| splitNum < 1 = (lineAcc, stream)
| splitNum == 1 = (lineAcc <> x, stream)
| splitNum > 1 = ( last split
, stream
<|> (select
. ((lineAcc <> head split) :)
. tail
. init
$ split
)
)
where
split = Text.splitOn "\n" x
splitNum = length split

-- | Get the current time
date :: MonadIO io => io UTCTime
date = liftIO getCurrentTime
Expand Down