Move Network.Units into Common

This commit is contained in:
towards-a-new-leftypol 2024-03-06 00:46:51 -05:00
parent c1d966e0cd
commit 66e8d902c5
3 changed files with 2 additions and 20 deletions

View File

@ -67,6 +67,7 @@ executable chandlr
Network.Http
Common.Network.HttpTypes
Network.Client
Common.Network.Units
Common.Network.ClientTypes
Common.Network.CatalogPostType
Common.Network.BoardType

@ -1 +1 @@
Subproject commit 5ca701e30861498565fd4927ea1c9ea3ea105b8c
Subproject commit 0e5547e7475679cd5a3a449b6f09f0f6d063244e

View File

@ -1,19 +0,0 @@
module Network.Units where
import Text.Printf (printf)
data Unit = KiB | MiB | GiB | TiB | PiB | EiB | ZiB | YiB
| KB | MB | GB | TB | PB | EB | ZB | YB
deriving (Enum, Show, Bounded)
bytesToHumanReadable :: Integral a => a -> Bool -> String
bytesToHumanReadable bytes binaryPrefix = printf "%.2f %s" size (show unit)
where
(factor, units) = if binaryPrefix
then (1024.0, enumFrom KiB) -- Binary prefix, 1024 base, explicitly Double
else (1000.0, enumFrom KB) -- Decimal prefix, 1000 base, explicitly Double
(size, unit) = foldl (\(accSize, accUnit) u ->
if accSize < factor then (accSize, accUnit)
else (accSize / factor, u))
(fromIntegral bytes :: Double, if binaryPrefix then KiB else KB) -- Explicitly Double
units