From 20f53cc1cb01b51bfecc3896c69e881dc72e6e05 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Fri, 3 Jun 2016 12:53:10 +0200 Subject: support for questions --- src/Sequence/Utils.hs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src/Sequence') diff --git a/src/Sequence/Utils.hs b/src/Sequence/Utils.hs index aea853d..32f8239 100644 --- a/src/Sequence/Utils.hs +++ b/src/Sequence/Utils.hs @@ -1,8 +1,8 @@ -{-# LANGUAGE ViewPatterns, FlexibleContexts, MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, UndecidableInstances #-} +{-# LANGUAGE ViewPatterns, FlexibleContexts, MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, UndecidableInstances, OverloadedStrings #-} module Sequence.Utils ( withArg, withFocus - , askBool + , askQ, askBool , toName, fromName ) where @@ -10,6 +10,7 @@ import Sequence.Types import Control.Monad.State.Strict +import Control.Monad import Control.Lens import Data.Bimap (Bimap) @@ -32,6 +33,8 @@ import System.Console.Shell import System.Console.Shell.ShellMonad import System.Console.Shell.Backend.Haskeline +import System.Console.Readline (readline) + class Argument a st | a -> st where arg :: String -> Sh st (Maybe a) @@ -47,12 +50,17 @@ withFocus f = use gFocus >>= \focus -> case focus of Just id -> f id askBool :: String -> Bool -> Sh st Bool -askBool prompt initial = liftIO $ runShell (initialShellDescription { shellCommands = [cmd "y" yes "yes", cmd "n" no "no"], commandStyle = SingleCharCommands, prompt = const $ return prompt, historyEnabled = False }) haskelineBackend initial +askBool prompt initial = askQ prompt $ fromMaybe initial . join . fmap (eval . CI.mk) where - yes, no :: Sh Bool () - yes = put True - no = put False - + eval "yes" = Just True + eval "y" = Just True + eval "no" = Just False + eval "n" = Just False + eval _ = Nothing + +askQ :: String -> (Maybe String -> a) -> Sh st a +askQ prompt eval = eval <$> liftIO (readline $ prompt ++ " ") + unaligned = view faction' def toName :: MonadState GameState m => EntityIdentifier -> m String -- cgit v1.2.3