{-# LANGUAGE DeriveFunctor, DeriveGeneric #-}
module Control.Effect.Readline
( -- * Readline effect
  Readline(..)
, prompt
, print
  -- * Re-exports
, Algebra
, Has
, run
) where

import Control.Algebra
import Data.Text.Prettyprint.Doc (Doc)
import Data.Text.Prettyprint.Doc.Render.Terminal (AnsiStyle)
import GHC.Generics (Generic1)
import Prelude hiding (print)

prompt :: Has Readline sig m => String -> m (Int, Maybe String)
prompt :: String -> m (Int, Maybe String)
prompt p :: String
p = Readline m (Int, Maybe String) -> m (Int, Maybe String)
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
       (m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (String
-> (Int -> Maybe String -> m (Int, Maybe String))
-> Readline m (Int, Maybe String)
forall (m :: * -> *) k.
String -> (Int -> Maybe String -> m k) -> Readline m k
Prompt String
p (((Int, Maybe String) -> m (Int, Maybe String))
-> Int -> Maybe String -> m (Int, Maybe String)
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (Int, Maybe String) -> m (Int, Maybe String)
forall (f :: * -> *) a. Applicative f => a -> f a
pure))

print :: Has Readline sig m => Doc AnsiStyle -> m ()
print :: Doc AnsiStyle -> m ()
print s :: Doc AnsiStyle
s = Readline m () -> m ()
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
       (m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (Doc AnsiStyle -> m () -> Readline m ()
forall (m :: * -> *) k. Doc AnsiStyle -> m k -> Readline m k
Print Doc AnsiStyle
s (() -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()))


data Readline m k
  = Prompt String (Int -> Maybe String -> m k)
  | Print (Doc AnsiStyle) (m k)
  deriving (a -> Readline m b -> Readline m a
(a -> b) -> Readline m a -> Readline m b
(forall a b. (a -> b) -> Readline m a -> Readline m b)
-> (forall a b. a -> Readline m b -> Readline m a)
-> Functor (Readline m)
forall a b. a -> Readline m b -> Readline m a
forall a b. (a -> b) -> Readline m a -> Readline m b
forall (m :: * -> *) a b.
Functor m =>
a -> Readline m b -> Readline m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> Readline m a -> Readline m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Readline m b -> Readline m a
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> Readline m b -> Readline m a
fmap :: (a -> b) -> Readline m a -> Readline m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> Readline m a -> Readline m b
Functor, (forall a. Readline m a -> Rep1 (Readline m) a)
-> (forall a. Rep1 (Readline m) a -> Readline m a)
-> Generic1 (Readline m)
forall a. Rep1 (Readline m) a -> Readline m a
forall a. Readline m a -> Rep1 (Readline m) a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
forall (m :: * -> *) a. Rep1 (Readline m) a -> Readline m a
forall (m :: * -> *) a. Readline m a -> Rep1 (Readline m) a
$cto1 :: forall (m :: * -> *) a. Rep1 (Readline m) a -> Readline m a
$cfrom1 :: forall (m :: * -> *) a. Readline m a -> Rep1 (Readline m) a
Generic1)

instance HFunctor Readline
instance Effect   Readline