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}
@@ -263,14 +262,14 @@ processTextStream config parser updater maintenance printerMay finalize initialS
263
262
atomically $ do
264
263
print_state <- readTMVar print_state_var
265
264
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 }
267
266
writeTMVar new_user_input ()
268
267
" ?" -> toggleHelp
269
268
" h" -> toggleHelp
270
269
" f" -> do
271
270
atomically $ do
272
271
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 }
274
273
writeTMVar new_user_input ()
275
274
_ -> pure ()
276
275
writeToScreen :: IO ()
@@ -281,13 +280,15 @@ processTextStream config parser updater maintenance printerMay finalize initialS
281
280
_ -> writeStateToScreen (not config. silent) printedLinesVar state_var print_state_var output_builder_var refresh_display_var maintenance printer output_handle
282
281
keepPrinting :: IO ()
283
282
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.
284
284
runConcurrently
285
285
$ (Concurrently (threadDelay minFrameDuration) *> Concurrently waitForInput)
286
286
<|> Concurrently (threadDelay maxFrameDuration)
287
- <|> Concurrently (atomically $ takeTMVar input_received )
287
+ <|> Concurrently (atomically $ takeTMVar new_user_input )
288
288
writeToScreen
289
+ -- Actual main loop.
289
290
runConcurrently
290
- $ Concurrently keepProcessing
291
+ $ Concurrently keepProcessingNixCmd
291
292
<|> Concurrently keepProcessingStdin
292
293
<|> Concurrently keepPrinting
293
294
atomically (takeTMVar state_var) >>= execStateT finalize >>= atomically . putTMVar state_var
0 commit comments