From d5bc4db97129f3d4b1aa2724e6b6ce5bdca69599 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Wed, 16 Sep 2020 22:09:30 +0200 Subject: [PATCH] Add a verbosity flag to silence the repl --- lib/Fregot/Main/GlobalOptions.hs | 25 ++++++++++++++++++++++--- lib/Fregot/Main/Repl.hs | 3 ++- lib/Fregot/Repl.hs | 13 ++++++++++--- tests/golden/repl/silent.goldplate | 10 ++++++++++ tests/golden/repl/silent.stderr | 2 ++ tests/golden/repl/silent.stdout | 1 + 6 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 tests/golden/repl/silent.goldplate create mode 100644 tests/golden/repl/silent.stderr create mode 100644 tests/golden/repl/silent.stdout diff --git a/lib/Fregot/Main/GlobalOptions.hs b/lib/Fregot/Main/GlobalOptions.hs index deb45a20..020409df 100644 --- a/lib/Fregot/Main/GlobalOptions.hs +++ b/lib/Fregot/Main/GlobalOptions.hs @@ -2,7 +2,11 @@ module Fregot.Main.GlobalOptions ( Format (..) - , GlobalOptions (..), dumpTags, format + , Verbosity + , silentVerbosity + , defaultVerbosity + + , GlobalOptions (..), dumpTags, format, verbosity , parseGlobalOptions , inputPath @@ -18,9 +22,18 @@ data Format | Json deriving (Bounded, Enum, Show) +newtype Verbosity = Verbosity Int deriving (Eq, Ord, Read, Show) + +silentVerbosity :: Verbosity +silentVerbosity = Verbosity 0 + +defaultVerbosity :: Verbosity +defaultVerbosity = Verbosity 1 + data GlobalOptions = GlobalOptions - { _dumpTags :: Dump.Tags - , _format :: Format + { _dumpTags :: Dump.Tags + , _format :: Format + , _verbosity :: Verbosity } deriving (Show) $(makeLenses ''GlobalOptions) @@ -42,6 +55,12 @@ parseGlobalOptions = GlobalOptions OA.metavar "FORMAT" <> OA.help "Format for error messages and diagnostics" <> OA.hidden) + <*> OA.option (Verbosity <$> OA.auto) ( + OA.value defaultVerbosity <> + OA.short 'v' <> + OA.long "verbosity" <> + OA.metavar "VERBOSITY" <> + OA.hidden) inputPath :: OA.Parser (Maybe FilePath) inputPath = OA.optional $ OA.strOption $ diff --git a/lib/Fregot/Main/Repl.hs b/lib/Fregot/Main/Repl.hs index 553a177a..ebc6ac6a 100644 --- a/lib/Fregot/Main/Repl.hs +++ b/lib/Fregot/Main/Repl.hs @@ -58,7 +58,8 @@ main gopts opts = do replConfig <- (\c -> if opts ^. noHistoryFile then c {Repl._historyFile = Nothing} - else c) <$> + else c) . + (\c -> c {Repl._verbosity = gopts ^. verbosity}) <$> Repl.defaultConfig FileWatch.withHandle (FileWatch.Config (opts ^. watch)) $ \fileWatch -> diff --git a/lib/Fregot/Repl.hs b/lib/Fregot/Repl.hs index 9d8ab26b..f5e96732 100644 --- a/lib/Fregot/Repl.hs +++ b/lib/Fregot/Repl.hs @@ -45,6 +45,8 @@ import qualified Fregot.Error as Error import qualified Fregot.Error.Stack as Stack import qualified Fregot.Eval as Eval import qualified Fregot.Eval.Value as Eval +import Fregot.Main.GlobalOptions (Verbosity, defaultVerbosity, + silentVerbosity) import qualified Fregot.Interpreter as Interpreter import Fregot.Names import qualified Fregot.Parser as Parser @@ -81,7 +83,8 @@ data StepTo | StepOver Stack.StackTrace data Config = Config - { _historyFile :: !(Maybe FilePath) + { _verbosity :: !Verbosity + , _historyFile :: !(Maybe FilePath) , _resumeHistory :: !Int } deriving (Show) @@ -122,7 +125,7 @@ $(makeLenses ''MetaCommand) defaultConfig :: IO Config defaultConfig = do home <- Directory.getHomeDirectory - pure $ Config (Just $ home ".fregot.repl") 10 + pure $ Config defaultVerbosity (Just $ home ".fregot.repl") 10 withHandle :: Config @@ -358,12 +361,16 @@ run h = do more p0 line = case Multiline.feed p0 (T.pack line) of Multiline.Complete txt -> return $ Just txt Multiline.Partial p1 -> do - mbNextLine <- Hl.getInputLine " " + mbNextLine <- Hl.getInputLine $ + if h ^. config . verbosity == silentVerbosity + then "" + else " " case mbNextLine of Nothing -> return $ Just $ Multiline.finish p1 Just nextLine -> more p1 nextLine getPrompt :: Handle -> IO String +getPrompt h | h ^. config . verbosity == silentVerbosity = pure "" getPrompt h = do pkg <- readFocusedPackage h emode <- IORef.readIORef (h ^. mode) diff --git a/tests/golden/repl/silent.goldplate b/tests/golden/repl/silent.goldplate new file mode 100644 index 00000000..3d0fe890 --- /dev/null +++ b/tests/golden/repl/silent.goldplate @@ -0,0 +1,10 @@ +{ + "command": "fregot", + "arguments": ["repl", "--no-history-file", "-v0"], + "asserts": [ + {"exit_code": 0}, + {"stderr": "${GOLDPLATE_NAME}.stderr"}, + {"stdout": "${GOLDPLATE_NAME}.stdout"} + ], + "stdin": "1" +} diff --git a/tests/golden/repl/silent.stderr b/tests/golden/repl/silent.stderr new file mode 100644 index 00000000..a3f4ec69 --- /dev/null +++ b/tests/golden/repl/silent.stderr @@ -0,0 +1,2 @@ +F u g u e R E G O T o o l k i t +fregot v0.12.3 repl - use :help for usage info diff --git a/tests/golden/repl/silent.stdout b/tests/golden/repl/silent.stdout new file mode 100644 index 00000000..94e79e78 --- /dev/null +++ b/tests/golden/repl/silent.stdout @@ -0,0 +1 @@ += 1