module Mit.Builder
  ( empty,
    build,
    hcat,
    newline,
    put,
    putln,
    space,
    squoted,
    vcat,
  )
where

import Data.List qualified as List
import Data.Text.IO qualified as Text
import Data.Text.Lazy qualified as Text.Lazy
import Data.Text.Lazy.Builder
import Mit.Prelude

empty :: Builder
empty :: Builder
empty =
  forall a. Monoid a => a
mempty

build :: Builder -> Text
build :: Builder -> Text
build =
  Text -> Text
Text.Lazy.toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
toLazyText

hcat :: Foldable f => f Builder -> Builder
hcat :: forall (f :: * -> *). Foldable f => f Builder -> Builder
hcat =
  forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> [a] -> [a]
List.intersperse Builder
space forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
toList

newline :: Builder
newline :: Builder
newline =
  Char -> Builder
singleton Char
'\n'

put :: Builder -> IO ()
put :: Builder -> IO ()
put =
  Text -> IO ()
Text.putStr forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
build

putln :: Builder -> IO ()
putln :: Builder -> IO ()
putln =
  Text -> IO ()
Text.putStrLn forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
build

space :: Builder
space :: Builder
space =
  Char -> Builder
singleton Char
' '

squote :: Builder
squote :: Builder
squote =
  Char -> Builder
singleton Char
'\''

squoted :: Builder -> Builder
squoted :: Builder -> Builder
squoted Builder
s =
  Builder
squote forall a. Semigroup a => a -> a -> a
<> Builder
s forall a. Semigroup a => a -> a -> a
<> Builder
squote

vcat :: Foldable f => f Builder -> Builder
vcat :: forall (f :: * -> *). Foldable f => f Builder -> Builder
vcat =
  forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> [a] -> [a]
List.intersperse Builder
newline forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
toList