repl: add welcome text and :help command

This commit is contained in:
Domen Kožar 2018-11-24 20:17:45 +00:00
parent b0f346fc5a
commit c450af4dcd
No known key found for this signature in database
GPG key ID: C2FFBCAFD2C24246
2 changed files with 27 additions and 32 deletions

View file

@ -58,7 +58,7 @@ main = do
mapM_ (processFile opts) mapM_ (processFile opts)
=<< (lines <$> liftIO (readFile path)) =<< (lines <$> liftIO (readFile path))
Nothing -> case filePaths opts of Nothing -> case filePaths opts of
[] -> withNixContext Nothing $ Repl.shell (pure ()) [] -> withNixContext Nothing $ Repl.main
["-"] -> ["-"] ->
handleResult opts Nothing . parseNixTextLoc handleResult opts Nothing . parseNixTextLoc
=<< liftIO Text.getContents =<< liftIO Text.getContents
@ -94,7 +94,7 @@ main = do
=<< renderFrames @(NThunk (Lazy IO)) frames =<< renderFrames @(NThunk (Lazy IO)) frames
when (repl opts) $ when (repl opts) $
withNixContext Nothing $ Repl.shell (pure ()) withNixContext Nothing $ Repl.main
process opts mpath expr process opts mpath expr
| evaluate opts, tracing opts = | evaluate opts, tracing opts =

View file

@ -37,6 +37,8 @@ import Data.Monoid
import Data.Text (unpack, pack) import Data.Text (unpack, pack)
import qualified Data.Text as Text import qualified Data.Text as Text
import qualified Data.Text.IO as Text import qualified Data.Text.IO as Text
import Data.Version (showVersion)
import Paths_hnix (version)
import Control.Monad.Catch import Control.Monad.Catch
import Control.Monad.Identity import Control.Monad.Identity
@ -48,6 +50,18 @@ import System.Console.Repline
import System.Environment import System.Environment
import System.Exit import System.Exit
main :: (MonadNix e m, MonadIO m, MonadException m) => m ()
main = flip evalStateT initState $
#if MIN_VERSION_repline(0, 2, 0)
evalRepl (return prefix) cmd options (Just ':') completer init
#else
evalRepl prefix cmd options completer welcomeText
#endif
where
prefix = "hnix> "
welcomeText = liftIO $ putStrLn $ "Welcome to hnix " <> showVersion version <> ". For help type :help\n"
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
-- Types -- Types
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@ -131,11 +145,12 @@ load args = do
typeof :: (MonadNix e m, MonadException m, MonadIO m) => [String] -> Repl e m () typeof :: (MonadNix e m, MonadException m, MonadIO m) => [String] -> Repl e m ()
typeof args = do typeof args = do
st <- get st <- get
let arg = unwords args val <- case M.lookup line (tmctx st) of
val <- case M.lookup (Text.pack arg) (tmctx st) of
Just val -> return val Just val -> return val
Nothing -> exec False (Text.pack arg) Nothing -> exec False line
liftIO $ putStrLn $ describeValue $ valueType (_baseValue val) liftIO $ putStrLn $ describeValue $ valueType (_baseValue val)
where
line = Text.pack (unwords args)
-- :quit command -- :quit command
quit :: (MonadNix e m, MonadIO m) => a -> Repl e m () quit :: (MonadNix e m, MonadIO m) => a -> Repl e m ()
@ -164,37 +179,17 @@ options :: (MonadNix e m, MonadIO m, MonadException m)
=> [(String, [String] -> Repl e m ())] => [(String, [String] -> Repl e m ())]
options = [ options = [
("load" , load) ("load" , load)
, ("browse" , browse) --, ("browse" , browse)
, ("quit" , quit) , ("quit" , quit)
, ("type" , typeof) , ("type" , typeof)
, ("help" , help)
] ]
------------------------------------------------------------------------------- help :: forall e m . (MonadNix e m, MonadIO m, MonadException m)
-- Entry Point => [String] -> Repl e m ()
------------------------------------------------------------------------------- help _ = liftIO $ do
putStrLn "Available commands:\n"
mapM_ putStrLn $ map fst (options @e @m)
completer :: (MonadNix e m, MonadIO m) => CompleterStyle (StateT (IState m) m) completer :: (MonadNix e m, MonadIO m) => CompleterStyle (StateT (IState m) m)
completer = Prefix (wordCompleter comp) defaultMatcher completer = Prefix (wordCompleter comp) defaultMatcher
shell :: (MonadNix e m, MonadIO m, MonadException m) => Repl e m a -> m ()
shell pre = flip evalStateT initState $
#if MIN_VERSION_repline(0, 2, 0)
evalRepl (return prefix) cmd options (Just ':') completer pre
#else
evalRepl prefix cmd options completer pre
#endif
where
prefix = "hnix> "
-------------------------------------------------------------------------------
-- Toplevel
-------------------------------------------------------------------------------
-- main :: IO ()
-- main = do
-- args <- getArgs
-- case args of
-- [] -> shell (return ())
-- [fname] -> shell (load [fname])
-- ["test", fname] -> shell (load [fname] >> browse [] >> quit ())
-- _ -> putStrLn "invalid arguments"