module System.ProgressBar.ByteString(
mkByteStringProgressBar
, mkByteStringProgressWriter
, fileReadProgressBar
, fileReadProgressWriter
)
where
import Data.ByteString.Lazy(ByteString,hGetContents)
import Data.ByteString.Lazy.Progress
import System.IO(Handle,hSetBuffering,hPutChar,hPutStr,BufferMode(..))
import System.IO(openFile,hFileSize,IOMode(..))
import System.ProgressBar(Label, Progress(Progress), mkProgressBar)
type ℤ = Integer
mkByteStringProgressBar :: ByteString ->
(String -> IO ()) ->
ℤ ->
ℤ ->
Label ->
Label ->
IO ByteString
mkByteStringProgressBar input tracker width size prefix postfix =
trackProgressWithChunkSize bestSize updateFunction input
where
bestSize | size `div` 100 < 4096 = fromIntegral $ size `div` 100
| size `div` 100 < 16384 = 4096
| otherwise = 16384
updateProgressBar = mkProgressBar prefix postfix width
updateFunction _ now =
tracker $ updateProgressBar (Progress (fromIntegral now) size)
mkByteStringProgressWriter :: ByteString ->
Handle ->
ℤ ->
ℤ ->
Label ->
Label ->
IO ByteString
mkByteStringProgressWriter input handle width size prefix postfix = do
hSetBuffering handle NoBuffering
mkByteStringProgressBar input tracker width size prefix postfix
where
tracker str = hPutChar handle '\r' >> hPutStr handle str
fileReadProgressBar :: FilePath ->
(String -> IO ()) ->
ℤ ->
Label ->
Label ->
IO ByteString
fileReadProgressBar path tracker width prefix postfix = do
inHandle <- openFile path ReadMode
size <- hFileSize inHandle
bytestring <- hGetContents inHandle
mkByteStringProgressBar bytestring tracker width size prefix postfix
fileReadProgressWriter :: FilePath ->
Handle ->
ℤ ->
Label ->
Label ->
IO ByteString
fileReadProgressWriter path handle width prefix postfix = do
inHandle <- openFile path ReadMode
size <- hFileSize inHandle
bytestring <- hGetContents inHandle
mkByteStringProgressWriter bytestring handle width size prefix postfix