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
|
( searchWithProgressBar
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Concurrent (ThreadId, killThread)
|
import Unused.CLI.ProgressBar (ProgressBar, startProgressBar, incrementProgressBar, stopProgressBar)
|
||||||
import System.ProgressBar (ProgressRef, startProgress, incProgress, msg, percentage)
|
|
||||||
import Unused.TermSearch (search)
|
import Unused.TermSearch (search)
|
||||||
|
|
||||||
searchWithProgressBar :: [String] -> IO [String]
|
searchWithProgressBar :: [String] -> IO [String]
|
||||||
searchWithProgressBar terms = do
|
searchWithProgressBar terms = do
|
||||||
putStr "\n\n"
|
putStr "\n\n"
|
||||||
(bar, tid) <- buildProgressBar $ toInteger $ length terms
|
bar <- startProgressBar $ length terms
|
||||||
concat <$> mapM (performSearch bar) terms <* killThread tid
|
concat <$> mapM (performSearch bar) terms <* stopProgressBar bar
|
||||||
|
|
||||||
performSearch :: ProgressRef -> String -> IO [String]
|
performSearch :: ProgressBar -> String -> IO [String]
|
||||||
performSearch ref t =
|
performSearch bar t =
|
||||||
search t <* incProgress ref 1
|
search t <* incrementProgressBar bar
|
||||||
|
|
||||||
buildProgressBar :: Integer -> IO (ProgressRef, ThreadId)
|
|
||||||
buildProgressBar =
|
|
||||||
startProgress (msg message) percentage progressBarWidth
|
|
||||||
where
|
|
||||||
message = "Working"
|
|
||||||
progressBarWidth = 60
|
|
||||||
|
@ -28,6 +28,7 @@ library
|
|||||||
, Unused.CLI.SearchWithoutProgress
|
, Unused.CLI.SearchWithoutProgress
|
||||||
, Unused.CLI.Util
|
, Unused.CLI.Util
|
||||||
, Unused.CLI.Spinner
|
, Unused.CLI.Spinner
|
||||||
|
, Unused.CLI.ProgressBar
|
||||||
build-depends: base >= 4.7 && < 5
|
build-depends: base >= 4.7 && < 5
|
||||||
, process
|
, process
|
||||||
, parsec
|
, parsec
|
||||||
|
Loading…
Reference in New Issue
Block a user