{-# OPTIONS_HADDOCK hide #-}

-- |
--
-- Copyright:
--   This file is part of the package byline. It is subject to the
--   license terms in the LICENSE file found in the top-level
--   directory of this distribution and at:
--
--     https://github.com/pjones/byline
--
--   No part of this package, including this file, may be copied,
--   modified, propagated, or distributed except according to the
--   terms contained in the LICENSE file.
--
-- License: BSD-2-Clause
module Byline.Internal.Prim
  ( PrimF (..),
    say,
    sayLn,
    askLn,
    askChar,
    askPassword,
    pushCompFunc,
    popCompFunc,
  )
where

import Byline.Internal.Completion (CompletionFunc)
import Byline.Internal.Stylized (Stylized, text)
import Control.Monad.Trans.Free.Church (MonadFree)
import qualified Control.Monad.Trans.Free.Church as Free

-- | Primitive operations as a free monad.
--
-- @since 1.0.0.0
data PrimF f
  = Say (Stylized Text) f
  | AskLn (Stylized Text) (Maybe Text) (Text -> f)
  | AskChar (Stylized Text) (Char -> f)
  | AskPassword (Stylized Text) (Maybe Char) (Text -> f)
  | PushCompFunc (CompletionFunc IO) f
  | PopCompFunc f
  deriving (a -> PrimF b -> PrimF a
(a -> b) -> PrimF a -> PrimF b
(forall a b. (a -> b) -> PrimF a -> PrimF b)
-> (forall a b. a -> PrimF b -> PrimF a) -> Functor PrimF
forall a b. a -> PrimF b -> PrimF a
forall a b. (a -> b) -> PrimF a -> PrimF b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> PrimF b -> PrimF a
$c<$ :: forall a b. a -> PrimF b -> PrimF a
fmap :: (a -> b) -> PrimF a -> PrimF b
$cfmap :: forall a b. (a -> b) -> PrimF a -> PrimF b
Functor)

-- | Smart constructor.
--
-- @since 1.0.0.0
say :: MonadFree PrimF m => Stylized Text -> m ()
say :: Stylized Text -> m ()
say = PrimF () -> m ()
forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
Free.liftF (PrimF () -> m ())
-> (Stylized Text -> PrimF ()) -> Stylized Text -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Stylized Text -> () -> PrimF ()
forall f. Stylized Text -> f -> PrimF f
`Say` ())

-- | Smart constructor.
--
-- @since 1.0.0.0
sayLn :: MonadFree PrimF m => Stylized Text -> m ()
sayLn :: Stylized Text -> m ()
sayLn Stylized Text
message = Stylized Text -> m ()
forall (m :: * -> *). MonadFree PrimF m => Stylized Text -> m ()
say (Stylized Text
message Stylized Text -> Stylized Text -> Stylized Text
forall a. Semigroup a => a -> a -> a
<> Text -> Stylized Text
text Text
"\n")

-- | Smart constructor.
--
-- @since 1.0.0.0
askLn :: MonadFree PrimF m => Stylized Text -> Maybe Text -> m Text
askLn :: Stylized Text -> Maybe Text -> m Text
askLn Stylized Text
prompt Maybe Text
def = PrimF Text -> m Text
forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
Free.liftF (Stylized Text -> Maybe Text -> (Text -> Text) -> PrimF Text
forall f. Stylized Text -> Maybe Text -> (Text -> f) -> PrimF f
AskLn Stylized Text
prompt Maybe Text
def Text -> Text
forall a. a -> a
id)

-- | Smart constructor.
--
-- @since 1.0.0.0
askChar :: MonadFree PrimF m => Stylized Text -> m Char
askChar :: Stylized Text -> m Char
askChar = PrimF Char -> m Char
forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
Free.liftF (PrimF Char -> m Char)
-> (Stylized Text -> PrimF Char) -> Stylized Text -> m Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Stylized Text -> (Char -> Char) -> PrimF Char
forall f. Stylized Text -> (Char -> f) -> PrimF f
`AskChar` Char -> Char
forall a. a -> a
id)

-- | Smart constructor.
--
-- @since 1.0.0.0
askPassword :: MonadFree PrimF m => Stylized Text -> Maybe Char -> m Text
askPassword :: Stylized Text -> Maybe Char -> m Text
askPassword Stylized Text
prompt Maybe Char
mask = PrimF Text -> m Text
forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
Free.liftF (Stylized Text -> Maybe Char -> (Text -> Text) -> PrimF Text
forall f. Stylized Text -> Maybe Char -> (Text -> f) -> PrimF f
AskPassword Stylized Text
prompt Maybe Char
mask Text -> Text
forall a. a -> a
id)

-- | Smart constructor.
--
-- @since 1.0.0.0
pushCompFunc :: MonadFree PrimF m => CompletionFunc IO -> m ()
pushCompFunc :: CompletionFunc IO -> m ()
pushCompFunc = PrimF () -> m ()
forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
Free.liftF (PrimF () -> m ())
-> (CompletionFunc IO -> PrimF ()) -> CompletionFunc IO -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CompletionFunc IO -> () -> PrimF ()
forall f. CompletionFunc IO -> f -> PrimF f
`PushCompFunc` ())

-- | Smart constructor.
--
-- @since 1.0.0.0
popCompFunc :: MonadFree PrimF m => m ()
popCompFunc :: m ()
popCompFunc = PrimF () -> m ()
forall (f :: * -> *) (m :: * -> *) a.
(Functor f, MonadFree f m) =>
f a -> m a
Free.liftF (() -> PrimF ()
forall f. f -> PrimF f
PopCompFunc ())