module System.Taffybar.Information.Memory (
  MemoryInfo(..),
  parseMeminfo
  ) where

toMB :: String -> Double
toMB :: String -> Double
toMB String
size = (String -> Double
forall a. Read a => String -> a
read String
size :: Double) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
1024

data MemoryInfo = MemoryInfo
  { MemoryInfo -> Double
memoryTotal :: Double
  , MemoryInfo -> Double
memoryFree :: Double
  , MemoryInfo -> Double
memoryBuffer :: Double
  , MemoryInfo -> Double
memoryCache :: Double
  , MemoryInfo -> Double
memorySwapTotal :: Double
  , MemoryInfo -> Double
memorySwapFree :: Double
  , MemoryInfo -> Double
memorySwapUsed :: Double -- swapTotal - swapFree
  , MemoryInfo -> Double
memorySwapUsedRatio :: Double -- swapUsed / swapTotal
  , MemoryInfo -> Double
memoryAvailable :: Double -- An estimate of how much memory is available
  , MemoryInfo -> Double
memoryRest :: Double -- free + buffer + cache
  , MemoryInfo -> Double
memoryUsed :: Double -- total - rest
  , MemoryInfo -> Double
memoryUsedRatio :: Double -- used / total
  }

emptyMemoryInfo :: MemoryInfo
emptyMemoryInfo :: MemoryInfo
emptyMemoryInfo = Double
-> Double
-> Double
-> Double
-> Double
-> Double
-> Double
-> Double
-> Double
-> Double
-> Double
-> Double
-> MemoryInfo
MemoryInfo Double
0 Double
0 Double
0 Double
0 Double
0 Double
0 Double
0 Double
0 Double
0 Double
0 Double
0 Double
0

parseLines :: [String] -> MemoryInfo -> MemoryInfo
parseLines :: [String] -> MemoryInfo -> MemoryInfo
parseLines (String
line:[String]
rest) MemoryInfo
memInfo = [String] -> MemoryInfo -> MemoryInfo
parseLines [String]
rest MemoryInfo
newMemInfo
  where newMemInfo :: MemoryInfo
newMemInfo = case String -> [String]
words String
line of
                       (String
label:String
size:[String]
_) ->
                         case String
label of
                           String
"MemTotal:"     -> MemoryInfo
memInfo { memoryTotal :: Double
memoryTotal = String -> Double
toMB String
size }
                           String
"MemFree:"      -> MemoryInfo
memInfo { memoryFree :: Double
memoryFree = String -> Double
toMB String
size }
                           String
"MemAvailable:" -> MemoryInfo
memInfo { memoryAvailable :: Double
memoryAvailable = String -> Double
toMB String
size }
                           String
"Buffers:"      -> MemoryInfo
memInfo { memoryBuffer :: Double
memoryBuffer = String -> Double
toMB String
size }
                           String
"Cached:"       -> MemoryInfo
memInfo { memoryCache :: Double
memoryCache = String -> Double
toMB String
size }
                           String
"SwapTotal:"    -> MemoryInfo
memInfo { memorySwapTotal :: Double
memorySwapTotal = String -> Double
toMB String
size }
                           String
"SwapFree:"     -> MemoryInfo
memInfo { memorySwapFree :: Double
memorySwapFree = String -> Double
toMB String
size }
                           String
_               -> MemoryInfo
memInfo
                       [String]
_ -> MemoryInfo
memInfo
parseLines [String]
_ MemoryInfo
memInfo = MemoryInfo
memInfo

parseMeminfo :: IO MemoryInfo
parseMeminfo :: IO MemoryInfo
parseMeminfo = do
  String
s <- String -> IO String
readFile String
"/proc/meminfo"
  let m :: MemoryInfo
m = [String] -> MemoryInfo -> MemoryInfo
parseLines (String -> [String]
lines String
s) MemoryInfo
emptyMemoryInfo
      rest :: Double
rest = MemoryInfo -> Double
memoryFree MemoryInfo
m Double -> Double -> Double
forall a. Num a => a -> a -> a
+ MemoryInfo -> Double
memoryBuffer MemoryInfo
m Double -> Double -> Double
forall a. Num a => a -> a -> a
+ MemoryInfo -> Double
memoryCache MemoryInfo
m
      used :: Double
used = MemoryInfo -> Double
memoryTotal MemoryInfo
m Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
rest
      usedRatio :: Double
usedRatio = Double
used Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ MemoryInfo -> Double
memoryTotal MemoryInfo
m
      swapUsed :: Double
swapUsed = MemoryInfo -> Double
memorySwapTotal MemoryInfo
m Double -> Double -> Double
forall a. Num a => a -> a -> a
- MemoryInfo -> Double
memorySwapFree MemoryInfo
m
      swapUsedRatio :: Double
swapUsedRatio = Double
swapUsed Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ MemoryInfo -> Double
memorySwapTotal MemoryInfo
m
  MemoryInfo -> IO MemoryInfo
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return MemoryInfo
m { memoryRest :: Double
memoryRest = Double
rest
           , memoryUsed :: Double
memoryUsed = Double
used
           , memoryUsedRatio :: Double
memoryUsedRatio = Double
usedRatio
           , memorySwapUsed :: Double
memorySwapUsed = Double
swapUsed
           , memorySwapUsedRatio :: Double
memorySwapUsedRatio = Double
swapUsedRatio
           }