A peer-to-peer stack for code collaboration
Repl improvements
Julian K. Arni9584e5793cee
diff --git a/package.yaml b/package.yamlindex f64f604..ac17462 100644--- a/package.yaml+++ b/package.yaml@@ -16,6 +16,7 @@ dependencies:- mmorph- mtl- prettyprinter+- prettyprinter-ansi-terminal- scientific- syb- text@@ -63,6 +64,7 @@ executables:source-dirs: repldependencies:- radicle+ - optparse-applicativeghc-options: -Wall -threadeddefault-extensions:diff --git a/repl/Main.hs b/repl/Main.hsindex 94327bd..b3dd368 100644--- a/repl/Main.hs+++ b/repl/Main.hs@@ -1,14 +1,59 @@module Main (main) where-import System.Environment+import Data.Semigroup ((<>))import qualified Data.Text.IO as T-import Control.Concurrent+import Options.Applicative+import System.Environment-import Radicle+import Radiclemain :: IO ()main = do- [file] <- getArgs- src <- T.readFile file- _ <- forkIO $ repl src- threadDelay maxBound+ opts' <- execParser allOpts+ cfgSrc <- T.readFile =<< case configFile opts' of+ Nothing -> getConfig+ Just cfg -> pure cfg+ hist <- case histFile opts' of+ Nothing -> getHistory+ Just h -> pure h+ repl hist cfgSrc+ where+ allOpts = info (opts <**> helper)+ ( fullDesc+ <> progDesc "Run the radicle REPL"+ <> header "rad - The radicle REPL"+ )++-- | Uses XDG_CONFIG_HOME if available.+getConfig :: IO FilePath+getConfig = do+ mCfgHome <- lookupEnv "XDG_CONFIG_HOME"+ pure $ case mCfgHome of+ Nothing -> "~/.config/rad/config.rad"+ Just cfgHome -> cfgHome <> "/rad/config.rad"++-- | Uses XDG_CACHE_HOME if available.+getHistory :: IO FilePath+getHistory = do+ mCfgHome <- lookupEnv "XDG_CACHE_HOME"+ pure $ case mCfgHome of+ Nothing -> "/home/jkarni/.cache/rad/history"+ Just cfgHome -> cfgHome <> "/rad/history"++data Opts = Opts+ { configFile :: Maybe FilePath+ , histFile :: Maybe FilePath+ }++opts :: Parser Opts+opts = Opts+ <$> optional (strOption+ ( long "config"+ <> metavar "FILE"+ <> help "rad configuration file"+ ))+ <*> optional (strOption+ ( long "histfile"+ <> metavar "FILE"+ <> help "repl history file"+ ))diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex 07c539a..d4973d6 100644--- a/src/Radicle/Internal/Core.hs+++ b/src/Radicle/Internal/Core.hs@@ -32,6 +32,7 @@ data LangError =| WrongNumberOfArgs Text Int Int| OtherError Text| ParseError (Par.ParseError Char Void)+ | Exitderiving (Eq, Show, Read, Generic)diff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hsindex d0a0511..ef83bd5 100644--- a/src/Radicle/Internal/Pretty.hs+++ b/src/Radicle/Internal/Pretty.hs@@ -6,6 +6,7 @@ import Data.Text (Text)import qualified Data.Text as Timport Data.Text.Prettyprint.Docimport Data.Text.Prettyprint.Doc.Render.Text+import Text.Megaparsec.Error (parseErrorPretty)import Radicle.Internal.Core@@ -32,6 +33,19 @@ instance Pretty Value wherewhereescapeStr = T.replace "\"" "\\\"" . T.replace "\\" "\\\\"+instance Pretty LangError where+ pretty v = case v of+ UnknownIdentifier i -> "Unknown identifier:" <+> pretty i+ Impossible t -> "This cannot be!" <+> pretty t+ TypeError t -> "Type error:" <+> pretty t+ WrongNumberOfArgs t x y -> "Wrong number of args in" <+> pretty t+ <+> "Expected:" <+> pretty x+ <+> "Got:" <+> pretty y+ OtherError t -> "Error:" <+> pretty t+ ParseError t -> "Parser error:" <+> pretty (parseErrorPretty t)+ Exit -> "Exit"++-- | A fast and compact layout. Primarily intended for testing.---- Examples:diff --git a/src/Radicle/Internal/Subscriber.hs b/src/Radicle/Internal/Subscriber.hsindex 35e2293..1be67a2 100644--- a/src/Radicle/Internal/Subscriber.hs+++ b/src/Radicle/Internal/Subscriber.hs@@ -10,9 +10,12 @@ import qualified Data.Map as Mapimport Data.Monoid ((<>))import Data.Text (Text)import qualified Data.Text as T+import Data.Text.Prettyprint.Doc (pretty)+import Data.Text.Prettyprint.Doc.Render.Terminal (putDoc)import System.Console.Haskeline (CompletionFunc, InputT, completeWord,- defaultSettings, runInputT,- setComplete, simpleCompletion)+ defaultSettings, historyFile,+ runInputT, setComplete,+ simpleCompletion)import Radicle.Internal.Coreimport Radicle.Internal.Parse@@ -22,13 +25,17 @@ import Radicle.Internal.Subscriber.Capabilitiestype ReplM m = ( Monad m, Stdout (Lang m), Stdin (Lang m), GetEnv (Lang m), SetEnv (Lang m))-repl :: Text -> IO ()-repl preCode = do- let settings = setComplete completion defaultSettings+repl :: FilePath -> Text -> IO ()+repl histFile preCode = do+ let settings = setComplete completion+ $ defaultSettings { historyFile = Just histFile }r <- runInputT settings$ runLang replBindings$ interpretMany "[pre]" preCode- print r+ case r of+ Left Exit -> return ()+ Left e -> putDoc $ pretty e+ Right v -> putDoc $ pretty vcompletion :: Monad m => CompletionFunc mcompletion = completeWord Nothing ['(', ')', ' ', '\n'] go