{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Calligraphy.Util.Printer
  ( Printer,
    Prints,
    runPrinter,
    indent,
    brack,
    strLn,
    textLn,
    showLn,
  )
where

import Calligraphy.Prelude
import Control.Monad.RWS
import Data.Text (Text)
import qualified Data.Text.Lazy as TL
import Data.Text.Lazy.Builder (Builder)
import qualified Data.Text.Lazy.Builder as TB

-- | An monadic interface to a fairly primitive line printer.
-- It maintains an indentation level, and provides efficient concatenation through 'Builder', and that's it.
newtype Printer a = Printer {forall a. Printer a -> RWS Int () Builder a
_unPrinter :: RWS Int () Builder a}
  deriving newtype (forall a b. a -> Printer b -> Printer a
forall a b. (a -> b) -> Printer a -> Printer b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Printer b -> Printer a
$c<$ :: forall a b. a -> Printer b -> Printer a
fmap :: forall a b. (a -> b) -> Printer a -> Printer b
$cfmap :: forall a b. (a -> b) -> Printer a -> Printer b
Functor, Functor Printer
forall a. a -> Printer a
forall a b. Printer a -> Printer b -> Printer a
forall a b. Printer a -> Printer b -> Printer b
forall a b. Printer (a -> b) -> Printer a -> Printer b
forall a b c. (a -> b -> c) -> Printer a -> Printer b -> Printer c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. Printer a -> Printer b -> Printer a
$c<* :: forall a b. Printer a -> Printer b -> Printer a
*> :: forall a b. Printer a -> Printer b -> Printer b
$c*> :: forall a b. Printer a -> Printer b -> Printer b
liftA2 :: forall a b c. (a -> b -> c) -> Printer a -> Printer b -> Printer c
$cliftA2 :: forall a b c. (a -> b -> c) -> Printer a -> Printer b -> Printer c
<*> :: forall a b. Printer (a -> b) -> Printer a -> Printer b
$c<*> :: forall a b. Printer (a -> b) -> Printer a -> Printer b
pure :: forall a. a -> Printer a
$cpure :: forall a. a -> Printer a
Applicative, Applicative Printer
forall a. a -> Printer a
forall a b. Printer a -> Printer b -> Printer b
forall a b. Printer a -> (a -> Printer b) -> Printer b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> Printer a
$creturn :: forall a. a -> Printer a
>> :: forall a b. Printer a -> Printer b -> Printer b
$c>> :: forall a b. Printer a -> Printer b -> Printer b
>>= :: forall a b. Printer a -> (a -> Printer b) -> Printer b
$c>>= :: forall a b. Printer a -> (a -> Printer b) -> Printer b
Monad)
  deriving (NonEmpty (Printer a) -> Printer a
Printer a -> Printer a -> Printer a
forall b. Integral b => b -> Printer a -> Printer a
forall a. Semigroup a => NonEmpty (Printer a) -> Printer a
forall a. Semigroup a => Printer a -> Printer a -> Printer a
forall a b.
(Semigroup a, Integral b) =>
b -> Printer a -> Printer a
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
stimes :: forall b. Integral b => b -> Printer a -> Printer a
$cstimes :: forall a b.
(Semigroup a, Integral b) =>
b -> Printer a -> Printer a
sconcat :: NonEmpty (Printer a) -> Printer a
$csconcat :: forall a. Semigroup a => NonEmpty (Printer a) -> Printer a
<> :: Printer a -> Printer a -> Printer a
$c<> :: forall a. Semigroup a => Printer a -> Printer a -> Printer a
Semigroup, Printer a
[Printer a] -> Printer a
Printer a -> Printer a -> Printer a
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall {a}. Monoid a => Semigroup (Printer a)
forall a. Monoid a => Printer a
forall a. Monoid a => [Printer a] -> Printer a
forall a. Monoid a => Printer a -> Printer a -> Printer a
mconcat :: [Printer a] -> Printer a
$cmconcat :: forall a. Monoid a => [Printer a] -> Printer a
mappend :: Printer a -> Printer a -> Printer a
$cmappend :: forall a. Monoid a => Printer a -> Printer a -> Printer a
mempty :: Printer a
$cmempty :: forall a. Monoid a => Printer a
Monoid) via (Ap Printer a)

type Prints a = a -> Printer ()

runPrinter :: Printer () -> Text
runPrinter :: Printer () -> Text
runPrinter (Printer RWS Int () Builder ()
p) = Text -> Text
TL.toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
TB.toLazyText forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall r w s a. RWS r w s a -> r -> s -> (s, w)
execRWS RWS Int () Builder ()
p Int
0 forall a. Monoid a => a
mempty

{-# INLINE indent #-}
indent :: Printer a -> Printer a
indent :: forall a. Printer a -> Printer a
indent (Printer RWS Int () Builder a
p) = forall a. RWS Int () Builder a -> Printer a
Printer forall a b. (a -> b) -> a -> b
$ forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (forall a. Num a => a -> a -> a
+ Int
4) RWS Int () Builder a
p

{-# INLINE line #-}
line :: Builder -> Printer ()
line :: Builder -> Printer ()
line Builder
t = forall a. RWS Int () Builder a -> Printer a
Printer forall a b. (a -> b) -> a -> b
$ do
  Int
n <- forall r (m :: * -> *). MonadReader r m => m r
ask
  forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$
    forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Monoid a => a -> a -> a
mappend forall a b. (a -> b) -> a -> b
$
      forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold (forall a. Int -> a -> [a]
replicate Int
n (Char -> Builder
TB.singleton Char
' ')) forall a. Semigroup a => a -> a -> a
<> Builder
t forall a. Semigroup a => a -> a -> a
<> Char -> Builder
TB.singleton Char
'\n'

{-# INLINE brack #-}
brack :: String -> String -> Printer a -> Printer a
brack :: forall a. String -> String -> Printer a -> Printer a
brack String
pre String
post Printer a
inner = String -> Printer ()
strLn String
pre forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall a. Printer a -> Printer a
indent Printer a
inner forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* String -> Printer ()
strLn String
post

{-# INLINE strLn #-}
strLn :: String -> Printer ()
strLn :: String -> Printer ()
strLn = Builder -> Printer ()
line forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Builder
TB.fromString

{-# INLINE textLn #-}
textLn :: Text -> Printer ()
textLn :: Text -> Printer ()
textLn = Builder -> Printer ()
line forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
TB.fromText

{-# INLINE showLn #-}
showLn :: (Show a) => a -> Printer ()
showLn :: forall a. Show a => a -> Printer ()
showLn = String -> Printer ()
strLn forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show