2022-05-18 18:10:10 +03:00
|
|
|
module App where
|
|
|
|
|
|
|
|
import GlobalOptions
|
|
|
|
import MiniJuvix.Pipeline
|
|
|
|
import MiniJuvix.Prelude hiding (Doc)
|
2022-05-26 18:52:08 +03:00
|
|
|
import MiniJuvix.Prelude.Error qualified as Error
|
2022-05-18 18:10:10 +03:00
|
|
|
import MiniJuvix.Prelude.Pretty hiding (Doc)
|
|
|
|
import System.Console.ANSI qualified as Ansi
|
|
|
|
|
|
|
|
data App m a where
|
2022-05-26 18:52:08 +03:00
|
|
|
ExitError :: MiniJuvixError -> App m a
|
2022-05-18 18:10:10 +03:00
|
|
|
Say :: Text -> App m ()
|
|
|
|
RenderStdOut :: (HasAnsiBackend a, HasTextBackend a) => a -> App m ()
|
2022-05-26 18:52:08 +03:00
|
|
|
RunPipelineEither :: Sem PipelineEff a -> App m (Either MiniJuvixError a)
|
2022-05-18 18:10:10 +03:00
|
|
|
ReadGlobalOptions :: App m GlobalOptions
|
|
|
|
|
|
|
|
makeSem ''App
|
|
|
|
|
|
|
|
runAppIO :: forall r a. Member (Embed IO) r => GlobalOptions -> Sem (App ': r) a -> Sem r a
|
|
|
|
runAppIO g = interpret $ \case
|
|
|
|
RenderStdOut t
|
|
|
|
| g ^. globalOnlyErrors -> return ()
|
|
|
|
| otherwise -> embed $ do
|
|
|
|
sup <- Ansi.hSupportsANSI stdout
|
|
|
|
renderIO (not (g ^. globalNoColors) && sup) t
|
|
|
|
ReadGlobalOptions -> return g
|
|
|
|
RunPipelineEither p -> embed (runIOEither p)
|
|
|
|
Say t
|
|
|
|
| g ^. globalOnlyErrors -> return ()
|
|
|
|
| otherwise -> embed (putStrLn t)
|
|
|
|
ExitError e -> do
|
2022-05-26 18:52:08 +03:00
|
|
|
(embed . hPutStrLn stderr . Error.render (not (g ^. globalNoColors))) e
|
2022-05-18 18:10:10 +03:00
|
|
|
embed exitFailure
|
|
|
|
|
|
|
|
runPipeline :: Member App r => Sem PipelineEff a -> Sem r a
|
|
|
|
runPipeline p = do
|
|
|
|
r <- runPipelineEither p
|
|
|
|
case r of
|
|
|
|
Left err -> exitError err
|
|
|
|
Right res -> return res
|
|
|
|
|
|
|
|
newline :: Member App r => Sem r ()
|
|
|
|
newline = say ""
|