{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
module Headroom.Text
( detectNewLine
, showNewLine
, lines'
, unlines'
)
where
import Headroom.Types ( NewLine(..) )
import RIO
import RIO.Text ( isInfixOf )
import qualified RIO.Text as T
import qualified RIO.Text.Partial as TP
detectNewLine :: Text
-> Maybe NewLine
detectNewLine :: Text -> Maybe NewLine
detectNewLine text :: Text
text | NewLine -> Text
showNewLine NewLine
CRLF Text -> Text -> Bool
`isInfixOf` Text
text = NewLine -> Maybe NewLine
forall a. a -> Maybe a
Just NewLine
CRLF
| NewLine -> Text
showNewLine NewLine
CR Text -> Text -> Bool
`isInfixOf` Text
text = NewLine -> Maybe NewLine
forall a. a -> Maybe a
Just NewLine
CR
| NewLine -> Text
showNewLine NewLine
LF Text -> Text -> Bool
`isInfixOf` Text
text = NewLine -> Maybe NewLine
forall a. a -> Maybe a
Just NewLine
LF
| Bool
otherwise = Maybe NewLine
forall a. Maybe a
Nothing
showNewLine :: NewLine
-> Text
showNewLine :: NewLine -> Text
showNewLine = \case
CR -> "\r"
CRLF -> "\r\n"
LF -> "\n"
lines' :: Text
-> (NewLine, [Text])
lines' :: Text -> (NewLine, [Text])
lines' text :: Text
text = (NewLine
newLine, [Text]
chunks)
where
newLine :: NewLine
newLine = NewLine -> Maybe NewLine -> NewLine
forall a. a -> Maybe a -> a
fromMaybe NewLine
LF (Text -> Maybe NewLine
detectNewLine Text
text)
chunks :: [Text]
chunks = Text -> Text -> [Text]
TP.splitOn (NewLine -> Text
showNewLine NewLine
newLine) Text
text
unlines' :: NewLine -> [Text] -> Text
unlines' :: NewLine -> [Text] -> Text
unlines' newLine :: NewLine
newLine = Text -> [Text] -> Text
T.intercalate (Text -> [Text] -> Text) -> Text -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ NewLine -> Text
showNewLine NewLine
newLine