Skip to content

Commit

Permalink
Add a verbosity flag to silence the repl
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj-luminal authored Sep 16, 2020
1 parent 2c2e16a commit d5bc4db
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
25 changes: 22 additions & 3 deletions lib/Fregot/Main/GlobalOptions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
module Fregot.Main.GlobalOptions
( Format (..)

, GlobalOptions (..), dumpTags, format
, Verbosity
, silentVerbosity
, defaultVerbosity

, GlobalOptions (..), dumpTags, format, verbosity
, parseGlobalOptions

, inputPath
Expand All @@ -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)
Expand All @@ -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 $
Expand Down
3 changes: 2 additions & 1 deletion lib/Fregot/Main/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
13 changes: 10 additions & 3 deletions lib/Fregot/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -81,7 +83,8 @@ data StepTo
| StepOver Stack.StackTrace

data Config = Config
{ _historyFile :: !(Maybe FilePath)
{ _verbosity :: !Verbosity
, _historyFile :: !(Maybe FilePath)
, _resumeHistory :: !Int
} deriving (Show)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions tests/golden/repl/silent.goldplate
Original file line number Diff line number Diff line change
@@ -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"
}
2 changes: 2 additions & 0 deletions tests/golden/repl/silent.stderr
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/golden/repl/silent.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= 1

0 comments on commit d5bc4db

Please sign in to comment.