Return ThreadId with ProgressIndicator

Why?
====

It's common to return a two-tuple of `(ThreadId, a)` when performing a
forking operation to provide a handle to the thread.
This commit is contained in:
Joshua Clayton 2016-05-16 06:06:51 -04:00
parent 8c5e94c862
commit 85df4ae01f
2 changed files with 4 additions and 4 deletions

View File

@ -23,7 +23,7 @@ createSpinner =
progressWithIndicator :: (a -> IO [b]) -> ProgressIndicator -> [a] -> IO [b] progressWithIndicator :: (a -> IO [b]) -> ProgressIndicator -> [a] -> IO [b]
progressWithIndicator f i terms = do progressWithIndicator f i terms = do
printPrefix i printPrefix i
indicator <- start i $ length terms (_, indicator) <- start i $ length terms
concat <$> parallel (ioOps indicator) <* stop indicator <* stopGlobalPool concat <$> parallel (ioOps indicator) <* stop indicator <* stopGlobalPool
where where
ioOps i' = map (\t -> f t <* increment i') terms ioOps i' = map (\t -> f t <* increment i') terms

View File

@ -11,13 +11,13 @@ import System.ProgressBar (ProgressRef, startProgress, incProgress, msg, percent
import Unused.CLI.ProgressIndicator.Types import Unused.CLI.ProgressIndicator.Types
import Unused.CLI.Util import Unused.CLI.Util
start :: ProgressIndicator -> Int -> IO ProgressIndicator start :: ProgressIndicator -> Int -> IO (ThreadId, ProgressIndicator)
start s@Spinner{} _ = do start s@Spinner{} _ = do
tid <- forkIO $ runSpinner 0 s tid <- forkIO $ runSpinner 0 s
return $ s { sThreadId = Just tid } return (tid, s { sThreadId = Just tid })
start ProgressBar{} i = do start ProgressBar{} i = do
(ref, tid) <- buildProgressBar $ toInteger i (ref, tid) <- buildProgressBar $ toInteger i
return $ ProgressBar (Just ref) (Just tid) return (tid, ProgressBar (Just ref) (Just tid))
stop :: ProgressIndicator -> IO () stop :: ProgressIndicator -> IO ()
stop ProgressBar{ pbThreadId = Just tid } = killThread tid stop ProgressBar{ pbThreadId = Just tid } = killThread tid