module Util where

import System.IO
import System.IO.Unsafe

countIO :: Int -> String -> [a] -> IO [a]
countIO step msg xs = sequence $ map unsafeInterleaveIO $ ct 0 xs
   where ct s xs' = let (a,b) = splitAt (step-1) xs'
                        next  = s+step
                        output c = hPutStr stderr ((take (length (show c)+2+length msg) $ repeat ' ')++'\r':msg++show c) >> hFlush stderr
                   in if null b then map return a ++ []
                      else map return a ++ [output next >> return (head b)]
                           ++ ct next (tail b)
