Safe Haskell | Trustworthy |
---|
- newtype Wizard backend a = Wizard (MaybeT (RecPrompt (WizardAction backend)) a)
- type PromptString = String
- line :: PromptString -> Wizard b String
- linePrewritten :: PromptString -> String -> String -> Wizard b String
- password :: PromptString -> Maybe Char -> Wizard b String
- character :: PromptString -> Wizard b Char
- output :: String -> Wizard b ()
- outputLn :: String -> Wizard b ()
- retry :: Wizard b a -> Wizard b a
- retryMsg :: String -> Wizard b a -> Wizard b a
- defaultTo :: Wizard b a -> a -> Wizard b a
- parser :: (a -> Maybe c) -> Wizard b a -> Wizard b c
- validator :: (a -> Bool) -> Wizard b a -> Wizard b a
- nonEmpty :: Wizard b [a] -> Wizard b [a]
- inRange :: Ord a => (a, a) -> Wizard b a -> Wizard b a
- parseRead :: Read a => Wizard b String -> Wizard b a
- liftMaybe :: Maybe a -> Wizard b a
- ensure :: (a -> Bool) -> a -> Maybe a
- readP :: Read a => String -> Maybe a
Wizards
newtype Wizard backend a Source
A Wizard a
is a conversation with the user 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.
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 prompt monad for each primitive action.
Wizard (MaybeT (RecPrompt (WizardAction backend)) a) |
Monad (Wizard backend) | |
Functor (Wizard backend) | |
MonadPlus (Wizard backend) | |
Applicative (Wizard backend) | |
Alternative (Wizard backend) | |
MonadIO (Wizard Haskeline) | |
MonadIO (Wizard BasicIO) | |
MonadPrompt (WizardAction s (RecPrompt (WizardAction s))) (Wizard s) |
type PromptString = StringSource
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.
line :: PromptString -> Wizard b StringSource
Read one line of input from the user.
:: 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. Backends are not required to display this default text, or position the cursor anywhere, it is merely a suggestion.
:: PromptString | |
-> Maybe Char | Mask character, if any. |
-> Wizard b String |
Read one line of password input, with an optional mask character. The exact masking behavior of the password may vary from backend to backend. The masking character does not have to be honoured.
character :: PromptString -> Wizard b CharSource
Read a single character only from input.
outputLn :: String -> Wizard b ()Source
Output a string followed by a newline, if the backend used supports such output.
Modifiers
Modifiers change the behaviour of existing wizards.
retry :: Wizard b a -> Wizard b aSource
Retry produces a wizard that will retry the entire conversation again if it fails.
Conceptually, it could thought of as retry x = x <|> retry x
, however it also prints
a user-friendly error message in the event of failure.
retryMsg :: String -> Wizard b a -> Wizard b aSource
Same as retry
, except the error message can be specified.
defaultTo :: Wizard b a -> a -> Wizard b aSource
x `defaultTo` y
will return y
if x
fails, e.g parseRead line `defaultTo` 0
.
validator :: (a -> Bool) -> Wizard b a -> Wizard b aSource
validator p w
causes a wizard to fail if the output value does not satisfy the predicate p
.
Convenience
nonEmpty :: Wizard b [a] -> Wizard b [a]Source
Simply validator (not . null)
, makes a wizard fail if it gets an empty string.
inRange :: Ord a => (a, a) -> Wizard b a -> Wizard b aSource
Makes a wizard fail if it gets an ordered quantity outside of the given range.
parseRead :: Read a => Wizard b String -> Wizard b aSource
Simply parser readP
. Attaches a simple read
parser to a Wizard
.