Safe Haskell | Trustworthy |
---|---|
Language | Haskell98 |
System.Console.Wizard
Synopsis
- newtype Wizard (backend :: Type -> Type) a = Wizard (MaybeT (Free backend) a)
- type PromptString = String
- run :: forall (f :: Type -> Type) b a. (Functor f, Monad b, Run b f) => Wizard f a -> b (Maybe a)
- class (Functor sub, Functor sup) => (sub :: Type -> Type) :<: (sup :: Type -> Type)
- data ((f :: Type -> Type) :+: (g :: Type -> Type)) w
- data Line w
- line :: forall (b :: Type -> Type). Line :<: b => PromptString -> Wizard b String
- data LinePrewritten w
- linePrewritten :: forall (b :: Type -> Type). LinePrewritten :<: b => PromptString -> String -> String -> Wizard b String
- data Password w
- password :: forall (b :: Type -> Type). Password :<: b => PromptString -> Maybe Char -> Wizard b String
- data Character w
- character :: forall (b :: Type -> Type). Character :<: b => PromptString -> Wizard b Char
- data Output w
- output :: forall (b :: Type -> Type). Output :<: b => String -> Wizard b ()
- data OutputLn w
- outputLn :: forall (b :: Type -> Type). OutputLn :<: b => String -> Wizard b ()
- data ArbitraryIO w
- retry :: forall (b :: Type -> Type) a. Functor b => Wizard b a -> Wizard b a
- retryMsg :: forall (b :: Type -> Type) a. OutputLn :<: b => String -> Wizard b a -> Wizard b a
- defaultTo :: forall (b :: Type -> Type) a. Functor b => Wizard b a -> a -> Wizard b a
- parser :: forall (b :: Type -> Type) a c. Functor b => (a -> Maybe c) -> Wizard b a -> Wizard b c
- validator :: forall (b :: Type -> Type) a. Functor b => (a -> Bool) -> Wizard b a -> Wizard b a
- nonEmpty :: forall (b :: Type -> Type) a. Functor b => Wizard b [a] -> Wizard b [a]
- inRange :: forall a (b :: Type -> Type). (Ord a, Functor b) => (a, a) -> Wizard b a -> Wizard b a
- parseRead :: forall a (b :: Type -> Type). (Read a, Functor b) => Wizard b String -> Wizard b a
- liftMaybe :: forall (b :: Type -> Type) a. Functor b => Maybe a -> Wizard b a
- ensure :: (a -> Bool) -> a -> Maybe a
- readP :: Read a => String -> Maybe a
Wizards
newtype Wizard (backend :: Type -> Type) a Source #
A Wizard b a
is a conversation with the user via back-end b
that will result in a data type a
, or may fail.
A Wizard
is made up of one or more "primitives" (see below), composed using the Applicative
,
Monad
and Alternative
instances. The Alternative
instance is, as you might expect, a maybe-style cascade.
If the first wizard fails, the next one is tried. mzero
can be used to induce failure directly.
The Wizard
constructor is exported here for use when developing backends, but it is better for end-users to
simply pretend that Wizard
is an opaque data type. Don't depend on this unless you have no other choice.
Wizard
s are, internally, just a maybe transformer over a free monad built from some coproduct of functors,
each of which is a primitive action.
Instances
ArbitraryIO :<: b => MonadIO (Wizard b) Source # | |
Defined in System.Console.Wizard | |
Functor backend => Alternative (Wizard backend) Source # | |
Functor backend => Applicative (Wizard backend) Source # | |
Defined in System.Console.Wizard.Internal Methods pure :: a -> Wizard backend a # (<*>) :: Wizard backend (a -> b) -> Wizard backend a -> Wizard backend b # liftA2 :: (a -> b -> c) -> Wizard backend a -> Wizard backend b -> Wizard backend c # (*>) :: Wizard backend a -> Wizard backend b -> Wizard backend b # (<*) :: Wizard backend a -> Wizard backend b -> Wizard backend a # | |
Functor backend => Functor (Wizard backend) Source # | |
Functor backend => Monad (Wizard backend) Source # | |
Functor backend => MonadPlus (Wizard backend) Source # | |
type PromptString = String Source #
A string for a prompt
run :: forall (f :: Type -> Type) b a. (Functor f, Monad b, Run b f) => Wizard f a -> b (Maybe a) Source #
Run a wizard using some back-end.
class (Functor sub, Functor sup) => (sub :: Type -> Type) :<: (sup :: Type -> Type) Source #
Subsumption of two functors. You shouldn't define any of your own instances of this when writing back-ends, rely only on GeneralizedNewtypeDeriving.
Minimal complete definition
inj
Instances
data ((f :: Type -> Type) :+: (g :: Type -> Type)) w infixr 9 Source #
Coproduct of two functors
Instances
(Functor f, Functor g) => f :<: (f :+: g) Source # | |
Defined in System.Console.Wizard.Internal | |
(Functor f, Functor g, Functor h, f :<: g) => f :<: (h :+: g) Source # | |
Defined in System.Console.Wizard.Internal | |
(Run b f, Run b g) => Run b (f :+: g) Source # | |
Defined in System.Console.Wizard.Internal Methods runAlgebra :: (f :+: g) (b v) -> b v Source # | |
(Functor f, Functor g) => Functor (f :+: g) Source # | |
Primitives
Primitives are the basic building blocks for wizards
. Use these functions to produce wizards that
ask for input from the user, or output information.
Instances
Functor Line Source # | |
Line :<: BasicIO Source # | |
Defined in System.Console.Wizard.BasicIO | |
Line :<: Haskeline Source # | |
Defined in System.Console.Wizard.Haskeline | |
Line :<: Pure Source # | |
Defined in System.Console.Wizard.Pure | |
Run IO Line Source # | |
Defined in System.Console.Wizard.BasicIO | |
Run (InputT IO) Line Source # | |
Defined in System.Console.Wizard.Haskeline | |
Run (State PureState) Line Source # | |
Defined in System.Console.Wizard.Pure |
line :: forall (b :: Type -> Type). Line :<: b => PromptString -> Wizard b String Source #
Read one line of input from the user. Cannot fail (but may throw exceptions, depending on the backend).
data LinePrewritten w Source #
Instances
Functor LinePrewritten Source # | |
Defined in System.Console.Wizard.Internal Methods fmap :: (a -> b) -> LinePrewritten a -> LinePrewritten b # (<$) :: a -> LinePrewritten b -> LinePrewritten a # | |
LinePrewritten :<: Haskeline Source # | |
Defined in System.Console.Wizard.Haskeline Methods inj :: LinePrewritten a -> Haskeline a | |
Run (InputT IO) LinePrewritten Source # | |
Defined in System.Console.Wizard.Haskeline Methods runAlgebra :: LinePrewritten (InputT IO v) -> InputT IO v Source # |
Arguments
:: forall (b :: Type -> Type). LinePrewritten :<: b | |
=> PromptString | |
-> String | Text to the left of the cursor |
-> String | Text to the right of the cursor |
-> Wizard b String |
Read one line of input, with some default text already present, before and/or after the editing cursor.
Arguments
:: forall (b :: Type -> Type). Password :<: b | |
=> PromptString | |
-> Maybe Char | Mask character, if any. |
-> Wizard b String |
Read one line of password input, with an optional mask character.
Instances
Functor Character Source # | |
Character :<: BasicIO Source # | |
Defined in System.Console.Wizard.BasicIO | |
Character :<: Haskeline Source # | |
Defined in System.Console.Wizard.Haskeline | |
Character :<: Pure Source # | |
Defined in System.Console.Wizard.Pure | |
Run IO Character Source # | |
Defined in System.Console.Wizard.BasicIO | |
Run (InputT IO) Character Source # | |
Defined in System.Console.Wizard.Haskeline | |
Run (State PureState) Character Source # | |
Defined in System.Console.Wizard.Pure |
character :: forall (b :: Type -> Type). Character :<: b => PromptString -> Wizard b Char Source #
Read a single character only from input. Cannot fail (but may throw exceptions, depending on the backend).
Instances
Functor Output Source # | |
Output :<: BasicIO Source # | |
Defined in System.Console.Wizard.BasicIO | |
Output :<: Haskeline Source # | |
Defined in System.Console.Wizard.Haskeline | |
Output :<: Pure Source # | |
Defined in System.Console.Wizard.Pure | |
Run IO Output Source # | |
Defined in System.Console.Wizard.BasicIO | |
Run (InputT IO) Output Source # | |
Defined in System.Console.Wizard.Haskeline | |
Run (State PureState) Output Source # | |
Defined in System.Console.Wizard.Pure |
output :: forall (b :: Type -> Type). Output :<: b => String -> Wizard b () Source #
Output a string. Does not fail.
Instances
Functor OutputLn Source # | |
OutputLn :<: BasicIO Source # | |
Defined in System.Console.Wizard.BasicIO | |
OutputLn :<: Haskeline Source # | |
Defined in System.Console.Wizard.Haskeline | |
OutputLn :<: Pure Source # | |
Defined in System.Console.Wizard.Pure | |
Run IO OutputLn Source # | |
Defined in System.Console.Wizard.BasicIO | |
Run (InputT IO) OutputLn Source # | |
Defined in System.Console.Wizard.Haskeline | |
Run (State PureState) OutputLn Source # | |
Defined in System.Console.Wizard.Pure |
outputLn :: forall (b :: Type -> Type). OutputLn :<: b => String -> Wizard b () Source #
Output a string followed by a newline. Does not fail.
data ArbitraryIO w Source #
Instances
Functor ArbitraryIO Source # | |
Defined in System.Console.Wizard.Internal Methods fmap :: (a -> b) -> ArbitraryIO a -> ArbitraryIO b # (<$) :: a -> ArbitraryIO b -> ArbitraryIO a # | |
ArbitraryIO :<: BasicIO Source # | |
Defined in System.Console.Wizard.BasicIO Methods inj :: ArbitraryIO a -> BasicIO a | |
ArbitraryIO :<: Haskeline Source # | |
Defined in System.Console.Wizard.Haskeline Methods inj :: ArbitraryIO a -> Haskeline a | |
Run IO ArbitraryIO Source # | |
Defined in System.Console.Wizard.BasicIO Methods runAlgebra :: ArbitraryIO (IO v) -> IO v Source # | |
Run (InputT IO) ArbitraryIO Source # | |
Defined in System.Console.Wizard.Haskeline Methods runAlgebra :: ArbitraryIO (InputT IO v) -> InputT IO v Source # |
Modifiers
Modifiers change the behaviour of existing wizards.
retry :: forall (b :: Type -> Type) a. Functor b => Wizard b a -> Wizard b a Source #
Retry produces a wizard that will retry the entire conversation again if it fails.
It is simply retry x = x <|> retry x
.
retryMsg :: forall (b :: Type -> Type) a. OutputLn :<: b => String -> Wizard b a -> Wizard b a Source #
Same as retry
, except an error message can be specified.
defaultTo :: forall (b :: Type -> Type) a. Functor b => Wizard b a -> a -> Wizard b a Source #
x `defaultTo` y
will return y
if x
fails, e.g parseRead line `defaultTo` 0
.
parser :: forall (b :: Type -> Type) a c. Functor b => (a -> Maybe c) -> Wizard b a -> Wizard b c Source #
validator :: forall (b :: Type -> Type) a. Functor b => (a -> Bool) -> Wizard b a -> Wizard b a Source #
validator p
causes a wizard to fail if the output value does not satisfy the predicate p
.
Convenience
nonEmpty :: forall (b :: Type -> Type) a. Functor b => Wizard b [a] -> Wizard b [a] Source #
Simply validator (not . null)
, makes a wizard fail if it gets an empty string.
inRange :: forall a (b :: Type -> Type). (Ord a, Functor b) => (a, a) -> Wizard b a -> Wizard b a Source #
Makes a wizard fail if it gets an ordered quantity outside of the given range.
parseRead :: forall a (b :: Type -> Type). (Read a, Functor b) => Wizard b String -> Wizard b a Source #
Simply parser readP
. Attaches a simple read
parser to a Wizard
.
Utility
liftMaybe :: forall (b :: Type -> Type) a. Functor b => Maybe a -> Wizard b a Source #
Translate a maybe value into wizard success/failure.
ensure :: (a -> Bool) -> a -> Maybe a Source #
Ensures that a maybe value satisfies a given predicate.