packages feed

ghc-debug-brick-0.8.0.0: src/GHC/Debug/Brick/Render/Histogram.hs

{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE OverloadedStrings #-}
module GHC.Debug.Brick.Render.Histogram where

import Brick
import qualified GHC.Debug.Types as GD
import GHC.Debug.Types
import GHC.Debug.Brick.Model
import qualified Data.List as List
import GHC.Debug.Brick.Render.Utils

-- | Render a histogram with n lines which displays the number of elements in each bucket,
-- and how much they contribute to the total size.
histogram :: Int -> [GD.Size] -> Widget Name
histogram boxes m =
  vBox $ map displayLine (bin 0 (map calcPercentage (List.sort m )))
  where
    Size maxSize = case m of
      [] -> Size 0 -- we dont divide by maxSize if m is empty
      _ -> maximum m

    calcPercentage (Size tot) =
      (tot, (fromIntegral tot / fromIntegral maxSize) * 100 :: Double)

    displayLine (l, h, n, tot) =
      str (show l) <+> txt "%-" <+> str (show h) <+> str "%: " <+> str (show n) <+> str " " <+> renderBytes tot

    step = fromIntegral (ceiling @Double @Int (100 / fromIntegral boxes))

    bin _ [] = []
    bin k xs = case now of
                 [] -> bin (k + step) later
                 _ -> (k, k+step, length now, sum (map fst now)) : bin (k + step) later
      where
        (now, later) = span ((<= k + step) . snd) xs