buildString :: String ->
               UTCTime -> UTCTime -> Maybe Word64 -> Word64 -> Word64 ->
               String
buildString form startTime curTime mTotal chunkSize amtRead = subPercents form
 where
  per_b = show amtRead
  per_B = bytesToUnittedStr amtRead
  per_c = show chunkSize
  per_C = bytesToUnittedStr chunkSize
  diff  = max 1 (round $ toRational $ diffUTCTime curTime startTime)
  rate  = amtRead `div` diff
  per_r = show rate
  per_R = bytesToUnittedStr rate ++ "ps"
  total = case mTotal of
            Just t  -> t
            Nothing -> error "INTERNAL ERROR (needed total w/ Nothing)"
  tleft = (total - amtRead) `div` rate
  per_t = show tleft
  hLeft = tleft `div` (60 * 60)
  mLeft = (tleft `div` 60) `mod` 60
  sLeft =  tleft           `mod` 60
  per_T = showPadded hLeft ++ ":" ++ showPadded mLeft ++
          ":" ++  showPadded sLeft
  perc  = 100 * (fromIntegral amtRead / fromIntegral total) :: Double
  per_p = show (round perc) ++ "%"
  oktot = isJust mTotal
  --
  subPercents []         = []
  subPercents ('%':rest) = subPercents' rest
  subPercents (x:rest)   = x : subPercents rest
  --
  subPercents' []                 = []
  subPercents' ('b':rest)         = per_b ++ subPercents rest
  subPercents' ('B':rest)         = per_B ++ subPercents rest
  subPercents' ('c':rest)         = per_c ++ subPercents rest
  subPercents' ('C':rest)         = per_C ++ subPercents rest
  subPercents' ('r':rest)         = per_r ++ subPercents rest
  subPercents' ('R':rest)         = per_R ++ subPercents rest
  subPercents' ('t':rest) | oktot = per_t ++ subPercents rest
  subPercents' ('T':rest) | oktot = per_T ++ subPercents rest
  subPercents' ('p':rest) | oktot = per_p ++ subPercents rest
  subPercents' ('%':rest)         = "%"   ++ subPercents rest
  subPercents' (x:rest)           = '%' : ('x' : subPercents rest)

-- show a number padded to force at least two digits.
buildString :: String ->
               UTCTime -> UTCTime -> Maybe Word64 -> Word64 -> Word64 ->
               String
buildString form startTime curTime mTotal chunkSize amtRead = subPercents form
 where
  per_b = show amtRead
  per_B = bytesToUnittedStr amtRead
  per_c = show chunkSize
  per_C = bytesToUnittedStr chunkSize
  diff  = max 1 (round $ toRational $ diffUTCTime curTime startTime)
  rate  = amtRead `div` diff
  per_r = show rate
  per_R = bytesToUnittedStr rate ++ "ps"
  total = case mTotal of
            Just t  -> t
            Nothing -> error "INTERNAL ERROR (needed total w/ Nothing)"
  tleft = (total - amtRead) `div` rate
  per_t = show tleft
  hLeft = tleft `div` (60 * 60)
  mLeft = (tleft `div` 60) `mod` 60
  sLeft =  tleft           `mod` 60
  per_T = showPadded hLeft ++ ":" ++ showPadded mLeft ++
          ":" ++  showPadded sLeft
  perc  = 100 * (fromIntegral amtRead / fromIntegral total) :: Double
  per_p = show (round perc) ++ "%"
  oktot = isJust mTotal
  --
  subPercents []         = []
  subPercents ('%':rest) = subPercents' rest
  subPercents (x:rest)   = x : subPercents rest
  --
  subPercents' []                 = []
  subPercents' ('b':rest)         = per_b ++ subPercents rest
  subPercents' ('B':rest)         = per_B ++ subPercents rest
  subPercents' ('c':rest)         = per_c ++ subPercents rest
  subPercents' ('C':rest)         = per_C ++ subPercents rest
  subPercents' ('r':rest)         = per_r ++ subPercents rest
  subPercents' ('R':rest)         = per_R ++ subPercents rest
  subPercents' ('t':rest) | oktot = per_t ++ subPercents rest
  subPercents' ('T':rest) | oktot = per_T ++ subPercents rest
  subPercents' ('p':rest) | oktot = per_p ++ subPercents rest
  subPercents' ('%':rest)         = "%"   ++ subPercents rest
  subPercents' (x:rest)           = '%' : ('x' : subPercents rest)

-- show a number padded to force at least two digits.
buildString form startTime curTime mTotal chunkSize amtRead = subPercents form
 where
  per_b = show amtRead
  per_B = bytesToUnittedStr amtRead
  per_c = show chunkSize
  per_C = bytesToUnittedStr chunkSize
  diff  = max 1 (round $ toRational $ diffUTCTime curTime startTime)
  rate  = amtRead `div` diff
  per_r = show rate
  per_R = bytesToUnittedStr rate ++ "ps"
  total = case mTotal of
            Just t  -> t
            Nothing -> error "INTERNAL ERROR (needed total w/ Nothing)"
  tleft = (total - amtRead) `div` rate
  per_t = show tleft
  hLeft = tleft `div` (60 * 60)
  mLeft = (tleft `div` 60) `mod` 60
  sLeft =  tleft           `mod` 60
  per_T = showPadded hLeft ++ ":" ++ showPadded mLeft ++
          ":" ++  showPadded sLeft
  perc  = 100 * (fromIntegral amtRead / fromIntegral total) :: Double
  per_p = show (round perc) ++ "%"
  oktot = isJust mTotal
  --
  subPercents []         = []
  subPercents ('%':rest) = subPercents' rest
  subPercents (x:rest)   = x : subPercents rest
  --
  subPercents' []                 = []
  subPercents' ('b':rest)         = per_b ++ subPercents rest
  subPercents' ('B':rest)         = per_B ++ subPercents rest
  subPercents' ('c':rest)         = per_c ++ subPercents rest
  subPercents' ('C':rest)         = per_C ++ subPercents rest
  subPercents' ('r':rest)         = per_r ++ subPercents rest
  subPercents' ('R':rest)         = per_R ++ subPercents rest
  subPercents' ('t':rest) | oktot = per_t ++ subPercents rest
  subPercents' ('T':rest) | oktot = per_T ++ subPercents rest
  subPercents' ('p':rest) | oktot = per_p ++ subPercents rest
  subPercents' ('%':rest)         = "%"   ++ subPercents rest
  subPercents' (x:rest)           = '%' : ('x' : subPercents rest)

-- show a number padded to force at least two digits.