1- module NOM.IO (interact , processTextStream , StreamParser , Stream , Window , Output ) where
1+ module NOM.IO (interact , mainIOLoop , StreamParser , Stream , Window , Output ) where
22
33import Control.Concurrent (threadDelay )
44import Control.Concurrent.Async (Concurrently (Concurrently , runConcurrently ))
@@ -201,7 +201,7 @@ interact ::
201201 state ->
202202 IO state
203203interact config parser updater maintenance printer finalize input_stream output_handle initialState =
204- processTextStream config parser updater maintenance (Just (printer, output_handle)) finalize initialState input_stream
204+ mainIOLoop config parser updater maintenance (Just (printer, output_handle)) finalize initialState input_stream
205205
206206-- frame durations are passed to threadDelay and thus are given in microseconds
207207
@@ -223,7 +223,7 @@ getKey = reverse <$> getKey' ""
223223 more <- System.IO. hReady stdin
224224 (if more then getKey' else return ) (char : chars)
225225
226- processTextStream ::
226+ mainIOLoop ::
227227 forall update state .
228228 Config ->
229229 StreamParser update ->
@@ -234,24 +234,23 @@ processTextStream ::
234234 state ->
235235 Stream (Either NOMError ByteString ) ->
236236 IO state
237- processTextStream config parser updater maintenance printerMay finalize initialState inputStream = do
237+ mainIOLoop config parser updater maintenance printerMay finalize initialState inputStream = do
238238 state_var <- newTMVarIO initialState
239239 print_state_var <- newTMVarIO initPrintState
240- input_received <- newEmptyTMVarIO
240+ new_user_input <- newEmptyTMVarIO
241241 output_builder_var <- newTVarIO []
242242 refresh_display_var <- newTVarIO False
243- let keepProcessing :: IO ()
244- keepProcessing =
243+ let keepProcessingNixCmd :: IO ()
244+ keepProcessingNixCmd =
245245 inputStream
246246 & Stream. tap (errorsToBuilderFold output_builder_var)
247247 & Stream. mapMaybe rightToMaybe
248248 & parser
249249 & Stream. fold (Fold. drainMapM (runUpdate output_builder_var state_var refresh_display_var updater))
250250 waitForInput :: IO ()
251251 waitForInput = atomically $ check =<< readTVar refresh_display_var
252- printerMay & maybe keepProcessing \ (printer, output_handle) -> do
253- linesVar <- newTVarIO 0
254- let keepProcessingStdin :: IO ()
252+ printerMay & maybe keepProcessingNixCmd \ (printer, output_handle) -> do
253+ printedLinesVar <- newTVarIO 0
255254 let toggleHelp :: IO () = atomically $ do
256255 print_state <- readTMVar print_state_var
257256 writeTMVar print_state_var $ print_state{printHelp = not print_state. printHelp}
@@ -263,14 +262,14 @@ processTextStream config parser updater maintenance printerMay finalize initialS
263262 atomically $ do
264263 print_state <- readTMVar print_state_var
265264 let print_state_style = if print_state. printName == PrintName then PrintDerivationPath else PrintName
266- writeTMVar print_state_var $ print_state{printName = print_state_style}
265+ writeTMVar print_state_var $ print_state{printName = print_state_style, printHelp = False }
267266 writeTMVar new_user_input ()
268267 " ?" -> toggleHelp
269268 " h" -> toggleHelp
270269 " f" -> do
271270 atomically $ do
272271 print_state <- readTMVar print_state_var
273- writeTMVar print_state_var $ print_state{freeze = not print_state. freeze}
272+ writeTMVar print_state_var $ print_state{freeze = not print_state. freeze, printHelp = False }
274273 writeTMVar new_user_input ()
275274 _ -> pure ()
276275 writeToScreen :: IO ()
@@ -281,13 +280,15 @@ processTextStream config parser updater maintenance printerMay finalize initialS
281280 _ -> writeStateToScreen (not config. silent) printedLinesVar state_var print_state_var output_builder_var refresh_display_var maintenance printer output_handle
282281 keepPrinting :: IO ()
283282 keepPrinting = forever do
283+ -- Wait for either a Nix new input, the max frame duration (to update the timestamp), or a new input from the user.
284284 runConcurrently
285285 $ (Concurrently (threadDelay minFrameDuration) *> Concurrently waitForInput)
286286 <|> Concurrently (threadDelay maxFrameDuration)
287- <|> Concurrently (atomically $ takeTMVar input_received )
287+ <|> Concurrently (atomically $ takeTMVar new_user_input )
288288 writeToScreen
289+ -- Actual main loop.
289290 runConcurrently
290- $ Concurrently keepProcessing
291+ $ Concurrently keepProcessingNixCmd
291292 <|> Concurrently keepProcessingStdin
292293 <|> Concurrently keepPrinting
293294 atomically (takeTMVar state_var) >>= execStateT finalize >>= atomically . putTMVar state_var
0 commit comments