{-# LANGUAGE UndecidableInstances #-}
module Shrun.Data.Text
( UnlinedText (..),
fromText,
fromTextReplace,
unsafeUnlinedText,
toText,
concat,
intercalate,
reallyUnsafeLiftUnlined,
)
where
import Data.String (IsString (fromString))
import Data.Text qualified as T
import Shrun.Prelude
newtype UnlinedText = UnsafeUnlinedText {UnlinedText -> Text
unUnlinedText :: Text}
deriving stock (UnlinedText -> UnlinedText -> Bool
(UnlinedText -> UnlinedText -> Bool)
-> (UnlinedText -> UnlinedText -> Bool) -> Eq UnlinedText
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UnlinedText -> UnlinedText -> Bool
== :: UnlinedText -> UnlinedText -> Bool
$c/= :: UnlinedText -> UnlinedText -> Bool
/= :: UnlinedText -> UnlinedText -> Bool
Eq, Int -> UnlinedText -> ShowS
[UnlinedText] -> ShowS
UnlinedText -> String
(Int -> UnlinedText -> ShowS)
-> (UnlinedText -> String)
-> ([UnlinedText] -> ShowS)
-> Show UnlinedText
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UnlinedText -> ShowS
showsPrec :: Int -> UnlinedText -> ShowS
$cshow :: UnlinedText -> String
show :: UnlinedText -> String
$cshowList :: [UnlinedText] -> ShowS
showList :: [UnlinedText] -> ShowS
Show)
deriving (Semigroup UnlinedText
UnlinedText
Semigroup UnlinedText =>
UnlinedText
-> (UnlinedText -> UnlinedText -> UnlinedText)
-> ([UnlinedText] -> UnlinedText)
-> Monoid UnlinedText
[UnlinedText] -> UnlinedText
UnlinedText -> UnlinedText -> UnlinedText
forall a.
Semigroup a =>
a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
$cmempty :: UnlinedText
mempty :: UnlinedText
$cmappend :: UnlinedText -> UnlinedText -> UnlinedText
mappend :: UnlinedText -> UnlinedText -> UnlinedText
$cmconcat :: [UnlinedText] -> UnlinedText
mconcat :: [UnlinedText] -> UnlinedText
Monoid, NonEmpty UnlinedText -> UnlinedText
UnlinedText -> UnlinedText -> UnlinedText
(UnlinedText -> UnlinedText -> UnlinedText)
-> (NonEmpty UnlinedText -> UnlinedText)
-> (forall b. Integral b => b -> UnlinedText -> UnlinedText)
-> Semigroup UnlinedText
forall b. Integral b => b -> UnlinedText -> UnlinedText
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
$c<> :: UnlinedText -> UnlinedText -> UnlinedText
<> :: UnlinedText -> UnlinedText -> UnlinedText
$csconcat :: NonEmpty UnlinedText -> UnlinedText
sconcat :: NonEmpty UnlinedText -> UnlinedText
$cstimes :: forall b. Integral b => b -> UnlinedText -> UnlinedText
stimes :: forall b. Integral b => b -> UnlinedText -> UnlinedText
Semigroup) via Text
instance IsString UnlinedText where
fromString :: String -> UnlinedText
fromString = Text -> UnlinedText
fromTextReplace (Text -> UnlinedText) -> (String -> Text) -> String -> UnlinedText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
pack
instance
( k ~ A_Getter,
a ~ Text,
b ~ Text
) =>
LabelOptic "unUnlinedText" k UnlinedText UnlinedText a b
where
labelOptic :: Optic k NoIx UnlinedText UnlinedText a b
labelOptic = (UnlinedText -> a) -> Getter UnlinedText a
forall s a. (s -> a) -> Getter s a
to (\(UnsafeUnlinedText Text
ts) -> a
Text
ts)
{-# INLINE labelOptic #-}
fromText :: Text -> List UnlinedText
fromText :: Text -> [UnlinedText]
fromText = (Text -> UnlinedText) -> [Text] -> [UnlinedText]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> UnlinedText
UnsafeUnlinedText ([Text] -> [UnlinedText])
-> (Text -> [Text]) -> Text -> [UnlinedText]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
T.lines
fromTextReplace :: Text -> UnlinedText
fromTextReplace :: Text -> UnlinedText
fromTextReplace = Text -> UnlinedText
UnsafeUnlinedText (Text -> UnlinedText) -> (Text -> Text) -> Text -> UnlinedText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"\n" Text
" "
unsafeUnlinedText :: (HasCallStack) => Text -> UnlinedText
unsafeUnlinedText :: HasCallStack => Text -> UnlinedText
unsafeUnlinedText Text
txt =
if Char
'\n' Char -> Text -> Bool
`T.elem` Text
txt
then String -> UnlinedText
forall a. HasCallStack => String -> a
error (String -> UnlinedText) -> String -> UnlinedText
forall a b. (a -> b) -> a -> b
$ String
"Unwanted newline in text: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
unpack Text
txt
else Text -> UnlinedText
UnsafeUnlinedText Text
txt
toText :: List UnlinedText -> Text
toText :: [UnlinedText] -> Text
toText = Text -> [Text] -> Text
T.intercalate Text
" " ([Text] -> Text)
-> ([UnlinedText] -> [Text]) -> [UnlinedText] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (UnlinedText -> Text) -> [UnlinedText] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (Optic' A_Getter NoIx UnlinedText Text -> UnlinedText -> Text
forall k (is :: IxList) s a.
Is k A_Getter =>
Optic' k is s a -> s -> a
view Optic' A_Getter NoIx UnlinedText Text
#unUnlinedText)
concat :: List UnlinedText -> UnlinedText
concat :: [UnlinedText] -> UnlinedText
concat = Text -> UnlinedText
UnsafeUnlinedText (Text -> UnlinedText)
-> ([UnlinedText] -> Text) -> [UnlinedText] -> UnlinedText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [UnlinedText] -> Text
toText
intercalate :: UnlinedText -> List UnlinedText -> UnlinedText
intercalate :: UnlinedText -> [UnlinedText] -> UnlinedText
intercalate (UnsafeUnlinedText Text
d) =
Text -> UnlinedText
UnsafeUnlinedText
(Text -> UnlinedText)
-> ([UnlinedText] -> Text) -> [UnlinedText] -> UnlinedText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text] -> Text
T.intercalate Text
d
([Text] -> Text)
-> ([UnlinedText] -> [Text]) -> [UnlinedText] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (UnlinedText -> Text) -> [UnlinedText] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (Optic' A_Getter NoIx UnlinedText Text -> UnlinedText -> Text
forall k (is :: IxList) s a.
Is k A_Getter =>
Optic' k is s a -> s -> a
view Optic' A_Getter NoIx UnlinedText Text
#unUnlinedText)
reallyUnsafeLiftUnlined :: (Text -> Text) -> UnlinedText -> UnlinedText
reallyUnsafeLiftUnlined :: (Text -> Text) -> UnlinedText -> UnlinedText
reallyUnsafeLiftUnlined Text -> Text
f (UnsafeUnlinedText Text
t) = Text -> UnlinedText
UnsafeUnlinedText (Text -> Text
f Text
t)