1
- module NOM.IO (interact , processTextStream , StreamParser , Stream , Window , Output ) where
1
+ module NOM.IO (interact , mainIOLoop , StreamParser , Stream , Window , Output ) where
2
2
3
3
import Control.Concurrent (threadDelay )
4
4
import Control.Concurrent.Async (Concurrently (Concurrently , runConcurrently ))
@@ -201,7 +201,7 @@ interact ::
201
201
state ->
202
202
IO state
203
203
interact 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
205
205
206
206
-- frame durations are passed to threadDelay and thus are given in microseconds
207
207
@@ -223,7 +223,7 @@ getKey = reverse <$> getKey' ""
223
223
more <- System.IO. hReady stdin
224
224
(if more then getKey' else return ) (char : chars)
225
225
226
- processTextStream ::
226
+ mainIOLoop ::
227
227
forall update state .
228
228
Config ->
229
229
StreamParser update ->
@@ -234,24 +234,23 @@ processTextStream ::
234
234
state ->
235
235
Stream (Either NOMError ByteString ) ->
236
236
IO state
237
- processTextStream config parser updater maintenance printerMay finalize initialState inputStream = do
237
+ mainIOLoop config parser updater maintenance printerMay finalize initialState inputStream = do
238
238
state_var <- newTMVarIO initialState
239
239
print_state_var <- newTMVarIO initPrintState
240
- input_received <- newEmptyTMVarIO
240
+ new_user_input <- newEmptyTMVarIO
241
241
output_builder_var <- newTVarIO []
242
242
refresh_display_var <- newTVarIO False
243
- let keepProcessing :: IO ()
244
- keepProcessing =
243
+ let keepProcessingNixCmd :: IO ()
244
+ keepProcessingNixCmd =
245
245
inputStream
246
246
& Stream. tap (errorsToBuilderFold output_builder_var)
247
247
& Stream. mapMaybe rightToMaybe
248
248
& parser
249
249
& Stream. fold (Fold. drainMapM (runUpdate output_builder_var state_var refresh_display_var updater))
250
250
waitForInput :: IO ()
251
251
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
255
254
let toggleHelp :: IO () = atomically $ do
256
255
print_state <- readTMVar print_state_var
257
256
writeTMVar print_state_var $ print_state{printHelp = not print_state. printHelp}
@@ -285,13 +284,15 @@ processTextStream config parser updater maintenance printerMay finalize initialS
285
284
_ -> writeStateToScreen (not config. silent) printedLinesVar state_var print_state_var output_builder_var refresh_display_var maintenance printer output_handle
286
285
keepPrinting :: IO ()
287
286
keepPrinting = forever do
287
+ -- Wait for either a Nix new input, the max frame duration (to update the timestamp), or a new input from the user.
288
288
runConcurrently
289
289
$ (Concurrently (threadDelay minFrameDuration) *> Concurrently waitForInput)
290
290
<|> Concurrently (threadDelay maxFrameDuration)
291
- <|> Concurrently (atomically $ takeTMVar input_received )
291
+ <|> Concurrently (atomically $ takeTMVar new_user_input )
292
292
writeToScreen
293
+ -- Actual main loop.
293
294
runConcurrently
294
- $ Concurrently keepProcessing
295
+ $ Concurrently keepProcessingNixCmd
295
296
<|> Concurrently keepProcessingStdin
296
297
<|> Concurrently keepPrinting
297
298
atomically (takeTMVar state_var) >>= execStateT finalize >>= atomically . putTMVar state_var
0 commit comments