From 61a0f767e483b4734ae6a7eee9cacc431edbbe7c Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Thu, 24 Oct 2024 21:28:27 +0200 Subject: [PATCH] WIP --- lib/Patat/Eval.hs | 5 +---- lib/Patat/Eval/Internal.hs | 14 ++++++++++++++ lib/Patat/Main.hs | 4 +--- lib/Patat/Presentation/Internal.hs | 2 +- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/Patat/Eval.hs b/lib/Patat/Eval.hs index cc36463..460d583 100644 --- a/lib/Patat/Eval.hs +++ b/lib/Patat/Eval.hs @@ -94,7 +94,7 @@ evalBlock evalBlock settings orig@(Pandoc.CodeBlock attr@(_, classes, _) txt) | [s@EvalSettings {..}] <- lookupSettings classes settings = do var <- state freshVar - tell $ HMS.singleton var $ EvalBlock s txt Nothing mempty + tell $ HMS.singleton var $ EvalBlock s attr txt Nothing mempty out <- liftIO $ unsafeInterleaveIO $ do EvalResult {..} <- evalCode s txt pure $ case erExitCode of @@ -175,14 +175,12 @@ startEval2 var writeOutput presentation = case HMS.lookup var evalBlocks of Nothing -> pure presentation Just EvalBlock {..} | Just _ <- ebAsync -> pure presentation Just eb@EvalBlock {..} -> do - appendFile "log.txt" $ "starting eval for var " ++ show var ++ "\n" let EvalSettings {..} = ebSettings let proc = (Process.shell $ T.unpack evalCommand) { Process.std_in = Process.CreatePipe , Process.std_out = Process.CreatePipe , Process.std_err = Process.CreatePipe } - appendFile "log.txt" $ "Creating process\n" (Just hIn, Just hOut, Just hErr, hProc) <- Process.createProcess proc async <- Async.async $ Async.withAsync (T.hPutStr hIn ebInput `finally` IO.hClose hIn) $ \_ -> @@ -197,7 +195,6 @@ startEval2 var writeOutput presentation = case HMS.lookup var evalBlocks of ExitFailure i -> evalCommand <> ": exit code " <> T.pack (show i) <> "\n" <> erStderr - appendFile "log.txt" $ "OUT: " ++ show out ++ "\n" writeOutput $ UpdateEvalBlock var out pure presentation { pEvalBlocks = HMS.insert var eb {ebAsync = Just async} evalBlocks diff --git a/lib/Patat/Eval/Internal.hs b/lib/Patat/Eval/Internal.hs index cbaf712..ccb158b 100644 --- a/lib/Patat/Eval/Internal.hs +++ b/lib/Patat/Eval/Internal.hs @@ -3,6 +3,8 @@ module Patat.Eval.Internal ( EvalBlocks , EvalBlock (..) + , evalBlockToBlocks + , EvalState (..) , Handle (..) , newHandle @@ -23,6 +25,7 @@ import Patat.Presentation.Settings import System.Exit (ExitCode (..)) import qualified System.IO as IO import qualified System.Process as Process +import qualified Text.Pandoc as Pandoc -------------------------------------------------------------------------------- @@ -33,12 +36,23 @@ type EvalBlocks = HMS.HashMap Var EvalBlock -- | Block that needs to be evaluated. data EvalBlock = EvalBlock { ebSettings :: !EvalSettings + , ebAttr :: !Pandoc.Attr , ebInput :: !T.Text , ebAsync :: !(Maybe (Async.Async ())) , ebOutput :: !T.Text } +-------------------------------------------------------------------------------- +evalBlockToBlocks :: EvalBlock -> [Pandoc.Block] +evalBlockToBlocks EvalBlock {..} = case evalContainer ebSettings of + EvalContainerCode -> [Pandoc.CodeBlock ebAttr ebOutput] + EvalContainerNone -> [Pandoc.RawBlock fmt ebOutput] + EvalContainerInline -> [Pandoc.Plain [Pandoc.RawInline fmt ebOutput]] + where + fmt = "eval" + + -------------------------------------------------------------------------------- data EvalState = Starting diff --git a/lib/Patat/Main.hs b/lib/Patat/Main.hs index 88ab148..e219afc 100644 --- a/lib/Patat/Main.hs +++ b/lib/Patat/Main.hs @@ -212,10 +212,8 @@ loop app@App {..} = do (activeSpeakerNotes aPresentation) -- Start necessary eval blocks - appendFile "log.txt" "lol test\n" presentation <- foldM - (\presentation var -> do - appendFile "log.txt" ("starting var: " ++ show var ++ "\n") + (\presentation var -> Eval.startEval2 var (\u -> Chan.writeChan aCommandChan (PresentationCommand $ UpdateEvalBlock u)) presentation) aPresentation (activeVars aPresentation) diff --git a/lib/Patat/Presentation/Internal.hs b/lib/Patat/Presentation/Internal.hs index cfc0720..7b87e9e 100644 --- a/lib/Patat/Presentation/Internal.hs +++ b/lib/Patat/Presentation/Internal.hs @@ -155,7 +155,7 @@ activeFragment presentation = do where resolve var = case HMS.lookup var (pEvalBlocks presentation) of Nothing -> [] - Just eb -> [Pandoc.Para [Pandoc.Str "out: ", Pandoc.Str $ Eval.ebOutput eb]] + Just eb -> Eval.evalBlockToBlocks eb --------------------------------------------------------------------------------