Anonymous user
File size distribution: Difference between revisions
m
→{{header|Haskell}}
Line 405:
import System.Environment (getArgs)
import System.FilePath.Posix ((</>))
import System.IO (hFileSize, withFile, IOMode(ReadMode), FilePath
import Text.Printf (printf, hPrintf)
data Item = File FilePath Integer
Line 425 ⟶ 426:
groupMinMax = (,0) <$> zip groups (pred <$> tail groups)
placeGroups []
placeGroups (d:ds)
fmap (\g@((min,max), count) ->
if d >= min && d <= max
then ((min, max), succ count)
else g
)
fileSizes :: [Item] -> [Integer]
Line 459 ⟶ 460:
groupsFromGroup :: Int -> [Integer] -> FrequencyGroup -> [FrequencyGroup]
groupsFromGroup gsize fileSizes ((min, max), count)
| length range > 1 = frequencyGroups gsize range
| otherwise = [((min, max), count)]
Line 477 ⟶ 478:
expand = ((\g@((min, max), count) ->
displaySize :: Integer -> String
Line 501 ⟶ 502:
collectItems folderPath = catch tryCollect
(\e -> do
pure []
)
Line 515:
displayFrequency :: Integer -> FrequencyGroup -> IO ()
displayFrequency filesCount ((min, max), count) =
printf "%5s <-> %5s = %
(displaySize min)
(displaySize max)
Line 558:
args <- getArgs
case parseArgs args of
Left errorMessage ->
Right (path, groupSize) -> do
items <- parallelItemCollector path
Line 566:
printf "Total folders: %d\n" folderCount
printf "Total size: %s\n" $ displaySize $ totalBytes items
putStrLn "\nDistribution:\n"
let results = expandedGroups groupSize (sizes items) (groupThreshold fileCount) items
printf "%5s <-> %4s %8s\n" "From" "To" "Count"
putStrLn $ replicate 37 '-'
mapM_ (displayFrequency fileCount) results
where
Line 577 ⟶ 579:
. initialGroups gsize</lang>
{{out}}
<pre style="height: 50rem;">$ filedist ~/Music
Using 4 worker threads
Total files: 688
Line 584 ⟶ 586:
Distribution:
0B <-> 80B = 7 1.017%: █ ▼
From <-> To Count
-------------------------------------
7MB <-> 13MB = 72 10.465%: ██████████
$ filedist ~/Music 10
Line 615 ⟶ 620:
Distribution:
From <-> To Count
-------------------------------------
178B <-> 266B = 156 22.674%: ███████████████████████▼
▲ 801B <-> 889B = 2 0.291%:
16MB <-> 19MB = 3 0.436%:
24MB <-> 27MB = 1 0.145%:
</pre>
|