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

Safe Haskell | Trustworthy
Language | Haskell98

System.Console.Wizard

Contents

- Wizards
- Primitives
- Modifiers
- Convenience
- Utility
- Orphan instances

Synopsis

- newtype Wizard backend a = Wizard (MaybeT (Free backend) a)
- type PromptString = String
- run :: (Functor f, Monad b, Run b f) => Wizard f a -> b (Maybe a)
- class (Functor sub, Functor sup) => sub :<: sup
- data (f :+: g) w
- data Line w
- line :: Line :<: b => PromptString -> Wizard b String
- data LinePrewritten w
- linePrewritten :: LinePrewritten :<: b => PromptString -> String -> String -> Wizard b String
- data Password w
- password :: Password :<: b => PromptString -> Maybe Char -> Wizard b String
- data Character w
- character :: Character :<: b => PromptString -> Wizard b Char
- data Output w
- output :: Output :<: b => String -> Wizard b ()
- data OutputLn w
- outputLn :: OutputLn :<: b => String -> Wizard b ()
- data ArbitraryIO w
- retry :: Functor b => Wizard b a -> Wizard b a
- retryMsg :: OutputLn :<: b => String -> Wizard b a -> Wizard b a
- defaultTo :: Functor b => Wizard b a -> a -> Wizard b a
- parser :: Functor b => (a -> Maybe c) -> Wizard b a -> Wizard b c
- validator :: Functor b => (a -> Bool) -> Wizard b a -> Wizard b a
- nonEmpty :: Functor b => Wizard b [a] -> Wizard b [a]
- inRange :: (Ord a, Functor b) => (a, a) -> Wizard b a -> Wizard b a
- parseRead :: (Read a, Functor b) => Wizard b String -> Wizard b a
- liftMaybe :: Functor b => 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 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.

Constructors

Wizard (MaybeT (Free backend) a) |

Instances

Functor backend => Monad (Wizard backend) Source # |
Instance details Defined in System.Console.Wizard.Internal Methods (>>=) :: Wizard backend a -> (a -> Wizard backend b) -> Wizard backend b Source # (>>) :: Wizard backend a -> Wizard backend b -> Wizard backend b Source # return :: a -> Wizard backend a Source # fail :: String -> Wizard backend a Source #
Functor backend => Functor (Wizard backend) Source # |
Instance details Defined in System.Console.Wizard.Internal Methods fmap :: (a -> b) -> Wizard backend a -> Wizard backend b Source # (<$) :: a -> Wizard backend b -> Wizard backend a Source #
Functor backend => Applicative (Wizard backend) Source # |

Instance details

Defined in System.Console.Wizard.Internal

Methods

pure :: a -> Wizard backend a Source #

(<*>) :: Wizard backend (a -> b) -> Wizard backend a -> Wizard backend b Source #

liftA2 :: (a -> b -> c) -> Wizard backend a -> Wizard backend b -> Wizard backend c Source #

(*>) :: Wizard backend a -> Wizard backend b -> Wizard backend b Source #

(<*) :: Wizard backend a -> Wizard backend b -> Wizard backend a Source #

ArbitraryIO :<: b => MonadIO (Wizard b) Source # |
Instance details Defined in System.Console.Wizard Methods liftIO :: IO a -> Wizard b a Source #
Functor backend => Alternative (Wizard backend) Source # |
Instance details Defined in System.Console.Wizard.Internal Methods empty :: Wizard backend a Source # (<|>) :: Wizard backend a -> Wizard backend a -> Wizard backend a Source # some :: Wizard backend a -> Wizard backend [a] Source # many :: Wizard backend a -> Wizard backend [a] Source #
Functor backend => MonadPlus (Wizard backend) Source # |
Instance details Defined in System.Console.Wizard.Internal Methods mzero :: Wizard backend a Source # mplus :: Wizard backend a -> Wizard backend a -> Wizard backend a Source #

type PromptString = String Source #

A string for a prompt

run :: (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 :<: sup 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

Functor f => f :<: f Source # |
Instance details Defined in System.Console.Wizard.Internal Methods inj :: f a -> f a
ArbitraryIO :<: Haskeline Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods inj :: ArbitraryIO a -> Haskeline a
ArbitraryIO :<: BasicIO Source # |
Instance details Defined in System.Console.Wizard.BasicIO Methods inj :: ArbitraryIO a -> BasicIO a
Password :<: Haskeline Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods inj :: Password a -> Haskeline a
LinePrewritten :<: Haskeline Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods inj :: LinePrewritten a -> Haskeline a
Character :<: Haskeline Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods inj :: Character a -> Haskeline a
Character :<: BasicIO Source # |
Instance details Defined in System.Console.Wizard.BasicIO Methods inj :: Character a -> BasicIO a
Character :<: Pure Source # |
Instance details Defined in System.Console.Wizard.Pure Methods inj :: Character a -> Pure a
Line :<: Haskeline Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods inj :: Line a -> Haskeline a
Line :<: BasicIO Source # |
Instance details Defined in System.Console.Wizard.BasicIO Methods inj :: Line a -> BasicIO a
Line :<: Pure Source # |
Instance details Defined in System.Console.Wizard.Pure Methods inj :: Line a -> Pure a
OutputLn :<: Haskeline Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods inj :: OutputLn a -> Haskeline a
OutputLn :<: BasicIO Source # |
Instance details Defined in System.Console.Wizard.BasicIO Methods inj :: OutputLn a -> BasicIO a
OutputLn :<: Pure Source # |
Instance details Defined in System.Console.Wizard.Pure Methods inj :: OutputLn a -> Pure a
Output :<: Haskeline Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods inj :: Output a -> Haskeline a
Output :<: BasicIO Source # |
Instance details Defined in System.Console.Wizard.BasicIO Methods inj :: Output a -> BasicIO a
Output :<: Pure Source # |
Instance details Defined in System.Console.Wizard.Pure Methods inj :: Output a -> Pure a
WithSettings :<: Haskeline Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods inj :: WithSettings a -> Haskeline a
(Functor f, Functor g, Functor h, f :<: g) => f :<: (h :+: g) Source # |
Instance details Defined in System.Console.Wizard.Internal Methods inj :: f a -> (h :+: g) a
(Functor f, Functor g) => f :<: (f :+: g) Source # |
Instance details Defined in System.Console.Wizard.Internal Methods inj :: f a -> (f :+: g) a

data (f :+: g) w infixr 9 Source #

Coproduct of two functors

Instances

(Run b f, Run b g) => Run b (f :+: g) Source # |
Instance details Defined in System.Console.Wizard.Internal Methods runAlgebra :: (f :+: g) (b v) -> b v Source #
(Functor f, Functor g, Functor h, f :<: g) => f :<: (h :+: g) Source # |
Instance details Defined in System.Console.Wizard.Internal Methods inj :: f a -> (h :+: g) a
(Functor f, Functor g) => f :<: (f :+: g) Source # |
Instance details Defined in System.Console.Wizard.Internal Methods inj :: f a -> (f :+: g) a
(Functor f, Functor g) => Functor (f :+: g) Source # |
Instance details Defined in System.Console.Wizard.Internal Methods fmap :: (a -> b) -> (f :+: g) a -> (f :+: g) b Source # (<$) :: a -> (f :+: g) b -> (f :+: g) a 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.

data Line w Source #

Instances

Functor Line Source # |
Instance details Defined in System.Console.Wizard.Internal Methods fmap :: (a -> b) -> Line a -> Line b Source # (<$) :: a -> Line b -> Line a Source #
Run IO Line Source # |
Instance details Defined in System.Console.Wizard.BasicIO Methods runAlgebra :: Line (IO v) -> IO v Source #
Line :<: Haskeline Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods inj :: Line a -> Haskeline a
Line :<: BasicIO Source # |
Instance details Defined in System.Console.Wizard.BasicIO Methods inj :: Line a -> BasicIO a
Line :<: Pure Source # |
Instance details Defined in System.Console.Wizard.Pure Methods inj :: Line a -> Pure a
Run (InputT IO) Line Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods runAlgebra :: Line (InputT IO v) -> InputT IO v Source #
Run (State PureState) Line Source # |
Instance details Defined in System.Console.Wizard.Pure Methods runAlgebra :: Line (State PureState v) -> State PureState v Source #

line :: 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 # |
Instance details Defined in System.Console.Wizard.Internal Methods fmap :: (a -> b) -> LinePrewritten a -> LinePrewritten b Source # (<$) :: a -> LinePrewritten b -> LinePrewritten a Source #
LinePrewritten :<: Haskeline Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods inj :: LinePrewritten a -> Haskeline a
Run (InputT IO) LinePrewritten Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods runAlgebra :: LinePrewritten (InputT IO v) -> InputT IO v Source #

linePrewritten Source #

Arguments

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

data Password w Source #

Instances

Functor Password Source # |
Instance details Defined in System.Console.Wizard.Internal Methods fmap :: (a -> b) -> Password a -> Password b Source # (<$) :: a -> Password b -> Password a Source #
Password :<: Haskeline Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods inj :: Password a -> Haskeline a
Run (InputT IO) Password Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods runAlgebra :: Password (InputT IO v) -> InputT IO v Source #

password Source #

Arguments

:: Password :<: b |
=> PromptString |
-> Maybe Char | Mask character, if any.
-> Wizard b String |

Read one line of password input, with an optional mask character.

data Character w Source #

Instances

Functor Character Source # |
Instance details Defined in System.Console.Wizard.Internal Methods fmap :: (a -> b) -> Character a -> Character b Source # (<$) :: a -> Character b -> Character a Source #
Run IO Character Source # |
Instance details Defined in System.Console.Wizard.BasicIO Methods runAlgebra :: Character (IO v) -> IO v Source #
Character :<: Haskeline Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods inj :: Character a -> Haskeline a
Character :<: BasicIO Source # |
Instance details Defined in System.Console.Wizard.BasicIO Methods inj :: Character a -> BasicIO a
Character :<: Pure Source # |
Instance details Defined in System.Console.Wizard.Pure Methods inj :: Character a -> Pure a
Run (InputT IO) Character Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods runAlgebra :: Character (InputT IO v) -> InputT IO v Source #
Run (State PureState) Character Source # |
Instance details Defined in System.Console.Wizard.Pure Methods runAlgebra :: Character (State PureState v) -> State PureState v Source #

character :: Character :<: b => PromptString -> Wizard b Char Source #

Read a single character only from input. Cannot fail (but may throw exceptions, depending on the backend).

data Output w Source #

Instances

Functor Output Source # |
Instance details Defined in System.Console.Wizard.Internal Methods fmap :: (a -> b) -> Output a -> Output b Source # (<$) :: a -> Output b -> Output a Source #
Run IO Output Source # |
Instance details Defined in System.Console.Wizard.BasicIO Methods runAlgebra :: Output (IO v) -> IO v Source #
Output :<: Haskeline Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods inj :: Output a -> Haskeline a
Output :<: BasicIO Source # |
Instance details Defined in System.Console.Wizard.BasicIO Methods inj :: Output a -> BasicIO a
Output :<: Pure Source # |
Instance details Defined in System.Console.Wizard.Pure Methods inj :: Output a -> Pure a
Run (InputT IO) Output Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods runAlgebra :: Output (InputT IO v) -> InputT IO v Source #
Run (State PureState) Output Source # |
Instance details Defined in System.Console.Wizard.Pure Methods runAlgebra :: Output (State PureState v) -> State PureState v Source #

output :: Output :<: b => String -> Wizard b () Source #

Output a string. Does not fail.

data OutputLn w Source #

Instances

Functor OutputLn Source # |
Instance details Defined in System.Console.Wizard.Internal Methods fmap :: (a -> b) -> OutputLn a -> OutputLn b Source # (<$) :: a -> OutputLn b -> OutputLn a Source #
Run IO OutputLn Source # |
Instance details Defined in System.Console.Wizard.BasicIO Methods runAlgebra :: OutputLn (IO v) -> IO v Source #
OutputLn :<: Haskeline Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods inj :: OutputLn a -> Haskeline a
OutputLn :<: BasicIO Source # |
Instance details Defined in System.Console.Wizard.BasicIO Methods inj :: OutputLn a -> BasicIO a
OutputLn :<: Pure Source # |
Instance details Defined in System.Console.Wizard.Pure Methods inj :: OutputLn a -> Pure a
Run (InputT IO) OutputLn Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods runAlgebra :: OutputLn (InputT IO v) -> InputT IO v Source #
Run (State PureState) OutputLn Source # |
Instance details Defined in System.Console.Wizard.Pure Methods runAlgebra :: OutputLn (State PureState v) -> State PureState v Source #

outputLn :: OutputLn :<: b => String -> Wizard b () Source #

Output a string followed by a newline. Does not fail.

data ArbitraryIO w Source #

Instances

Functor ArbitraryIO Source # |
Instance details Defined in System.Console.Wizard.Internal Methods fmap :: (a -> b) -> ArbitraryIO a -> ArbitraryIO b Source # (<$) :: a -> ArbitraryIO b -> ArbitraryIO a Source #
Run IO ArbitraryIO Source # |
Instance details Defined in System.Console.Wizard.BasicIO Methods runAlgebra :: ArbitraryIO (IO v) -> IO v Source #
ArbitraryIO :<: Haskeline Source # |
Instance details Defined in System.Console.Wizard.Haskeline Methods inj :: ArbitraryIO a -> Haskeline a
ArbitraryIO :<: BasicIO Source # |
Instance details Defined in System.Console.Wizard.BasicIO Methods inj :: ArbitraryIO a -> BasicIO a
Run (InputT IO) ArbitraryIO Source # |
Instance details 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 :: 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 :: OutputLn :<: b => String -> Wizard b a -> Wizard b a Source #

Same as `retry`, except an error message can be specified.

defaultTo :: 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 :: Functor b => (a -> Maybe c) -> Wizard b a -> Wizard b c Source #

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

validator :: 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 :: Functor b => Wizard b [a] -> Wizard b [a] Source #

Simply `validator (not . null)`, makes a wizard fail if it gets an empty string.

inRange :: (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 :: (Read a, Functor b) => Wizard b String -> Wizard b a Source #

Simply `parser readP`. Attaches a simple `read` parser to a `Wizard`.

# Utility

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

readP :: Read a => String -> Maybe a Source #

A read-based parser for the `parser` modifier.

# Orphan instances

ArbitraryIO :<: b => MonadIO (Wizard b) Source # |
Instance details Methods liftIO :: IO a -> Wizard b a Source #