:help command in repl (#1237)

* :help command in repl (#1236)

* review feedback
This commit is contained in:
Eric Conlon 2019-08-18 12:42:56 -07:00 committed by mergify[bot]
parent 8c3db821db
commit 96c94ddfc4
2 changed files with 84 additions and 14 deletions

4
.gitignore vendored
View File

@ -20,4 +20,6 @@ cabal.project.local
cabal.project.local~
.HTF/
.ghc.environment.*
.cache
.cache
.history
stack.yaml.lock

View File

@ -23,6 +23,7 @@ import Data.List.NonEmpty (NonEmpty(..))
import Data.Maybe ( mapMaybe )
import Data.Semigroup ((<>))
import Data.Text ( Text )
import Data.Version (showVersion)
import Dhall.Context (Context)
import Dhall.Import (hashExpressionToCode)
import Dhall.Pretty (CharacterSet(..))
@ -48,6 +49,7 @@ import qualified Dhall.Import as Dhall
import qualified Dhall.Map as Map
import qualified Dhall.Parser as Dhall
import qualified Dhall.TypeCheck as Dhall
import qualified Paths_dhall as Meta
import qualified System.Console.ANSI
import qualified System.Console.Haskeline.Completion as Haskeline
import qualified System.Console.Haskeline.MonadException as Haskeline
@ -398,26 +400,90 @@ cmdQuit _ = do
liftIO (putStrLn "Goodbye.")
liftIO (throwIO Interrupt)
help
:: ( Haskeline.MonadException m, MonadFail m, MonadIO m, MonadState Env m )
=> HelpOptions m -> [String] -> m ()
help hs _ = do
liftIO (putStrLn "Type any expression to normalize it or use one of the following commands:")
forM_ hs $ \h -> do
let name = helpOptionName h
syntax = helpOptionSyntax h
doc = helpOptionDoc h
liftIO (putStrLn (":" <> name <> " " <> syntax))
liftIO (putStrLn (" " <> doc))
optionsPrefix :: Char
optionsPrefix = ':'
data HelpOption m = HelpOption
{ helpOptionName :: String
, helpOptionSyntax :: String
, helpOptionDoc :: String
, helpOptionFunction :: Repline.Cmd m
}
type HelpOptions m = [HelpOption m]
helpOptions
:: ( Haskeline.MonadException m, MonadFail m, MonadIO m, MonadState Env m )
=> HelpOptions m
helpOptions =
[ HelpOption
"help"
""
"Print help text and describe options"
(dontCrash . help helpOptions)
, HelpOption
"type"
"EXPRESSION"
"Infer the type of an expression"
(dontCrash . typeOf)
, HelpOption
"hash"
"EXPRESSION"
"Hash the normalized value of an expression"
(dontCrash . hashBinding)
, HelpOption
"let"
"IDENTIFIER = EXPRESSION"
"Assign an expression to a variable"
(dontCrash . addBinding . separateEqual)
, HelpOption
"clear"
""
"Clear all bound variables"
(dontCrash . clearBindings)
, HelpOption
"load"
"[FILENAME]"
"Load bound variables from a file"
(dontCrash . loadBinding)
, HelpOption
"save"
"[FILENAME | FILENAME = EXPRESSION]"
"Save bound variables or a given expression to a file"
(dontCrash . saveBinding . separateEqual)
, HelpOption
"set"
"OPTION"
"Set an option. Currently supported: --explain"
(dontCrash . setOption)
, HelpOption
"unset"
"OPTION"
"Unset an option"
(dontCrash . unsetOption)
, HelpOption
"quit"
""
"Exit the REPL"
cmdQuit
]
options
:: ( Haskeline.MonadException m, MonadFail m, MonadIO m, MonadState Env m )
=> Repline.Options m
options =
[ ( "type", dontCrash . typeOf )
, ( "hash", dontCrash . hashBinding )
, ( "let", dontCrash . addBinding . separateEqual )
, ( "clear", dontCrash . clearBindings)
, ( "load", dontCrash . loadBinding )
, ( "save", dontCrash . saveBinding . separateEqual )
, ( "set", dontCrash . setOption)
, ( "unset", dontCrash . unsetOption)
, ( "quit", cmdQuit )
]
options = (\h -> (helpOptionName h, helpOptionFunction h)) <$> helpOptions
completer
:: (Monad m, MonadFail m, MonadIO m, MonadState Env m)
@ -507,7 +573,9 @@ completeFunc reversedPrev word
greeter :: MonadIO m => m ()
greeter =
return ()
let version = showVersion Meta.version
message = "Welcome to the Dhall v" <> version <> " REPL! Type :help for more information."
in liftIO (putStrLn message)
dontCrash :: ( MonadIO m, Haskeline.MonadException m ) => m () -> m ()