Commits

Konstantine Rybnikov  committed 22096ed

Add more-or-less nice output of sizes.

  • Participants
  • Parent commits 0fd6eba

Comments (0)

Files changed (4)

File Duplicates/Duplicates.hs

   getDuplicatesSummary,
   printIndex,
   showSummary,
+  compareSummaries,
 
   NodeInfo(..),
   NodePath(..),
 import Data.IxSet        ( Indexable, IxSet, ixSet, ixGen, ixFun,
                            Proxy(Proxy), empty,
                            (@=), (&&&), toList, fromList, size )
-import Duplicates.Utils  ( allPossibleFiles )
+import Duplicates.Utils  ( allPossibleFiles, humanReadableFileSize )
 
 newtype NodePath     = NodePath { unNodePath :: FilePath }
                      deriving ( Show, Ord, Eq, Data, Typeable )
 showSummary :: DuplicateSummary -> String
 showSummary summary =
   "count: " ++ (show $ unDuplicatesCount $ getDuplicatesCount summary)
-  ++ ", size: " ++ (show $ unDuplicatesSize $ getDuplicatesSize summary)
+  ++ ", size: " ++
+  (humanReadableFileSize $ fromInteger $ unDuplicatesSize $ getDuplicatesSize summary)
 
 getInfos :: [FilePath] -> IO [NodeInfo]
 getInfos = mapM getInfo
   putStrLn "Index: "
   putStrLn $ intercalate "\n" (map ("    " ++) infos)
   return ()
+
+compareSummaries :: DuplicateSummary -> DuplicateSummary -> Ordering
+compareSummaries s1 s2 =
+  (compare 
+   (unDuplicatesSize (getDuplicatesSize s1))
+   (unDuplicatesSize (getDuplicatesSize s2)))

File Duplicates/Utils.hs

          allPossibleFiles
        , allPossibleDirs
        , safeHead
+       , humanReadableFileSize
        ) where
 
 import System.Directory (getDirectoryContents, doesFileExist)
 safeHead :: [a] -> Maybe a
 safeHead [] = Nothing
 safeHead xs = Just (head xs)
+
+prettyShowDouble :: Double -> String
+prettyShowDouble d =
+  show ((fromInteger (round (d * 100)) :: Double) / 100)
+
+humanReadableFileSize :: Integer -> String
+humanReadableFileSize size =
+  humanReadableFileSize' (fromInteger size) ["", "K", "M", "G", "T"]
+  where humanReadableFileSize' size [] = (prettyShowDouble size)
+        humanReadableFileSize' size labels =
+          if size < 1024.0 && size > -1024.0
+          then (prettyShowDouble size) ++ (head labels)
+          else humanReadableFileSize' (size / 1024) (tail labels)

File duplicates.hs

-import Data.List (intercalate)
+import Data.List             ( intercalate, sortBy )
 import System.Environment    ( getArgs )
 import System.Directory      ( doesDirectoryExist, canonicalizePath )
+
 import Duplicates.Utils      ( safeHead, allPossibleDirs )
-import Duplicates.Duplicates ( showSummary, indexPath, getDuplicatesSummary, printIndex )
+import Duplicates.Duplicates ( showSummary, indexPath, getDuplicatesSummary )
+import Duplicates.Duplicates ( printIndex, compareSummaries )
 
 getPathFromArgs :: [String] -> Maybe String
 getPathFromArgs args = safeHead args
           index <- indexPath canonicalized
           -- printIndex index
           dirs' <- allPossibleDirs canonicalized
-          let dirs = canonicalized : dirs'
           -- putStrLn "Dirs:"
           -- putStrLn $ intercalate "\n" dirs
+          let dirs = canonicalized : dirs'
           let dirSummaries = map (getDuplicatesSummary index) dirs
           let dirsWithSummary = zip dirs dirSummaries
-          putStrLn "Number of duplicate files:"
+          let sortedDirsWithSummary =
+                sortBy (\(d1, s1) (d2, s2) -> (compareSummaries s1 s2)) dirsWithSummary
           let showDirWithSummary (dir, summary) = dir ++ ": " ++ (showSummary summary)
-          let stats = map showDirWithSummary dirsWithSummary
+          let stats = map showDirWithSummary sortedDirsWithSummary
+          putStrLn "Duplicate stats:"
           putStrLn $ intercalate "\n" $ map ("> " ++) $ stats
           return ()

File tests/TestUtils.hs

+import Test.HUnit
+import Duplicates.Utils
+
+should_show_kibibytes =
+  TestCase (
+    assertEqual "should_show_kibibytes"
+    "1.0K"
+    (humanReadableFileSize 1024))
+
+should_show_kibibytes_complex =
+  TestCase (
+    assertEqual "should_show_kibibytes_complex"
+    "1.29K"
+    (humanReadableFileSize 1324))
+
+should_show_mebibytes =
+  TestCase (
+    assertEqual "should_show_mebibytes"
+    "1.1M"
+    (humanReadableFileSize (ceiling $ 1024.0 * 1024.0 * 1.1)))
+
+should_show_tebibytes =
+  TestCase (
+    assertEqual "should_show_tebibytes"
+    "1.8T"
+    (humanReadableFileSize (ceiling $ 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1.8)))
+
+tests = TestList [
+  TestLabel "should_show_kibibytes" should_show_kibibytes,
+  TestLabel "should_show_kibibytes_complex" should_show_kibibytes_complex,
+  TestLabel "should_show_mebibytes" should_show_mebibytes,
+  TestLabel "should_show_tebibytes" should_show_tebibytes
+  ]
+
+main = do
+  runTestTT tests