wizards-0.1: High level, generic library for interrogative user interfaces

Safe HaskellTrustworthy

System.Console.Wizard

Contents

Synopsis

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.

Wizards are, internally, just a maybe transformer over a prompt monad for each primitive action.

Constructors

Wizard (MaybeT (RecPrompt (WizardAction backend)) a) 

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.

linePrewrittenSource

Arguments

:: 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.

passwordSource

Arguments

:: 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.

output :: String -> Wizard b ()Source

Output a string, if the backend used supports output.

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.

parser :: (a -> Maybe c) -> Wizard b a -> Wizard b cSource

Like fmap, except the function may be partial (Nothing causes the wizard to fail).

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.

Utility

liftMaybe :: Maybe a -> Wizard b aSource

Translate a maybe value into wizard success/failure.

ensure :: (a -> Bool) -> a -> Maybe aSource

Ensures that a maybe value satisfies a given predicate.

readP :: Read a => String -> Maybe aSource

A read-based parser for the parser modifier.