module Monky.Examples.Memory
( getMemoryHandle
, getMemoryBarHandle
, getMemoryFreeHandle
, MHandle
, MFHandle
, MBHandle
)
where
import Monky.Examples.Utility
import Monky.Examples.Images
import Monky.Modules
import Monky.Memory hiding (getMemoryHandle)
import qualified Monky.Memory as M (getMemoryHandle)
newtype MHandle = MH MemoryHandle
getMemoryHandle :: IO MHandle
getMemoryHandle = fmap MH $ M.getMemoryHandle
instance PollModule MHandle where
getOutput (MH h) = do
mp <- getMemoryAvailable h
return
[ memoryImage
, MonkyPlain $ convertUnitB (mp * 1024) "B"
]
newtype MFHandle = MFH MemoryHandle
getMemoryFreeHandle :: IO MFHandle
getMemoryFreeHandle = fmap MFH $ M.getMemoryHandle
instance PollModule MFHandle where
getOutput (MFH h) = do
mp <- getMemoryFree h
return
[ MonkyImage "mem" '🐏'
, MonkyPlain $ convertUnitB (mp * 1024) "B"
]
data MBHandle = MBH Float MemoryHandle
getMemoryBarHandle
:: Float
-> IO MBHandle
getMemoryBarHandle f = fmap (MBH f) $ M.getMemoryHandle
getUsagePercents :: MemoryHandle -> IO (Int,Int)
getUsagePercents h = do
(total, avail, free, _) <- getMemoryStats h
return ((total avail) * 100 `div` total, (total free) * 100 `div` total)
newBar :: Float -> (Int, Int) -> [MonkyOut]
newBar f (u, us) =
let used = round $ fromIntegral u / f
cached = round $ fromIntegral (us u) / f
fill = round (100 / f) used cached in
[ MonkyHBar used
, MonkyColor ("#444444", "") (MonkyHBar cached)
, MonkyColor ("#262626", "") (MonkyHBar fill)
]
getNMemoryOut :: MemoryHandle -> Float -> IO [MonkyOut]
getNMemoryOut h f = do
percents <- getUsagePercents h
return $ memoryImage:newBar f percents
instance PollModule MBHandle where
getOutput (MBH f h) = getNMemoryOut h f