ascii-progress 0.1.1.0 → 0.2.0.0
raw patch · 3 files changed
+73/−4 lines, 3 filesdep +HTTPdep +bytestringdep +conduitnew-component:exe:download-examplePVP ok
version bump matches the API change (PVP)
Dependencies added: HTTP, bytestring, conduit, http-conduit, http-types, transformers
API changes (from Hackage documentation)
+ System.Console.AsciiProgress: complete :: ProgressBar -> IO ()
Files
- ascii-progress.cabal +30/−1
- bin/DownloadExample.hs +32/−0
- lib/System/Console/AsciiProgress.hs +11/−3
ascii-progress.cabal view
@@ -1,5 +1,5 @@ name: ascii-progress-version: 0.1.1.0+version: 0.2.0.0 synopsis: A simple progress bar for the console. description: A simple Haskell progress bar for the console. Heavily borrows from TJ@@ -18,6 +18,10 @@ build-type: Simple cabal-version: >=1.10 +flag examples+ description: Build the examples+ default: False+ library exposed-modules: System.Console.AsciiProgress , System.Console.AsciiProgress.Internal@@ -38,6 +42,31 @@ , time >= 1.4.2 hs-source-dirs: lib , bin+ if flag(examples)+ buildable: True+ else+ buildable: False+ default-language: Haskell2010++executable download-example+ main-is: DownloadExample.hs+ build-depends: HTTP+ , ansi-terminal+ , async >= 2.0.1.5+ , base >=4.7 && <4.8+ , bytestring+ , data-default >= 0.5.3+ , time >= 1.4.2+ , conduit >= 1.2+ , http-conduit >= 2.1+ , http-types >= 0.8+ , transformers >= 0.3+ hs-source-dirs: lib+ , bin+ if flag(examples)+ buildable: True+ else+ buildable: False default-language: Haskell2010 test-suite hspec
+ bin/DownloadExample.hs view
@@ -0,0 +1,32 @@+import Control.Monad.IO.Class (MonadIO, liftIO)+import qualified Data.ByteString as ByteString (length)+import Data.ByteString (ByteString)+import Data.ByteString.Char8 as ByteString (unpack)+import Data.Conduit (ConduitM, ($=+), ($$+-), await, yield)+import Data.Conduit.List (sinkNull)+import Network.HTTP.Conduit (http, parseUrl, responseBody, responseHeaders,+ withManager)+import Network.HTTP.Types (hContentLength)+import System.Console.AsciiProgress (ProgressBar, Options(..), complete, def,+ newProgressBar, tickN)++main :: IO ()+main = withManager $ \manager -> do+ -- Start the request+ req <- parseUrl "https://i.imgur.com/8CJGhZQ.gif"+ res <- http req manager+ -- Get the Content-Length and initialize the progress bar+ let Just cl = lookup hContentLength (responseHeaders res)+ pg <- liftIO $ newProgressBar def { pgTotal = read (ByteString.unpack cl) }+ -- Consume the response updating the progress bar+ responseBody res $=+ updateProgress pg $$+- sinkNull+ -- Force the progress bar to complete+ liftIO $ complete pg+ liftIO $ putStrLn "Done"++updateProgress :: MonadIO m => ProgressBar -> ConduitM ByteString ByteString m ()+updateProgress pg = await >>= maybe (return ()) (\chunk -> do+ let len = ByteString.length chunk+ liftIO $ tickN pg len+ yield chunk+ updateProgress pg)
lib/System/Console/AsciiProgress.hs view
@@ -5,6 +5,7 @@ , Stats(..) , isComplete , newProgressBar+ , complete , tick , tickN , getProgressStrIO@@ -16,9 +17,8 @@ where import Control.Applicative ((<$>))-import Control.Concurrent -- (Chan, MVar, modifyMVar, newChan, newEmptyMVar,- -- newMVar, readChan, readMVar, writeChan, modifyMVar_)-import Control.Concurrent.Async (Async, async, poll)+import Control.Concurrent (readChan, readMVar, writeChan, modifyMVar_)+import Control.Concurrent.Async (Async, async, poll, wait) import Data.Default (Default(..)) import Data.Maybe (isJust) import System.Console.ANSI (clearLine, setCursorColumn)@@ -75,6 +75,14 @@ -- ticks) isComplete :: ProgressBar -> IO Bool isComplete (ProgressBar _ future) = isJust <$> poll future++-- |+-- Forces a 'ProgressBar' to finish+complete :: ProgressBar -> IO ()+complete pg@(ProgressBar info future) = do+ let total = pgTotal (pgOptions info)+ tickN pg total+ wait future -- | -- Gets the progress bar current @Stats @object