mirror of
https://github.com/joshuaclayton/unused.git
synced 2024-09-11 04:45:28 +03:00
Extract ProgressBar to have a similar interface to Spinner
This commit is contained in:
parent
0f4e056641
commit
a924cb99f3
33
src/Unused/CLI/ProgressBar.hs
Normal file
33
src/Unused/CLI/ProgressBar.hs
Normal file
@ -0,0 +1,33 @@
|
||||
module Unused.CLI.ProgressBar
|
||||
( ProgressBar
|
||||
, startProgressBar
|
||||
, incrementProgressBar
|
||||
, stopProgressBar
|
||||
) where
|
||||
|
||||
import Control.Concurrent (ThreadId, killThread)
|
||||
import System.ProgressBar (ProgressRef, startProgress, incProgress, msg, percentage)
|
||||
|
||||
data ProgressBar = ProgressBar
|
||||
{ pbProgressRef :: ProgressRef
|
||||
, pbThreadId :: ThreadId
|
||||
}
|
||||
|
||||
startProgressBar :: Int -> IO ProgressBar
|
||||
startProgressBar i = do
|
||||
(ref, tid) <- buildProgressBar $ toInteger i
|
||||
return $ ProgressBar ref tid
|
||||
|
||||
incrementProgressBar :: ProgressBar -> IO ()
|
||||
incrementProgressBar ProgressBar{ pbProgressRef = ref } =
|
||||
incProgress ref 1
|
||||
|
||||
stopProgressBar :: ProgressBar -> IO ()
|
||||
stopProgressBar ProgressBar { pbThreadId = tid } = killThread tid
|
||||
|
||||
buildProgressBar :: Integer -> IO (ProgressRef, ThreadId)
|
||||
buildProgressBar =
|
||||
startProgress (msg message) percentage progressBarWidth
|
||||
where
|
||||
message = "Working"
|
||||
progressBarWidth = 60
|
@ -2,23 +2,15 @@ module Unused.CLI.SearchWithProgress
|
||||
( searchWithProgressBar
|
||||
) where
|
||||
|
||||
import Control.Concurrent (ThreadId, killThread)
|
||||
import System.ProgressBar (ProgressRef, startProgress, incProgress, msg, percentage)
|
||||
import Unused.CLI.ProgressBar (ProgressBar, startProgressBar, incrementProgressBar, stopProgressBar)
|
||||
import Unused.TermSearch (search)
|
||||
|
||||
searchWithProgressBar :: [String] -> IO [String]
|
||||
searchWithProgressBar terms = do
|
||||
putStr "\n\n"
|
||||
(bar, tid) <- buildProgressBar $ toInteger $ length terms
|
||||
concat <$> mapM (performSearch bar) terms <* killThread tid
|
||||
bar <- startProgressBar $ length terms
|
||||
concat <$> mapM (performSearch bar) terms <* stopProgressBar bar
|
||||
|
||||
performSearch :: ProgressRef -> String -> IO [String]
|
||||
performSearch ref t =
|
||||
search t <* incProgress ref 1
|
||||
|
||||
buildProgressBar :: Integer -> IO (ProgressRef, ThreadId)
|
||||
buildProgressBar =
|
||||
startProgress (msg message) percentage progressBarWidth
|
||||
where
|
||||
message = "Working"
|
||||
progressBarWidth = 60
|
||||
performSearch :: ProgressBar -> String -> IO [String]
|
||||
performSearch bar t =
|
||||
search t <* incrementProgressBar bar
|
||||
|
@ -28,6 +28,7 @@ library
|
||||
, Unused.CLI.SearchWithoutProgress
|
||||
, Unused.CLI.Util
|
||||
, Unused.CLI.Spinner
|
||||
, Unused.CLI.ProgressBar
|
||||
build-depends: base >= 4.7 && < 5
|
||||
, process
|
||||
, parsec
|
||||
|
Loading…
Reference in New Issue
Block a user