bytestring-progress 1.0.2.1 → 1.0.3
raw patch · 3 files changed
+121/−1 lines, 3 filesdep +terminal-progress-bar
Dependencies added: terminal-progress-bar
Files
- Example.hs +30/−0
- System/ProgressBar/ByteString.hs +81/−0
- bytestring-progress.cabal +10/−1
Example.hs view
@@ -6,7 +6,11 @@ import Data.ByteString.Lazy.Progress (trackProgressString) import qualified Data.ByteString.Lazy as BS import System.IO (openBinaryFile, hClose, IOMode(..))+import System.IO (stderr) import System.Environment (getArgs)+import System.ProgressBar (msg,noLabel)+import System.ProgressBar.ByteString (mkByteStringProgressWriter)+import System.ProgressBar.ByteString (fileReadProgressWriter) downloadFile :: String -> FilePath -> IO () downloadFile url path = do@@ -27,6 +31,32 @@ formatStr = "\r Downloading file ... %p (%R, estimated done in %T)" handler = putStr +downloadFile' :: String -> FilePath -> IO ()+downloadFile' url path = do+ fhndl <- openBinaryFile path WriteMode+ http <- simpleHTTP dbReq+ case http of+ Left x -> fail $ "Couldn't download file: " ++ show x+ Right resp -> do+ let Just size = read `fmap` findHeader HdrContentLength resp+ putStrLn $ "Total size is " ++ show size ++ " bytes."+ mkByteStringProgressWriter (rspBody resp) stderr 72 (fromIntegral size)+ (msg "Downloading: ") noLabel+ >>= BS.hPut fhndl+ hClose fhndl+ putStrLn "Done!"+ where+ dbReq = mkRequest GET link+ Just link = parseURI url+ formatStr = "\r Downloading file ... %p (%R, estimated done in %T)"+ handler = putStr++ main :: IO () main = do+ downloadFile' "http://ftp.ndlug.nd.edu/pub/fedora/linux/releases/16/Fedora/x86_64/iso/Fedora-16-x86_64-netinst.iso" "foo.iso"+ bs <- fileReadProgressWriter "foo.iso" stderr 78 (msg "Checksum comp: ")+ noLabel+ let checksum = BS.foldl' (+) 0 bs+ putStrLn $ "Checksum: " ++ show checksum downloadFile "http://ftp.ndlug.nd.edu/pub/fedora/linux/releases/16/Fedora/x86_64/iso/Fedora-16-x86_64-netinst.iso" "foo.iso"
+ System/ProgressBar/ByteString.hs view
@@ -0,0 +1,81 @@+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++type ℤ = Integer++-- |Track the progress of a ByteString as it is consumed by some computation.+-- This is the most general version in the library, and will render a progress+-- string and pass it to the given function. See other functions for interacting+-- with fixed-size files, the console, or generic Handles.+mkByteStringProgressBar :: ByteString -> -- ^ The ByteString to track.+ (String -> IO ()) -> -- ^ Function to call on update.+ ℤ -> -- ^ Progress bar width+ ℤ -> -- ^ The size of the ByteString+ Label -> -- ^ Prefixed label+ Label -> -- ^ Postfixed 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 (fromIntegral now) size++-- |As mkByteStringProgressBar, but simply print the output to the given+-- Handle instead of using a callback.+mkByteStringProgressWriter :: ByteString -> -- ^ The ByteString to track.+ Handle -> -- ^ Handle to write to+ ℤ -> -- ^ Progress bar width+ ℤ -> -- ^ The size of the ByteString+ Label -> -- ^ Prefixed label+ Label -> -- ^ Postfixed 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++-- |Track the loading of a file as it is consumed by some computation. The+-- use of this function should be essentially similar to ByteString's+-- readFile, but with a lot more arguments and side effects.+fileReadProgressBar :: FilePath -> -- ^ The file to load.+ (String -> IO ()) -> -- ^ Function to call on update.+ ℤ -> -- ^ Progress bar width+ Label -> -- ^ Prefixed label+ Label -> -- ^ Postfixed 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++-- |As fileReadProgressBar, but simply write the progress bar to the given+-- Handle instead of calling a generic function.+fileReadProgressWriter :: FilePath -> -- ^ The file to load.+ Handle -> -- ^ Handle to write to+ ℤ -> -- ^ Progress bar width+ Label -> -- ^ Prefixed label+ Label -> -- ^ Postfixed 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+
bytestring-progress.cabal view
@@ -1,5 +1,5 @@ Name: bytestring-progress-Version: 1.0.2.1+Version: 1.0.3 Build-Type: Simple Cabal-Version: >= 1.6 License: BSD3@@ -18,11 +18,20 @@ data-files: Example.hs +Flag use-system-progressbar+ Description: Enable integration with the terminal-progress-bar library.+ Default: True+ Library Build-Depends: base >= 4.0 && < 5.0, time >= 1.1 && < 1.5, bytestring >= 0.9 && < 1.0 Exposed-Modules: Data.ByteString.Lazy.Progress++ if flag(use-system-progressbar)+ Build-Depends: terminal-progress-bar >= 0.0.1 && < 0.0.2+ Exposed-Modules: System.ProgressBar.ByteString+ source-repository head type: git