aboutsummaryrefslogtreecommitdiff
path: root/Utility/DataUnits.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2013-07-19 19:39:14 -0400
committerGravatar Joey Hess <joey@kitenet.net>2013-07-19 19:39:14 -0400
commitd3d9381f09fba70b0d622815b797358c6bcdc548 (patch)
tree38fcd594a788a56fc6f52552346f54f51ad3b2e1 /Utility/DataUnits.hs
parent284970a49c77e8a410929231906766c538fe9269 (diff)
refactor and unify code
This fixes several bugs in both modules.
Diffstat (limited to 'Utility/DataUnits.hs')
-rw-r--r--Utility/DataUnits.hs29
1 files changed, 11 insertions, 18 deletions
diff --git a/Utility/DataUnits.hs b/Utility/DataUnits.hs
index 511b3be80..2a936f1fd 100644
--- a/Utility/DataUnits.hs
+++ b/Utility/DataUnits.hs
@@ -50,6 +50,8 @@ module Utility.DataUnits (
import Data.List
import Data.Char
+import Utility.HumanNumber
+
type ByteSize = Integer
type Name = String
type Abbrev = String
@@ -105,7 +107,7 @@ oldSchoolUnits = zipWith (curry mingle) storageUnits memoryUnits
{- approximate display of a particular number of bytes -}
roughSize :: [Unit] -> Bool -> ByteSize -> String
-roughSize units abbrev i
+roughSize units short i
| i < 0 = '-' : findUnit units' (negate i)
| otherwise = findUnit units' i
where
@@ -116,23 +118,14 @@ roughSize units abbrev i
| otherwise = findUnit us i'
findUnit [] i' = showUnit i' (last units') -- bytes
- showUnit i' (Unit s a n) = let (num, decimal) = chop i' s in
- show num ++ decimal ++ " " ++
- (if abbrev then a else plural num decimal n)
-
- chop :: Integer -> Integer -> (Integer, String)
- chop i' d =
- let (num, decimal) = properFraction $ (fromInteger i' :: Double) / fromInteger d
- dnum = round (decimal * 100) :: Integer
- ds = show dnum
- ds' = (take (2 - length ds) (repeat '0')) ++ ds
- in if (dnum == 0)
- then (num, "")
- else (num, "." ++ ds')
-
- plural num decimal n
- | num == 1 && null decimal = n
- | otherwise = n ++ "s"
+ showUnit x (Unit size abbrev name) = s ++ " " ++ unit
+ where
+ v = (fromInteger x :: Double) / fromInteger size
+ s = showImprecise 2 v
+ unit
+ | short = abbrev
+ | s == "1" = name
+ | otherwise = name ++ "s"
{- displays comparison of two sizes -}
compareSizes :: [Unit] -> Bool -> ByteSize -> ByteSize -> String