Description
A user-friendly, dependently-typed library for asking your users questions.
Description
A library making use of the terminal package to prompt users for answers in a CLI context.
Supports freeform text as well as choices between sum type constructors.
README.md
prompt-hs
A user-friendly, dependently-typed library for asking your users questions
Table of Contents
Demo
Click to expand demo source
module Main (main) where
import Data.Proxy (Proxy (Proxy))
import Data.Text (pack, unpack)
import System.Prompt
( Chooseable (showChooseable),
Confirmation (DontConfirm, RequireConfirmation),
Requirement (Optional, Required),
promptChoice,
promptText,
)
data Colour = Red | Green | Blue deriving (Bounded, Enum, Eq, Show)
instance Chooseable Colour where
showChooseable = pack . show
main :: IO ()
main = do
name <- promptText Required RequireConfirmation $ pack "What is your name?"
favouriteColour <- promptChoice Optional DontConfirm (Proxy :: Proxy Colour) $ pack "And what is your favourite colour?"
putStrLn $
"Your name is " <> unpack name <> " and " <> case favouriteColour of
Just c -> "your favourite colour is " <> show c
Nothing -> "you didn't tell me your favourite colour."
Usage
- Produce an instance of
Chooseablefor any sum types you want to be able to choose from. For types withBoundedandEnuminstances, all you need to provide is how to display the options. - Choose a type of prompt: use
promptTextfor freeform text orpromptChoice(orpromptChoiceFromSetfor more flexible options) to get the user to choose one of many options. Requirement: is an answer needed, or can the user skip the question? Can beRequiredorOptional. 4:Confirmation: IfRequireConfirmation, get the user to confirm their answers after selecting/typing. Otherwise, accept it immediately and move on.- Give your prompt text.
Note: For Optional questions, the returned value is wrapped in Maybe. For freeform answers, a Just value returned from an Optional question will never be empty.
Development Environment
A development environment is provided:
- Install Nix
- Run
nix develop
Building for Production
To produce a production build as defined in nix/build.nix, run nix build.
This will produce a result directory with built artefacts.